英文:
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 "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?
答案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 "upgrade";
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;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论