带有子域名和两台服务器的 nginx

带有子域名和两台服务器的 nginx

我有两台服务器:第一台服务器的 IP 地址为 192.168.1.216,主机名为hub.domain.tld (jupyterhub)。DNS 记录指向公网 IP 地址,所有对端口80和 的请求都443转发到192.168.1.216

第一台服务器上的初始 Nginx 配置设置很简单,并且运行正常。

但是,问题出现在第二台服务器上,该服务器也配置了 Nginx,如下所述。问题是,所有发送到的请求vscode.domain.tld都被重定向到,hub.domain.tld而不是192.168.1.234:8080(code-server)

但是,当我移动并将此配置添加到第一台服务器时,一切都运行正常。

我想拆分配置文件。因为我想在docker compose中容器化Nginx:

  • 代码服务器,
  • 探索数据库,
  • grafana。
# /etc/nginx/sites-available/default




# top-level http config for websocket headers
# If Upgrade is defined, Connection = upgrade
# If Upgrade is empty, Connection = close
map $http_upgrade $connection_upgrade {
    default upgrade;
    ''      close;
}

# HTTP server to redirect all 80 traffic to SSL/HTTPS
server {
    listen 80;
    server_name vscode.domain.tld;

    # Tell all requests to port 80 to be 302 redirected to HTTPS
    return 302 https://$host$request_uri;
}

# HTTPS server to handle VS Code
server {
    listen 443 ssl;
    server_name vscode.domain.tld;

    ssl_certificate /etc/letsencrypt/live/vscode.domain.tld/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/vscode.domain.tld/privkey.pem;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_dhparam /etc/ssl/certs/dhparam.pem;
    ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA';
    ssl_session_timeout 1d;
    ssl_session_cache shared:SSL:50m;
    ssl_stapling on;
    ssl_stapling_verify on;
    add_header Strict-Transport-Security max-age=15768000;

    # Managing literal requests to the VS Code front end
    location / {
        proxy_pass http://192.168.1.234:8080;

        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        # websocket headers
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
        proxy_set_header X-Scheme $scheme;

        proxy_buffering off;
    }

    # Managing requests to verify letsencrypt host
    location ~ /.well-known {
        allow all;
    }
}


答案1

Nginx 未正确配置proxy_pass,没有代理配置将流量引导code-server192.168.1.234:8080

在您的 HTTPS 服务器块中,将此块添加到代理请求中:

server {
    listen 443 ssl;
    server_name vscode.domain.tld;

    ssl_certificate /etc/letsencrypt/live/vscode.domain.tld/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/vscode.domain.tld/privkey.pem;

    # ...

    location / {
        proxy_pass http://192.168.1.234:8080;
        proxy_set_header Host $host;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Accept-Encoding gzip;
    }
}

相关内容