nodejs+socket.io用nginx反向代理提示400 Bad Request

nginx 1.3.12以后的版本才支持websocket,所以请先更新nginx。我现在用的是1.6.0版本。

server {
     listen 80;
     server_name 111cn.net  www.111cn.net ;
     root /www/web/123/public_html;
     index index.html index.php index.htm;
     location / {
          proxy_pass http://127.0.0.1:8000;
          ......
          proxy_http_version 1.1;
          proxy_set_header Upgrade $http_upgrade;
          proxy_set_header Connection "upgrade";
     }
}

红色部分为最关键的内容,在你的.conf文件里加上这三行就可以了。

下面是英文原文:

WebSocket proxying

To turn a connection between a client and server from HTTP/1.1 into WebSocket, the protocol switch mechanism available in HTTP/1.1 is used.

There is one subtlety however: since the “Upgrade” is a hop-by-hop header, it is not passed from a client to proxied server. With forward proxying, clients may use the CONNECT method to circumvent this issue. This does not work with reverse proxying however, since clients are not aware of any proxy servers, and special processing on a proxy server is required.

Since version 1.3.13, nginx implements special mode of operation that allows setting up a tunnel between a client and proxied server if the proxied server returned a response with the code 101 (Switching Protocols), and the client asked for a protocol switch via the “Upgrade” header in a request.

As noted above, hop-by-hop headers including “Upgrade” and “Connection” are not passed from a client to proxied server, therefore in order for the proxied server to know about the client’s intention to switch a protocol to WebSocket, these headers have to be passed explicitly:

location /chat/ {
    proxy_pass http://backend;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
}

A more sophisticated example in which a value of the “Connection” header field in a request to the proxied server depends on the presence of the “Upgrade” field in the client request header:

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

    server {
        ...

        location /chat/ {
            proxy_pass http://backend;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;
        }
    }

By default, the connection will be closed if the proxied server does not transmit any data within 60 seconds. This timeout can be increased with the proxy_read_timeout directive. Alternatively, the proxied server can be configured to periodically send WebSocket ping frames to reset the timeout and check if the connection is still alive.

时间: 2024-08-04 12:42:51

nodejs+socket.io用nginx反向代理提示400 Bad Request的相关文章

使用Nginx反向代理实现简单的负载均衡

Nginx反向代理的原理优点: 反向代理可以简单的理解为:代理服务器来接收internet上的服务器请求,然后将请求转发给内部的服务器上,然后将结果返回给internet上请求的客户端,所以代理服务器对外表现出来的只是一台服务器.反向代理服务器也称为Web服务器加速,针对web服务器提供加速功能.他作为代理服务器,并不针对浏览器用户,而是针对一台或者多台特定的web服务器.可以缓存web的页面,降低web服务器的访问量,从而来降低web服务器的负载,实施反向代理,只要将反向代理设备放置在一台或多

Nginx反向代理,负载均衡,redis session共享,keepalived高可用

相关知识自行搜索,直接上干货... 使用的资源: nginx主服务器一台,nginx备服务器一台,使用keepalived进行宕机切换. tomcat服务器两台,由nginx进行反向代理和负载均衡,此处可搭建服务器集群. redis服务器一台,用于session的分离共享. nginx主服务器:192.168.50.133 nginx备服务器:192.168.50.135 tomcat项目服务器1:192.168.50.137 tomcat项目服务器2:192.168.50.139 redis服

Nginx 反向代理可以缓存 HTTP POST 请求页面吗?

Nginx 反向代理可以缓存 HTTP POST 请求页面吗?  2017-09-05 景峯 Netkiller 本文节选自<Netkiller Web 手札> 作者:netkiller 网站:http://www.netkiller.cn 答案是可以!  因为nginx 使用 url 作为缓存的key ( Nginx 将url地址 md5后作为缓存的 key ),所以默认情况下 Nginx 只能处理 HTTP GET 缓存. 对于 HTTP POST 请求,提交数据放在HTTP Head 头

基于CentOS 7配置Nginx反向代理

Nginx作为反向代理服务器被广泛使用在各大互联网企业.它简单易用,可以根据业务的需求将其不同的业务类型代理至不同的服务器,将整个站点请求压力按类型分摊到不同的服务器.该方式使的整个站点请求性能得以极大的提升.本文简要描述了Nginx几种不同情形的代理演示,供大家参考. 一.反向代理及演示环境描述 1.反向代理 在计算机网络中,反向代理是一种代理服务器,代表客户端从一个或多个服务器检索资源.然后将这些资源返回给客户机,就像它们源自Web服务器本身一样.与正向代理相反,正向代理是与其关联的客户端联

Nginx 反向代理 如何在web应用中获取用户ip

转载:http://blog.csdn.net/bao19901210/article/details/52537279 问题背景: 在实际应用中,我们可能需要获取用户的ip地址,比如做异地登陆的判断,或者统计ip访问次数等,通常情况下我们使用request.getRemoteAddr()就可以获取到客户端ip,但是当我们使用了nginx作为反向代理后,使用request.getRemoteAddr()获取到的就一直是nginx服务器的ip的地址,那这时应该怎么办? part1:解决方案 我在查

Nginx反向代理以及缓存

链接 关于缓存 环境说明 环境搭建 测试 upstream keepalive 健康检查 参考资料 1 链接 个人博客: alex-my.xyz CSDN: blog.csdn.net/alex_my 2 关于缓存 这里使用proxy_cache来实现缓存. 关于fastcgi_cache请看: http://alex-my.xyz/web/Nginx反向代理缓存 Nginx反向代理缓存 proxy_cache配置和fastcgi_cache配置差不多, 所以本文并没有写. 使用proxy_ca

【整理】自动的 Nginx 反向代理配置

本文内容参考自< 自动的 Nginx 反向代理配置 >.  个人觉得:名字翻译成<自动化 Nginx 反向代理配置>更为贴切.  [一句话总结] 介绍了如何构建一个能够自动化配置 nginx 反向代理的方式.即将后端服务的部署与前端 nginx 的配置更改进行解耦.  [知识点]  对于 API 的使用者来说所有操作都是在同一个 URL 空间里进行的,而实际上是根据 URL 中不同的顶级"段"来进行路由的. 自动化配置的流程:当增加处理新"段"

虚拟机-如何将java Web项目设置 nginx反向代理

问题描述 如何将java Web项目设置 nginx反向代理 操作系统 : cent Os 7 虚拟机上用tomcat跑了一个web项目,端口8001 虚拟机IP:192.168.168.144 在主机上可以通过 192.168.168.144:8001/web访问该项目. 请教如何配置nginx , 可以实现在 主机上 通过 www.mywebtest.com访问到该web项目? 呃,自己搞了半天.nginx都启不起来.. 配置文件如下: user nginx; worker_processe

tomcat集群-nginx 反向代理三个本地tomcat 负载均衡配置 无法加载js css 图片等 也不报错

问题描述 nginx 反向代理三个本地tomcat 负载均衡配置 无法加载js css 图片等 也不报错 打开速度非常慢,到最后js css无法加载是什么问题 我的项目是ssh+jquery+easyui的 单独访问tomcat完全正常 访问nginx就这样了 .求大神指点啊 访问webapp根目录是可以的 我的页面都是在web-inf目录下的貌似不行 nginx配置如下: #user nobody; worker_processes 1; #error_log logs/error.log;