【问题标题】:How to correctly deploy/setup NestJS backend using Nginx?如何使用 Nginx 正确部署/设置 NestJS 后端?
【发布时间】:2021-09-07 07:15:24
【问题描述】:

我正在尝试使用 Angular 和 NestJs 在 Ubuntu 远程服务器上使用 Nginx 部署我的 web 应用程序。 我让前端在https://ikse.fransenit.nl/products 上工作,但无法让后端工作。它在本地运行良好。当我尝试访问 /api/products 时,我得到一个 502 bad gateway。

启动 NestJS 后端时:

nginx 配置

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/ikse/html;

    server_name ikse.fransenit.nl www.ikse.fransenit.nl;
    
    index index.html index.htm;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ /index.html;
                # proxy_pass http://localhost:8080;
                # 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 /api/ {
            proxy_pass https://localhost:3000;
        }

    listen [::]:443 ssl; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/ikse.fransenit.nl/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/ikse.fransenit.nl/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

我错过了什么和/或做错了什么?

【问题讨论】:

  • 您可以尝试将 localhost 替换为 127.0.0.1 并再试一次,看看是否有效?还将 https 更改为 http(除非您在 nodejs 服务器中有 https)从位置 - /api/ 块。

标签: angular nginx nestjs nginx-config nginx-location


【解决方案1】:

我不知道你是否解决了它,但我认为你应该在proxy_pass 选项的https://localhost:3000 末尾添加/api/。因为在当前配置下你需要使https://doma.in.com/api/api/profile

【讨论】:

  • 我不久前修复了它,这确实是答案,不过谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-06-03
  • 2020-04-15
  • 1970-01-01
  • 2022-08-14
  • 2020-10-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多