It is strongly recommended to use nginx to host frontend static resources and reverse proxy backend service. The configuration is as follows:

General Configuration

  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}
Name Description
root frontend static resources
location /public resource files generated during system operations, such as uploaded pictures, uploaded files, etc.
location /api/ backend service

About location /public

I want to emphasize location /public here:

1. Default Configuration

In the production environment, resource files generated by backend service (such as module a-file) will be automatically stored in the directory {HomeDir}/cabloy/{ProjectName}/public. We just need to set the root value of location /public to {HomeDir}/cabloy/{ProjectName} in the nginx configuration file

2. Override Configuration

The configuration of module a-Base can be overrided in the project configuration file

src/backend/config/config.prod.js

  1. 1config.modules = {
  2. 2 'a-base': {
  3. 3 publicDir: '/path/to/public',
  4. 4 },
  5. 5};
  • publicDir: must be an absolute path, equivalent to overwriting the path in the production environment: {HomeDir}/cabloy/{ProjectName}/public

3. Why is internal specified for location /public?

Because CabloyJS uses nginx’s x-accel-redirect mechanism, and location /public is used for file redirect on the backend, which is not directly accessible by the frontend

For more information about x-accel-redirect, see: X-Accel

4. What effect does location /public specify internal?

For example, a picture is uploaded through the module a-file. The actual storage file name of the picture on the server is: {publicDir}/{instanceId}/file/image/2019-11-18/6fb2784415f949948355ef843d78459a-126_0.jpg, while the download link is: https://portal.cabloy.com/api/a/file/file/download/46c68dae591b41c3bd2181facc8d0af9.jpg. The frontend can download the picture directly through the download link, but it does not know the real location and real name of the picture stored in the server

Production Configuration

Generally, HTTPS protocol is used in production environment, so the corresponding configuration is as follows:

  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 ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
  39. 39 ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;
  40. 40}

Developmemt Configuration

If we want to configure the development environment on the server, and then reverse proxy the frontend service and backend service to the frontend through nginx, the corresponding configuration is as follows:

  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 /public/ {
  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 / {
  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_ip2:$node_port2$request_uri;
  59. 59 proxy_redirect off;
  60. 60 }
  61. 61
  62. 62 ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
  63. 63 ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;
  64. 64}