子域名和代理服务器的 nginx 配置

子域名和代理服务器的 nginx 配置

我在这里尝试了很多其他帖子解决方案,但没有人能帮助我解决我的问题。我有一个简单的 nginx 配置,带有 https 和一个带有主路径的 node.js 应用程序/,对于子域,还有另一个路径/api。我可以正确访问mywebapp.com,但如果我尝试访问,它会将我重定向到“欢迎使用 nginx”页面。我有指向我的服务器 ip 地址的api.mywebapp.comDNS 记录。api.mywebapp.com

这是我的 nginx 配置块:

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;

    server_name mywebapp.com;

    # SSL
    include /etc/letsencrypt/mycerts.conf;
    include /etc/letsencrypt/options-ssl-nginx.conf;

    # reverse proxy
    location / {
        proxy_pass http://localhost:8000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_cache_bypass $http_upgrade;
    }
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;

    server_name api.mywebapp.com;

    # SSL 
    include /etc/letsencrypt/mycerts.conf;      
    include /etc/letsencrypt/options-ssl-nginx.conf;

    location /api {
        limit_req zone=mylimit;
        proxy_pass http://localhost:8000/api;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_cache_bypass $http_upgrade;
    }
}

我遗漏了什么?

答案1

您已将代理配置为

location /api {
  # ...
}

因此只有通过 才可以到达https://api.mywebapp.com/api/

你很可能想将其更改为

location / {
  proxy_pass http://localhost:8000/api;  # <- keep this
  # ...
}

相关内容