我想使用 nginx 在同一台服务器上部署后端和前端单独的应用程序

Ker*_*rem 4 nginx node.js svelte sapper

我已经用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;
    }
   .
   .
   .
}
Run Code Online (Sandbox Code Playgroud)

小智 8

按照最佳实践,您的 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;

        ...
    }
}
Run Code Online (Sandbox Code Playgroud)