当在子目录上使用 proxy_pass 将请求定向到节点服务器时,Nginx 会覆盖所需的端口

问题描述 投票:0回答:1

我有以下 nginx 配置:

 
服务器{ 听80; 听 443 ssl;

ssl on;
    ssl_certificate /home/tom/local.tjrhodes.com.pem; 
ssl_certificate_key /home/tom/local.tjrhodes.com-key.pem;

root /home/tom/Nextcloud/local.tjrhodes.com/webroot/;

error_log /home/tom/Nextcloud/local.tjrhodes.com/logs/error.log;

# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;

server_name local.tjrhodes.com;

location / {
    # First attempt to serve request as file, then
    # as directory, then fall back to displaying a 404.
    try_files $uri $uri/ =404;
}

location /pat/ {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Host $host;
        proxy_set_header Host $host;
        proxy_pass http://127.0.0.1:3000;
        proxy_redirect off;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
}

}`

我在客户端上使用 fetch 调用 /pat/ 路径,但 proxy_pass 命令上的端口 3000 被 443 覆盖,因为我通过 https 进行本地测试。我在配置中缺少什么? `

我尝试过节点服务器使用正确的端口(127.0.0.1:3000)直接调用它,并且它响应正常。不过,通过 https 连接时我无法从客户端发出请求。

node.js nginx ssl port proxypass
1个回答
0
投票

将您的 nginx 配置更改为:

server {
    listen 80;
    listen 443 ssl;

    ssl_certificate /home/tom/local.tjrhodes.com.pem; 
    ssl_certificate_key /home/tom/local.tjrhodes.com-key.pem;

    root /home/tom/Nextcloud/local.tjrhodes.com/webroot/;

    error_log /home/tom/Nextcloud/local.tjrhodes.com/logs/error.log;

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html;

    server_name local.tjrhodes.com;

    location / {
        try_files $uri $uri/ =404;
    }

    location /pat/ {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header Host $host; # You might want to use $proxy_host or directly specify `127.0.0.1:3000` depending on your setup
        proxy_set_header X-Forwarded-Proto $scheme; # Pass the original protocol (http or https)
        proxy_pass http://127.0.0.1:3000;
        proxy_redirect off;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.