子網域名稱和代理伺服器的 nginx 配置

子網域名稱和代理伺服器的 nginx 配置

我在這裡嘗試了很多其他後期解決方案,但沒有人可以幫助我解決我的問題。我有一個簡單的 nginx 配置,其中包含 https 和一個帶有主路徑的 Node.js 應用程序/,對於子域,還有另一個路徑/api。我可以正確訪問mywebapp.com,但如果我嘗試訪問,api.mywebapp.com它會將我重定向到“歡迎來到 nginx”頁面。我有api.mywebapp.com指向我的伺服器 IP 位址的DNS 記錄。

這是我的 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
  # ...
}

相關內容