NGINX: 在配置 Websockets 时 “proxy_set_header” 中的参数数量无效。

huangapple go评论114阅读模式
英文:

NGINX: invalid number of arguments in "proxy_set_header" while configuring websockets

问题

I am trying to configure nginx for websockets.
But it returns an error.

[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

Error occurs in line 45, that's:
proxy_set_header Upgrade $http_upgrade;

I have no idea why it doesn't work, I copied it from nginx docs.

Looking for solutions I tried adding a \ before the $, switching nginx version.
It didn't help.

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

default.conf - Full config file

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;
    }
}

Any idea what I should change to make it work?

英文:

I am trying to configure nginx for websockets.
But it returns an error.

[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

Error occurs in line 45, that's:
proxy_set_header Upgrade $http_upgrade;

I have no idea why it doesn't work, I copied it from nginx docs.

Looking for solutions I tried adding a \ before the $, switching nginx version. <br/>
It didn't help.

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

default.conf - Full config file

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 &quot;max-age=31536000; includeSubDomains&quot; 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 &quot;upgrade&quot;;
    }

    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;
    }
}

Any idea what I should change to make it work?

答案1

得分: 0

我认为我可以将proxy_set_header放入另一个文件中,然后使用include。<br/>
它有效。
我创建了一个名为proxy_params的文件,在其中我放置了以下内容

proxy_params

proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";

然后在配置文件中我使用了
include /etc/nginx/proxy_params;
<br/>

这是我的WebSocket配置

default.conf - WebSocket配置

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

I thought I could put proxy_set_header into another file, then use include. <br/>
It works.
I created a file called proxy_params, in which I put the following

proxy_params

proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection &quot;upgrade&quot;;

Then in the configuration file I used

include /etc/nginx/proxy_params;

<br/>

This is what my config for websockets looks like

default.conf - websocket config

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

huangapple
  • 本文由 发表于 2023年6月6日 02:57:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76409266.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定