nginx 代理http/https之后,websocket请求失败

需要转发Header头 Upgrade Connection

修改之前的配置:

upstream xx {
        server 127.0.0.1:9090;
}

server {
        listen 80;

        listen 443;
        listen [::]:443 ssl;
        server_name xx.tinyfeng.com;
        client_max_body_size 500m;

        try_files $uri  @xx;

        location @xx {
                proxy_set_header Host $host;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto https;
                proxy_redirect off;
                proxy_pass http://xx;
        }
}

修改以后:

upstream xx {
        server 127.0.0.1:9090;
}

map $http_upgrade $connection_upgrade {
        default upgrade;
        '' close;
}

server {
        listen 80;

        listen 443;
        listen [::]:443 ssl;
        server_name xx.tinyfeng.com;
        client_max_body_size 500m;

        try_files $uri  @xx;

        location @xx {
                proxy_set_header Host $host;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header  X-Forwarded-Proto $scheme;
                # proxy_set_header X-Forwarded-Proto https;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection $connection_upgrade;
                proxy_redirect off;
                proxy_pass http://xx;
        }
}

发表于 2023.10.26