强烈建议使用NginX托管前端静态资源,并反向代理后端服务,配置如下:

参考配置

  1. 1server {
  2. 2
  3. 3 listen 8000;
  4. 4 server_name localhost;
  5. 5
  6. 6 set $node_ip 127.0.0.1;
  7. 7 set $node_port 7002;
  8. 8
  9. 9 root /path/to/project/dist/web;
  10. 10
  11. 11 location /public {
  12. 12 root /path/to/home/cabloy/project-name;
  13. 13 internal;
  14. 14 }
  15. 15
  16. 16 location /api/ {
  17. 17 proxy_http_version 1.1;
  18. 18 proxy_set_header X-Real-IP $remote_addr;
  19. 19 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  20. 20 proxy_set_header X-Forwarded-Host $server_name;
  21. 21 proxy_set_header X-Forwarded-Proto $scheme;
  22. 22 proxy_set_header Host $http_host;
  23. 23 proxy_set_header X-NginX-Proxy true;
  24. 24 proxy_set_header Upgrade $http_upgrade;
  25. 25 proxy_set_header Connection "upgrade";
  26. 26 proxy_pass http://$node_ip:$node_port$request_uri;
  27. 27 proxy_redirect off;
  28. 28 proxy_buffer_size 64k;
  29. 29 proxy_buffers 4 32k;
  30. 30 proxy_busy_buffers_size 64k;
  31. 31 }
  32. 32
  33. 33 location /socket.io/ {
  34. 34 proxy_http_version 1.1;
  35. 35 proxy_set_header X-Real-IP $remote_addr;
  36. 36 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  37. 37 proxy_set_header X-Forwarded-Host $server_name;
  38. 38 proxy_set_header X-Forwarded-Proto $scheme;
  39. 39 proxy_set_header Host $http_host;
  40. 40 proxy_set_header X-NginX-Proxy true;
  41. 41 proxy_set_header Upgrade $http_upgrade;
  42. 42 proxy_set_header Connection "upgrade";
  43. 43 proxy_pass http://$node_ip:$node_port$request_uri;
  44. 44 proxy_redirect off;
  45. 45 proxy_buffer_size 64k;
  46. 46 proxy_buffers 4 32k;
  47. 47 proxy_busy_buffers_size 64k;
  48. 48 }
  49. 49
  50. 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 /publicroot值设为{HomeDir}/cabloy/{ProjectName}即可

2. 修改配置

可以在项目配置文件中修改模块a-base的参数

src/backend/config/config.prod.js

  1. 1config.modules = {
  2. 2 'a-base': {
  3. 3 publicDir: '/path/to/public',
  4. 4 },
  5. 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协议,因此,相应的配置如下:

  1. 1server {
  2. 2
  3. 3 listen 80;
  4. 4 server_name yourdomain.com;
  5. 5
  6. 6 return 301 https://$server_name$request_uri;
  7. 7}
  8. 8
  9. 9server {
  10. 10
  11. 11 listen 443 ssl;
  12. 12 server_name yourdomain.com;
  13. 13
  14. 14 set $node_ip 127.0.0.1;
  15. 15 set $node_port 7003;
  16. 16
  17. 17 root /path/to/project/dist;
  18. 18
  19. 19 location /public {
  20. 20 root /path/to/home/cabloy/project-name;
  21. 21 internal;
  22. 22 }
  23. 23
  24. 24 location /api/ {
  25. 25 proxy_http_version 1.1;
  26. 26 proxy_set_header X-Real-IP $remote_addr;
  27. 27 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  28. 28 proxy_set_header X-Forwarded-Host $server_name;
  29. 29 proxy_set_header X-Forwarded-Proto $scheme;
  30. 30 proxy_set_header Host $http_host;
  31. 31 proxy_set_header X-NginX-Proxy true;
  32. 32 proxy_set_header Upgrade $http_upgrade;
  33. 33 proxy_set_header Connection "upgrade";
  34. 34 proxy_pass http://$node_ip:$node_port$request_uri;
  35. 35 proxy_redirect off;
  36. 36 }
  37. 37
  38. 38 location /socket.io/ {
  39. 39 proxy_http_version 1.1;
  40. 40 proxy_set_header X-Real-IP $remote_addr;
  41. 41 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  42. 42 proxy_set_header X-Forwarded-Host $server_name;
  43. 43 proxy_set_header X-Forwarded-Proto $scheme;
  44. 44 proxy_set_header Host $http_host;
  45. 45 proxy_set_header X-NginX-Proxy true;
  46. 46 proxy_set_header Upgrade $http_upgrade;
  47. 47 proxy_set_header Connection "upgrade";
  48. 48 proxy_pass http://$node_ip:$node_port$request_uri;
  49. 49 proxy_redirect off;
  50. 50 }
  51. 51
  52. 52 ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
  53. 53 ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;
  54. 54}

服务器开发配置

如果我们想在服务器上配置开发环境,需要通过NginX把前端开发服务后端开发服务反向代理给前端,相应的配置如下:

  1. 1server {
  2. 2
  3. 3 listen 80;
  4. 4 server_name yourdomain.com;
  5. 5
  6. 6 return 301 https://$server_name$request_uri;
  7. 7
  8. 8
  9. 9server {
  10. 10
  11. 11 listen 443 ssl;
  12. 12 server_name yourdomain.com;
  13. 13
  14. 14 set $node_ip 127.0.0.1;
  15. 15 set $node_ip2 127.0.0.1;
  16. 16
  17. 17 set $node_port 7102;
  18. 18 set $node_port2 9192;
  19. 19
  20. 20 location /api/ {
  21. 21 proxy_http_version 1.1;
  22. 22 proxy_set_header X-Real-IP $remote_addr;
  23. 23 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  24. 24 proxy_set_header X-Forwarded-Host $server_name;
  25. 25 proxy_set_header X-Forwarded-Proto $scheme;
  26. 26 proxy_set_header Host $http_host;
  27. 27 proxy_set_header X-NginX-Proxy true;
  28. 28 proxy_set_header Upgrade $http_upgrade;
  29. 29 proxy_set_header Connection "upgrade";
  30. 30 proxy_pass http://$node_ip:$node_port$request_uri;
  31. 31 proxy_redirect off;
  32. 32 }
  33. 33
  34. 34 location /socket.io/ {
  35. 35 proxy_http_version 1.1;
  36. 36 proxy_set_header X-Real-IP $remote_addr;
  37. 37 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  38. 38 proxy_set_header X-Forwarded-Host $server_name;
  39. 39 proxy_set_header X-Forwarded-Proto $scheme;
  40. 40 proxy_set_header Host $http_host;
  41. 41 proxy_set_header X-NginX-Proxy true;
  42. 42 proxy_set_header Upgrade $http_upgrade;
  43. 43 proxy_set_header Connection "upgrade";
  44. 44 proxy_pass http://$node_ip:$node_port$request_uri;
  45. 45 proxy_redirect off;
  46. 46 }
  47. 47
  48. 48 location /public/ {
  49. 49 proxy_http_version 1.1;
  50. 50 proxy_set_header X-Real-IP $remote_addr;
  51. 51 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  52. 52 proxy_set_header X-Forwarded-Host $server_name;
  53. 53 proxy_set_header X-Forwarded-Proto $scheme;
  54. 54 proxy_set_header Host $http_host;
  55. 55 proxy_set_header X-NginX-Proxy true;
  56. 56 proxy_set_header Upgrade $http_upgrade;
  57. 57 proxy_set_header Connection "upgrade";
  58. 58 proxy_pass http://$node_ip:$node_port$request_uri;
  59. 59 proxy_redirect off;
  60. 60 }
  61. 61
  62. 62 location / {
  63. 63 proxy_http_version 1.1;
  64. 64 proxy_set_header X-Real-IP $remote_addr;
  65. 65 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  66. 66 proxy_set_header X-Forwarded-Host $server_name;
  67. 67 proxy_set_header X-Forwarded-Proto $scheme;
  68. 68 proxy_set_header Host $http_host;
  69. 69 proxy_set_header X-NginX-Proxy true;
  70. 70 proxy_set_header Upgrade $http_upgrade;
  71. 71 proxy_set_header Connection "upgrade";
  72. 72 proxy_pass http://$node_ip2:$node_port2$request_uri;
  73. 73 proxy_redirect off;
  74. 74 }
  75. 75
  76. 76 ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
  77. 77 ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;
  78. 78}