【问题标题】:POST Error when running multiple SocketIO endpoints behind nginx在 nginx 后面运行多个 SocketIO 端点时出现 POST 错误
【发布时间】:2018-08-09 12:22:15
【问题描述】:

我正在尝试在 nginx 代理后面运行多个 socketio 端点。我的第一个套接字站点位于端点'/'。这个工作正常。第二个 socketio 端点位于“/red/”。这似乎有效。我的 html 中的 socketio 客户端

    var socket = io.connect('http://www.vagrantdevhost.com:8080', {path:'/red/'});
    socket.on('connect', function() {
        socket.emit('connectevent', {data: 'I\'m connected!'});
    });

似乎连接正确。我的连接事件被调用。但是,我在控制台中收到以下错误。

POST http://www.vagrantdevhost.com:8080/red/?EIO=3&transport=polling&t=MKUdom- 405(方法不允许)index.js:83

有什么解释吗?我在第三天,希望有任何建议。我已经包含了我的 nginx 配置。

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {

        listen       8080 default_server;
        listen       [::]:8080 default_server;
        server_name _;
        root         /usr/share/nginx/html;

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

    location /socket.io {
        proxy_pass http://127.0.0.1:8092/socket.io;
        proxy_redirect off;
        proxy_buffering off;

        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
    }

    location / {
        proxy_pass http://127.0.0.1:8092;
        proxy_redirect off;

        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    location /red/ {
        proxy_pass http://127.0.0.1:8093/;

        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    } 

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }


    }

}

【问题讨论】:

    标签: node.js nginx websocket socket.io


    【解决方案1】:

    通常这些错误消息是由 Nginx 无法在 POST 请求上提供静态内容的限制引起的。因此,这篇文章中有一个技巧,您可以将 405 重定向到 200。

    在这里查看:POST request not allowed - 405 Not Allowed - nginx, even with headers included

    server {
        listen       80;
        server_name  localhost;
    
        location / {
            root   html;
            index  index.html index.htm;
        }
    
        error_page  404     /404.html;
        error_page  403     /403.html;
    
        # To allow POST on static pages
        error_page  405     =200 $uri;
    
        # ...
    }
    

    【讨论】:

      猜你喜欢
      • 2021-12-06
      • 2012-05-08
      • 2013-10-30
      • 2020-04-03
      • 2011-04-17
      • 2021-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多