我必须在nginx服务器上部署一个SPA。后端API必须位于同一服务器中,但具有不同的域和端口。
假设前端域是front.example
,后端域是back.example
。
- 前端SPA应在
http://front.example:80
上运行 - 后端API应在
http://back.example:4444
上运行
nginx服务器中的正确配置是什么?
以下是解决方案:
/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.示例
server {
server_name back.example www.back.example;
location / {
proxy_pass http://0.0.0.0:4444;
}
}
注意:请确保在etc/nginx/nginx.conf
中仅包含sites-enabled/*
。