Page

[笔记]nginx代理https以及wss的相关配置

823Anson18-02-25


nginx代理https以及wss的相关配置

1、后端使用基于swoole的框架easyswoole创建的websocket服务器,前端使用nginx作为转发代理;


2、ssl证书使用腾讯云免费申请的证书;


3、配置参数:

server {
        listen 443;
        server_name 【域名地址】;
        index index.html index.htm index.php;

        ssl on;
        ssl_certificate 1_xxx_bundle.crt;   # crt证书路径
        ssl_certificate_key 2_xxx.key;      # key文件路径
        ssl_session_timeout 5m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; # 统一密码
        ssl_prefer_server_ciphers on;

        # https 配置项--begin
        location / {
             proxy_pass http://127.0.0.1:【https服务端口】;
             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;
             proxy_http_version 1.1;
             proxy_set_header Upgrade $http_upgrade;
             proxy_set_header Connection "upgrade";
        }
        # https 配置项--end
        
        
        # websocket ssl 配置项--begin
        location /websocket {
            proxy_pass http://127.0.0.1:【websocket服务端口】;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
        }
        # websocket 配置项--end
        
            access_log access.log;

}



4、浏览器端连接方式:

var ws = new WebSocket('wss://域名/websocket');


5、效果图:

blob.png

blob.png


4、配置只供参考,不做详细解读




来自ansion博客 

http://www.tp0.top

2018-02-25 10:53:20