强烈建议使用NginX
托管前端静态资源,并反向代理后端服务,配置如下:
参考配置
- 1server {
- 2
- 3 listen 8000;
- 4 server_name localhost;
- 5
- 6 set $node_ip 127.0.0.1;
- 7 set $node_port 7002;
- 8
- 9 root /path/to/project/dist/web;
- 10
- 11 location /public {
- 12 root /path/to/home/cabloy/project-name;
- 13 internal;
- 14 }
- 15
- 16 location /api/ {
- 17 proxy_http_version 1.1;
- 18 proxy_set_header X-Real-IP $remote_addr;
- 19 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- 20 proxy_set_header X-Forwarded-Host $server_name;
- 21 proxy_set_header X-Forwarded-Proto $scheme;
- 22 proxy_set_header Host $http_host;
- 23 proxy_set_header X-NginX-Proxy true;
- 24 proxy_set_header Upgrade $http_upgrade;
- 25 proxy_set_header Connection "upgrade";
- 26 proxy_pass http://$node_ip:$node_port$request_uri;
- 27 proxy_redirect off;
- 28 proxy_buffer_size 64k;
- 29 proxy_buffers 4 32k;
- 30 proxy_busy_buffers_size 64k;
- 31 }
- 32
- 33 location /socket.io/ {
- 34 proxy_http_version 1.1;
- 35 proxy_set_header X-Real-IP $remote_addr;
- 36 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- 37 proxy_set_header X-Forwarded-Host $server_name;
- 38 proxy_set_header X-Forwarded-Proto $scheme;
- 39 proxy_set_header Host $http_host;
- 40 proxy_set_header X-NginX-Proxy true;
- 41 proxy_set_header Upgrade $http_upgrade;
- 42 proxy_set_header Connection "upgrade";
- 43 proxy_pass http://$node_ip:$node_port$request_uri;
- 44 proxy_redirect off;
- 45 proxy_buffer_size 64k;
- 46 proxy_buffers 4 32k;
- 47 proxy_busy_buffers_size 64k;
- 48 }
- 49
- 50}
名称 | 说明 |
---|---|
root | 前端构建的静态资源 |
location /public | 存储系统运行中产生的资源文件,比如上传的图片 、文件 等 |
location /api/ | 后端API服务 |
location /socket.io/ | 后端socket.io服务 |
关于 location /public
在这里要重点介绍一下location /public
1. 默认配置
在生产环境,后端服务(比如模块a-file
)产生的资源文件,会自动存储在目录{HomeDir}/cabloy/{ProjectName}/public
中。我们只需在NginX配置文件中将location /public
的root
值设为{HomeDir}/cabloy/{ProjectName}
即可
2. 修改配置
可以在项目配置文件中修改模块a-base
的参数
src/backend/config/config.prod.js
- 1config.modules = {
- 2 'a-base': {
- 3 publicDir: '/path/to/public',
- 4 },
- 5};
publicDir
: 必须是绝对路径,相当于在生成环境中覆盖路径{HomeDir}/cabloy/{ProjectName}/public
3. location /public
为何要指定internal
?
因为CabloyJS采用了NginX的X-Accel-Redirect
机制,location /public
用于后端的文件跳转,前端并不能直接访问
关于
X-Accel-Redirect
的更多信息,请参见:X-Accel
4. location /public
指定internal
能达到什么效果?
比如通过模块a-file
上传了一张图片,该图片在服务器的实际存储文件名为:{publicDir}/{instanceId}/file/image/2019-11-18/6fb2784415f949948355ef843d78459a-126_0.jpg
,而对外提供的下载链接是:https://portal.cabloy.com/api/a/file/file/download/46c68dae591b41c3bd2181facc8d0af9.jpg
。前端通过该下载链接可以直接下载图片,但是并不知道该图片在服务器存储的真实位置和真实名称
服务器生产配置
在生产环境一般会使用https协议,因此,相应的配置如下:
- 1server {
- 2
- 3 listen 80;
- 4 server_name yourdomain.com;
- 5
- 6 return 301 https://$server_name$request_uri;
- 7}
- 8
- 9server {
- 10
- 11 listen 443 ssl;
- 12 server_name yourdomain.com;
- 13
- 14 set $node_ip 127.0.0.1;
- 15 set $node_port 7003;
- 16
- 17 root /path/to/project/dist;
- 18
- 19 location /public {
- 20 root /path/to/home/cabloy/project-name;
- 21 internal;
- 22 }
- 23
- 24 location /api/ {
- 25 proxy_http_version 1.1;
- 26 proxy_set_header X-Real-IP $remote_addr;
- 27 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- 28 proxy_set_header X-Forwarded-Host $server_name;
- 29 proxy_set_header X-Forwarded-Proto $scheme;
- 30 proxy_set_header Host $http_host;
- 31 proxy_set_header X-NginX-Proxy true;
- 32 proxy_set_header Upgrade $http_upgrade;
- 33 proxy_set_header Connection "upgrade";
- 34 proxy_pass http://$node_ip:$node_port$request_uri;
- 35 proxy_redirect off;
- 36 }
- 37
- 38 location /socket.io/ {
- 39 proxy_http_version 1.1;
- 40 proxy_set_header X-Real-IP $remote_addr;
- 41 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- 42 proxy_set_header X-Forwarded-Host $server_name;
- 43 proxy_set_header X-Forwarded-Proto $scheme;
- 44 proxy_set_header Host $http_host;
- 45 proxy_set_header X-NginX-Proxy true;
- 46 proxy_set_header Upgrade $http_upgrade;
- 47 proxy_set_header Connection "upgrade";
- 48 proxy_pass http://$node_ip:$node_port$request_uri;
- 49 proxy_redirect off;
- 50 }
- 51
- 52 ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
- 53 ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;
- 54}
服务器开发配置
如果我们想在服务器上配置开发环境,需要通过NginX把前端开发服务
和后端开发服务
反向代理给前端,相应的配置如下:
- 1server {
- 2
- 3 listen 80;
- 4 server_name yourdomain.com;
- 5
- 6 return 301 https://$server_name$request_uri;
- 7
- 8
- 9server {
- 10
- 11 listen 443 ssl;
- 12 server_name yourdomain.com;
- 13
- 14 set $node_ip 127.0.0.1;
- 15 set $node_ip2 127.0.0.1;
- 16
- 17 set $node_port 7102;
- 18 set $node_port2 9192;
- 19
- 20 location /api/ {
- 21 proxy_http_version 1.1;
- 22 proxy_set_header X-Real-IP $remote_addr;
- 23 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- 24 proxy_set_header X-Forwarded-Host $server_name;
- 25 proxy_set_header X-Forwarded-Proto $scheme;
- 26 proxy_set_header Host $http_host;
- 27 proxy_set_header X-NginX-Proxy true;
- 28 proxy_set_header Upgrade $http_upgrade;
- 29 proxy_set_header Connection "upgrade";
- 30 proxy_pass http://$node_ip:$node_port$request_uri;
- 31 proxy_redirect off;
- 32 }
- 33
- 34 location /socket.io/ {
- 35 proxy_http_version 1.1;
- 36 proxy_set_header X-Real-IP $remote_addr;
- 37 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- 38 proxy_set_header X-Forwarded-Host $server_name;
- 39 proxy_set_header X-Forwarded-Proto $scheme;
- 40 proxy_set_header Host $http_host;
- 41 proxy_set_header X-NginX-Proxy true;
- 42 proxy_set_header Upgrade $http_upgrade;
- 43 proxy_set_header Connection "upgrade";
- 44 proxy_pass http://$node_ip:$node_port$request_uri;
- 45 proxy_redirect off;
- 46 }
- 47
- 48 location /public/ {
- 49 proxy_http_version 1.1;
- 50 proxy_set_header X-Real-IP $remote_addr;
- 51 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- 52 proxy_set_header X-Forwarded-Host $server_name;
- 53 proxy_set_header X-Forwarded-Proto $scheme;
- 54 proxy_set_header Host $http_host;
- 55 proxy_set_header X-NginX-Proxy true;
- 56 proxy_set_header Upgrade $http_upgrade;
- 57 proxy_set_header Connection "upgrade";
- 58 proxy_pass http://$node_ip:$node_port$request_uri;
- 59 proxy_redirect off;
- 60 }
- 61
- 62 location / {
- 63 proxy_http_version 1.1;
- 64 proxy_set_header X-Real-IP $remote_addr;
- 65 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- 66 proxy_set_header X-Forwarded-Host $server_name;
- 67 proxy_set_header X-Forwarded-Proto $scheme;
- 68 proxy_set_header Host $http_host;
- 69 proxy_set_header X-NginX-Proxy true;
- 70 proxy_set_header Upgrade $http_upgrade;
- 71 proxy_set_header Connection "upgrade";
- 72 proxy_pass http://$node_ip2:$node_port2$request_uri;
- 73 proxy_redirect off;
- 74 }
- 75
- 76 ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
- 77 ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;
- 78}
评论: