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

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

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.

  1. location /ws/ {
  2. proxy_pass http://backend;
  3. proxy_http_version 1.1;
  4. proxy_set_header Upgrade $http_upgrade;
  5. proxy_set_header Connection "upgrade";
  6. }

default.conf - Full config file

  1. upstream frontend {
  2. server frontend:3000;
  3. }
  4. upstream backend {
  5. server backend:8000;
  6. }
  7. server {
  8. listen 80;
  9. listen [::]:80;
  10. server_name ${DOMAIN} www.${DOMAIN};
  11. location /.well-known/acme-challenge/ {
  12. root /vol/www/;
  13. }
  14. location / {
  15. return 301 https://${DOMAIN}$request_uri;
  16. }
  17. }
  18. server {
  19. listen [::]:443 ssl ipv6only=on;
  20. listen 443 ssl;
  21. server_name ${DOMAIN} www.${DOMAIN};
  22. ssl_certificate /etc/letsencrypt/live/${DOMAIN}/fullchain.pem;
  23. ssl_certificate_key /etc/letsencrypt/live/${DOMAIN}/privkey.pem;
  24. include /etc/nginx/options-ssl-nginx.conf;
  25. ssl_dhparam /vol/proxy/ssl-dhparams.pem;
  26. add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
  27. location /api/ {
  28. proxy_pass http://backend;
  29. proxy_redirect off;
  30. }
  31. location /ws/ {
  32. proxy_pass http://backend;
  33. proxy_http_version 1.1;
  34. proxy_set_header Upgrade $http_upgrade;
  35. proxy_set_header Connection "upgrade";
  36. }
  37. location / {
  38. proxy_pass http://frontend;
  39. proxy_redirect off;
  40. client_max_body_size 10M;
  41. }
  42. error_page 500 502 503 504 /50x.html;
  43. location = /50x.html {
  44. root /usr/share/nginx/html;
  45. }
  46. }

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.

  1. location /ws/ {
  2. proxy_pass http://backend;
  3. proxy_http_version 1.1;
  4. proxy_set_header Upgrade $http_upgrade;
  5. proxy_set_header Connection &quot;upgrade&quot;;
  6. }

default.conf - Full config file

  1. upstream frontend {
  2. server frontend:3000;
  3. }
  4. upstream backend {
  5. server backend:8000;
  6. }
  7. server {
  8. listen 80;
  9. listen [::]:80;
  10. server_name ${DOMAIN} www.${DOMAIN};
  11. location /.well-known/acme-challenge/ {
  12. root /vol/www/;
  13. }
  14. location / {
  15. return 301 https://${DOMAIN}$request_uri;
  16. }
  17. }
  18. server {
  19. listen [::]:443 ssl ipv6only=on;
  20. listen 443 ssl;
  21. server_name ${DOMAIN} www.${DOMAIN};
  22. ssl_certificate /etc/letsencrypt/live/${DOMAIN}/fullchain.pem;
  23. ssl_certificate_key /etc/letsencrypt/live/${DOMAIN}/privkey.pem;
  24. include /etc/nginx/options-ssl-nginx.conf;
  25. ssl_dhparam /vol/proxy/ssl-dhparams.pem;
  26. add_header Strict-Transport-Security &quot;max-age=31536000; includeSubDomains&quot; always;
  27. location /api/ {
  28. proxy_pass http://backend;
  29. proxy_redirect off;
  30. }
  31. location /ws/ {
  32. proxy_pass http://backend;
  33. proxy_http_version 1.1;
  34. proxy_set_header Upgrade $http_upgrade;
  35. proxy_set_header Connection &quot;upgrade&quot;;
  36. }
  37. location / {
  38. proxy_pass http://frontend;
  39. proxy_redirect off;
  40. client_max_body_size 10M;
  41. }
  42. error_page 500 502 503 504 /50x.html;
  43. location = /50x.html {
  44. root /usr/share/nginx/html;
  45. }
  46. }

Any idea what I should change to make it work?

答案1

得分: 0

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

proxy_params

  1. proxy_set_header Upgrade $http_upgrade;
  2. proxy_set_header Connection "upgrade";

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

这是我的WebSocket配置

default.conf - WebSocket配置

  1. location /ws/ {
  2. proxy_pass http://backend;
  3. proxy_http_version 1.1;
  4. include /etc/nginx/proxy_params;
  5. }
英文:

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

  1. proxy_set_header Upgrade $http_upgrade;
  2. 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

  1. location /ws/ {
  2. proxy_pass http://backend;
  3. proxy_http_version 1.1;
  4. include /etc/nginx/proxy_params;
  5. }

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:

确定