0

这是我的 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;
    }
}

基于代码的应用程序

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

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

当我尝试include proxy_params;在 nginx 配置中使用行时,我得到 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

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

4

1 回答 1

2

这在tomcat中对我有用。root如果您更改、error_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;
}
于 2018-03-07T07:40:11.203 回答