美文网首页
nginx配置https转http&wss转ws

nginx配置https转http&wss转ws

作者: Nick_4438 | 来源:发表于2019-10-30 17:40 被阅读0次

    前言

    安装软件

    • 下载
    yum -y install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel
    wget http://nginx.org/download/nginx-1.17.5.tar.gz
    tar -xzvf nginx-1.17.5.tar.gz
    cd nginx-1.17.5
    
    • 配置nginx
    ./configure --prefix=/usr/local/nginx --sbin-path=/usr/bin/nginx --with-http_ssl_module --with-http_realip_module --with-http_sub_module --with-stream --with-http_stub_status_module
    

    ./configure --help 可以查看配置项说明
    完成之后的配置文件见:/usr/local/nginx/conf/nginx.conf,参数配置说明:https://nginx.org/en/docs/configure.html

    • 编译
    make && make install
    
    • 修改配置文件
    #user  nobody;
    worker_processes  1;
    
    #error_log  logs/error.log;
    #error_log  logs/error.log  notice;
    #error_log  logs/error.log  info;
    
    #pid        logs/nginx.pid;
    
    events {
        worker_connections  1024;
    }
    
    
    http {
        include       mime.types;
        default_type  application/octet-stream;
    
        sendfile        on;
        keepalive_timeout  65;
    
        server{
            listen 80;
            server_name domain.com;
            #告诉浏览器有效期内只准用 https 访问
            add_header Strict-Transport-Security max-age=15768000;
            #永久重定向到 https 站点
            return 301 https://$server_name$request_uri;
        }
    
        server {
            listen       443 ssl;
            server_name  domain.com;
            # 需要准备好证书
            ssl_certificate /usr/local/nginx/conf/cr/server.pem;
            ssl_certificate_key /usr/local/nginx/conf/cr/server.key;
            
    
            #ssl_session_cache    shared:SSL:1m;
            ssl_session_timeout  5m;
            #协议配置
            ssl_protocols TLSv1 TLSv1.1 TLSv1.2;         
            ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
             # 转发到http
            location / {
               proxy_pass http://domain.com;
            }
        }
       # wss -> ws
        server {
            listen       82 ssl;
            server_name  domain.com;
            # 需要准备好证书
            ssl_certificate /usr/local/nginx/conf/cr/server.pem;
            ssl_certificate_key /usr/local/nginx/conf/cr/server.key;
    
            #ssl_session_cache    shared:SSL:1m;
            # ssl_session_timeout  5m;
            #协议配置
            # ssl_protocols TLSv1 TLSv1.1 TLSv1.2;  
            ssl_protocols SSLv3 SSLv2 TLSv1 TLSv1.1 TLSv1.2;
            ssl_session_cache shared:SSL:1m;
            ssl_session_timeout  10m;
            ssl_prefer_server_ciphers on;
            # ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
             # 转发到http
            location / {
               # 转发ws地址
               proxy_pass http://10.1.1.23:82;
               proxy_http_version 1.1;
               proxy_set_header Upgrade $http_upgrade;
               #由于服务器端源码(建议大家做好大小写匹配)只匹配了"Upgrade"字符串,所以如果这里填"upgrade"服务器端会将这条http请求当成普通的请求,导致websocket握手失败
               proxy_set_header Connection "Upgrade";
               proxy_set_header Remote_addr $remote_addr;
               proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
               proxy_read_timeout 600s;
            }
        }
    }
    
    • 重启生效
    nginx -s reload
    

    相关文章

      网友评论

          本文标题:nginx配置https转http&wss转ws

          本文链接:https://www.haomeiwen.com/subject/xchavctx.html