【问题标题】:Can't use nginx as websocket proxy不能使用 nginx 作为 websocket 代理
【发布时间】:2018-03-06 20:29:57
【问题描述】:

这是我的 nginx 配置

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

基于this代码的应用

因此,我无法通过代码从客户端页面连接到 websocket

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

当我尝试在 nginx 配置中使用 include proxy_params; 行时,我得到 2018/03/06 19:52:06 [emerg] 1#1: open() "/etc/nginx/proxy_params" failed (2: No这样的文件或目录)在/etc/nginx/conf.d/default.conf:6

我错在哪里以及如何允许 nginx 检索 websocket 连接?

【问题讨论】:

    标签: nginx websocket nginx-reverse-proxy python-socketio


    【解决方案1】:

    这在tomcat中对我有用。如果您更改 rooterror_logproxy_pass 并配置您自己的超时,则应该可以与其他 Web 应用程序一起使用。

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

    也将此添加到我的 http 配置中

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

    【讨论】:

      猜你喜欢
      • 2016-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-25
      • 1970-01-01
      相关资源
      最近更新 更多