【问题标题】:nginx: How to deploy an SPA and the backend api to a production server with different domains and port numbersnginx:如何将 SPA 和后端 api 部署到具有不同域和端口号的生产服务器
【发布时间】:2020-03-03 19:24:35
【问题描述】:
我必须将 SPA 部署到 nginx 服务器。后端 API 必须在同一台服务器上,但域和端口不同。
假设前端域是front.example,后端域是back.example。
- 前端 SPA 应该在
http://front.example:80 运行
- 后端 API 应该在
http://back.example:4444 运行
nginx服务器的正确配置是什么?
【问题讨论】:
标签:
node.js
angularjs
nginx
single-page-application
web-deployment
【解决方案1】:
解决办法如下:
/etc/nginx/sites-enabled/front.example
server {
listen 80;
listen [::]:80;
root /var/www/html;
index index.html index.htm;
server_name front.example www.front.example;
location / {
try_files $uri $uri/ /index.html;
}
}
/etc/nginx/sites-enabled/back.example
server {
server_name back.example www.back.example;
location / {
proxy_pass http://0.0.0.0:4444;
}
}
注意:确保在etc/nginx/nginx.conf 中只包含sites-enabled/*。