NGINX:配置websockets时,“proxy_set_header”中的参数数量无效

q35jwt9p  于 2023-06-21  发布在  Nginx
关注(0)|答案(1)|浏览(159)

我正在尝试为websockets配置nginx。但它返回一个错误。
[emerg] 8#8: invalid number of arguments in "proxy_set_header" directive in /etc/nginx/conf.d/default.conf:45 nginx: [emerg] directive in /etc/nginx/conf.d/default.conf:45
第45行出现错误,即:proxy_set_header Upgrade $http_upgrade;
我不知道为什么它不工作,我从nginx docs复制它。
寻找解决方案我尝试在$前加一个\,切换nginx版本。
没用

location /ws/ {
        proxy_pass http://backend;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }

default.conf -完整配置文件

upstream frontend {
    server frontend:3000;
}

upstream backend {
    server backend:8000;
}

server {
    listen 80;
    listen [::]:80;
    server_name ${DOMAIN} www.${DOMAIN};

    location /.well-known/acme-challenge/ {
        root /vol/www/;
    }

    location / {
        return 301 https://${DOMAIN}$request_uri;
    }
}

server {
    listen [::]:443 ssl ipv6only=on;
    listen 443 ssl;
    server_name ${DOMAIN} www.${DOMAIN};

    ssl_certificate     /etc/letsencrypt/live/${DOMAIN}/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/${DOMAIN}/privkey.pem;

    include     /etc/nginx/options-ssl-nginx.conf;

    ssl_dhparam /vol/proxy/ssl-dhparams.pem;

    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;

    location /api/ {
        proxy_pass http://backend;
        proxy_redirect off;
    }

    location /ws/ {
        proxy_pass http://backend;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }

    location / {
        proxy_pass           http://frontend;
        proxy_redirect       off;
        client_max_body_size 10M;
    }

    error_page 500 502 503 504 /50x.html;

    location = /50x.html {
        root /usr/share/nginx/html;
    }
}

你知道我该怎么做才能让它正常工作吗?

kq4fsx7k

kq4fsx7k1#

我想我可以把proxy_set_header放到另一个文件中,然后使用include。
我创建了一个名为proxy_params的文件,其中包含以下内容

proxy_params

proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";

然后在配置文件中使用include /etc/nginx/proxy_params;
这就是我的websockets配置的样子

default. conf-websocket config

location /ws/ {
    proxy_pass http://backend;
    proxy_http_version 1.1;
    include /etc/nginx/proxy_params;
}

相关问题