Blog

Blog

PHODAL

Nginx WebSocket 配置

在开发http://mqtt.phodal.com/使用Socket.io的时候,本地是没什么问题,但是发现到服务器上后就不行了,看了看当前版本的Nginx是支持的,网上搜索了下看到了方案。

Nginx Websocket

在配置中添加下面的内容:

# WebSocket support (nginx 1.4)
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";

最后我的配置如下所示:

server {

    listen 80;
    server_name mqtt.phodal.com;
    add_header Cache-Control "max-age=600";
    location / {
        etag on;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Scheme $scheme;
        proxy_connect_timeout 10;
        proxy_redirect off;
        proxy_pass http://127.0.0.1:3000;

# WebSocket support (nginx 1.4)
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}

Nginx 1.7.9

顺便又更新了一下NGINX,有强迫症真伤不起。

配置记录一下:

./configure --user=www --group=www --add-module=../nginx_tcp_proxy_module --add-module=../ngx_pagespeed-1.9.32.3-beta --prefix=/usr/local/nginx --with-pcre --with-http_spdy_module --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-mail --with-mail_ssl_module --with-ipv6

关于我

Github: @phodal     微博:@phodal     知乎:@phodal    

微信公众号(Phodal)

围观我的Github Idea墙, 也许,你会遇到心仪的项目

QQ技术交流群: 321689806
comment

Feeds

RSS / Atom

最近文章

关于作者

Phodal Huang

Engineer, Consultant, Writer, Designer

ThoughtWorks 技术专家

工程师 / 咨询师 / 作家 / 设计学徒

开源深度爱好者

出版有《前端架构:从入门到微前端》、《自己动手设计物联网》、《全栈应用开发:精益实践》

联系我: h@phodal.com

微信公众号: 最新技术分享

标签