nginx:如何将SPA和后端api部署到具有不同域和端口号的生产服务器



我必须在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/*

相关内容

最新更新