Nginx-Konfiguration für Subdomain und Proxyserver

Nginx-Konfiguration für Subdomain und Proxyserver

Ich habe viele andere hier gepostete Lösungen ausprobiert und niemand konnte mir helfen, mein Problem zu lösen. Ich habe eine einfache Nginx-Konfiguration mit https und eine Node.js-App mit dem Hauptpfad /und einem anderen Pfad für eine Subdomain /api. Ich kann mywebapp.comkorrekt darauf zugreifen, aber wenn ich versuche, darauf zuzugreifen, api.mywebapp.comwerde ich auf die Seite „Willkommen bei Nginx“ umgeleitet. Ich habe den DNS-Eintrag, api.mywebapp.comder auf die IP-Adresse meines Servers verweist.

Dies sind meine Nginx-Konfigurationsblöcke:

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;
    }
}

Was fehlt mir?

Antwort1

Sie haben Ihren Proxy konfiguriert für

location /api {
  # ...
}

Es ist also nur mit erreichbar https://api.mywebapp.com/api/.

Sie möchten dies höchstwahrscheinlich ändern in

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

verwandte Informationen