Can't use nginx as websocket proxy

烈酒焚心 提交于 2019-12-11 14:53:39

问题


Here is my nginx config

server {
    listen 80;
    server_name _;

    location / {
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_pass http://flask:8001;
    }

    location /socket.io {

        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_buffering off;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_pass http://flask:8001/socket.io;
    }
}

for app based on this code

As a result, I can't connect to websocket from my client page by code

var socket = io.connect(location.protocol + '//' + document.domain + ':' + (location.port || 80) + namespace)

When I try to use include proxy_params; line in nginx config I get 2018/03/06 19:52:06 [emerg] 1#1: open() "/etc/nginx/proxy_params" failed (2: No such file or directory) in /etc/nginx/conf.d/default.conf:6

Where I'm wrong and how to allow nginx to retrieve websocket connections?


回答1:


This worked for me in tomcat. should work with other web applications if you change root, error_log, and proxy_pass and config your own time outs.

server {
    listen       80;
    listen       443 ssl;
    server_name  x.y.com;
    root         /opt/tomcat/webapps/;

    error_log    /var/logs/nginx/x.y.com.log debug;

    ssl_certificate         /root/program/ssl/certificate.pem;
    ssl_certificate_key     /root/program/ssl/certificate.key;

    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;

    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $http_host;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
        proxy_set_header Connection "";
        proxy_connect_timeout  4000s;
        proxy_read_timeout     4000s;
        #proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        #proxy_set_header X-Forwarded-Proto $proxy_x_forwarded_proto;
        proxy_pass http://127.0.0.1:8080/;
    }

Also added this to my http config

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


来源:https://stackoverflow.com/questions/49139470/cant-use-nginx-as-websocket-proxy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!