在nginx多位置块中代理传递WebSocket

3phpmpom  于 2023-11-17  发布在  Nginx
关注(0)|答案(1)|浏览(137)

我有3个服务在我的机器上运行。我想用nginx管理它们:service-1运行在端口6000上,service-2运行在端口6002上,service-3是一个运行在端口6003上的WebSocket。
在我的nginx.conf文件中:

server {
    listen [::]:80;  # Listen on IPv6
    listen 80;
    server_name domain.com; 

    location /service-1/ {
        proxy_pass http://localhost:6001;
    }

    location /service-2/ {
        proxy_pass http://localhost:6002;
    }

    location /service-3/ {
        proxy_pass http://localhost:65000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
    }

}

字符串
问题是,如果我使用“/”作为WebSocket代理传递,那么连接就建立了。但是使用不同的位置块并不能建立连接。
举例来说:

###### THIS WORKS ######

server {
    listen [::]:80;  # Listen on IPv6
    listen 80;
    server_name domain.com; 

    location / {
        proxy_pass http://localhost:65000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
    }

}

###### THIS DOESN'T WORK ######

server {
    listen [::]:80;  # Listen on IPv6
    listen 80;
    server_name domain.com; 

    location /service-3/ {
        proxy_pass http://localhost:65000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
    }

}


注意:我正在使用Cloudflare CDN来管理我的域和子域。

wtlkbnrh

wtlkbnrh1#

我认为问题是你错过了代理部分的请求部分。

location /service-3/ {
    proxy_pass http://localhost:65000;

字符串
应该看起来像

location /service-3/ {
    proxy_pass http://localhost:65000/service-3/;

相关问题