nginx websocket 代理

來源:互聯網
上載者:User

標籤:nginx websocket 代理


websocket線上測試:http://www.blue-zero.com/WebSocket/,可以使用這個工具測試:


需求:大資料項目開開放2個連接埠8080和7398,一個走http協議,一個走tcp,也就是訪問socket。當client訪問這個項目socket會把client的資訊拋出到web頁面。現在要用nginx做代理,把這2個連接埠映射到內網,提供內網訪問。



nginx代理http協議當然很好解決,

server {

    listen 80;

    server_name  z.exam.com;


    access_log  /var/log/nginx/rdp.elenet.me_access.log main;

    error_log /var/log/nginx/rdp.elenet.me_error.log;


    location / {

        proxy_http_version 1.1;

        proxy_set_header Connection "";

        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_next_upstream http_502 http_504 http_404 http_503 error timeout invalid_header;

        proxy_pass http://11.1.250.241:8080;

    }


    error_page 404 /404.html;

    location = /40x.html {

    }

    error_page 500 502 503 504 /50x.html;

     location = /50x.html {

     }

}


tcp連接埠的代理用nginx,上面這段肯定是不行的。nginx官網參考文檔:http://nginx.org/en/docs/http/websocket.html


其實只需要在nginx的http塊和server分別加幾行就可以了:

http塊加以下幾行:


map $http_upgrade $connection_upgrade {

        default upgrade;

        ‘‘ close;

    }


server塊加以下2行:


proxy_set_header Upgrade $http_upgrade;

proxy_set_header Connection $connection_upgrade;



配置如下,http塊檔案較大,就不展示了:

server {

    listen 7398;

    server_name  z.exam.com;


    access_log  /var/log/nginx/rdp.elenet.me_access.log main;

    error_log /var/log/nginx/rdp.elenet.me_error.log;


    location / {

        proxy_http_version 1.1;

        proxy_set_header Connection "";

        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_next_upstream http_502 http_504 http_404 http_503 error timeout invalid_header;

        proxy_set_header Upgrade $http_upgrade;

        proxy_set_header Connection "upgrade";

        proxy_pass http://11.1.250.241:7398;

    }


    error_page 404 /404.html;

    location = /40x.html {

    }

    error_page 500 502 503 504 /50x.html;

     location = /50x.html {

     }

}


以上配置可以貼在同一個conf設定檔中。


本文出自 “LINUX Super夢” 部落格,請務必保留此出處http://215687833.blog.51cto.com/6724358/1854040

nginx websocket 代理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.