我已经用nodejs创建了一个restful api,并且我计划使用sapper/svelte作为前端。最后,这些将是单独的应用程序,我想在具有相同域的同一服务器上运行它们。这种做法合理吗?如果是,我的 nginx 配置文件应该是什么样子?如果不是,我的方法应该是什么?
这是我的 api 配置:
server {
server_name domain.name;
location / {
proxy_pass http://localhost:5000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
.
.
.
}
最佳答案
按照最佳实践,您的 API 将位于 BASE/api/下
这将允许您在同一服务器上托管后端+前端
server {
server_name domain.name;
location /api/ { # Backend
proxy_pass http://localhost:5000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
...
}
location / { # Frontend
root /app-path/;
index index.html;
try_files $uri $uri/ /index.html;
...
}
}
关于node.js - 我想使用 nginx 在同一台服务器上部署后端和前端单独的应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60208157/