nginx代理websocket的wss和项目的https

第一步:准备工作。

  • 服务器绑定的域名: www.xxxxxx.com
  • SSL证书文件:
    1_www.xxxxxx.com_bundle.crt; (证书文件名称)
    2_www.xxxxxx.com.key; (私钥文件名称)

第二步:下载nginx,解压、安装。

(1)官网地址:http://nginx.org/en/download.html 下载nginx ,如图:
在这里插入图片描述
(2)下载得到压缩包 :nginx-1.16.0(我下载的是这个版本),解压后目录如图:
在这里插入图片描述
(3)点击nginx.exe,发现一闪而过的窗口,在任务管理器里查看是否有两个进程,如果有,说明运行成功。如图:
在这里插入图片描述

第三步:配置nginx代理转发

(1)前提条件:

  • 已在 SSL 证书管理控制台 中下载并解压缩 www.domain.com 证书文件包到本地目录。
  • 解压缩后,可获得 Nginx 文件夹和 CSR 文件: 文件夹名称:Nginx 文件夹内容:
    1_www.domain.com_bundle.crt 证书文件
    2_www.domain.com.key 私钥文件
    CSR 文件内容:www.domain.com.csr 文件。
  • 已在当前服务器中安装配置 Nginx 服务器。

(2)把证书文件(1_www.domain.com_bundle.crt 证书文件 ,2_www.domain.com.key 私钥文件 )放入nginx里的conf文件夹里,如图:
在这里插入图片描述
(3)打开 nginx.conf 配置文件,配置代理内容,我这里配置的是监听443端口,转发到Tomcat的8080端口和转发到websocket的4909端口。如下:

#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;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout 75;

    #gzip  on;

    server {
        listen 443 ssl;
     	server_name www.xxxxxxxx.com;
        ssl_certificate 1_www.xxxxxxxx.com_bundle.crt;
        ssl_certificate_key 2_www.xxxxxxxx.com.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;
        location /{
		proxy_pass http://localhost:8080;
    		proxy_http_version 1.1;
    		proxy_set_header Upgrade $http_upgrade;
    		proxy_set_header Connection "Upgrade";
    		proxy_set_header X-Real-IP $remote_addr;
        }

	
        location /wss{
		proxy_pass http://localhost:4909;
    		proxy_http_version 1.1;
    		proxy_set_header Upgrade $http_upgrade;
    		proxy_set_header Connection "Upgrade";
    		proxy_set_header X-Real-IP $remote_addr;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

主要修改内容如图:
在这里插入图片描述
(4)重启nginx。
运行cmd,进入命令行界面,进入nginx的文件里,运行重启命令(.\nginx -s reload)。
开启命令:.\nginx
重启命令:.\nginx -s reload
判断配置文件是否正确命令:.\nginx -t

第四步:验证是否配置成功

(1)https访问项目:https://www.xxxxxxxxx.com/项目名。
(2)wss访问websocket服务:

wx.connectSocket({
	url: 'wss://www.xxxxxxxxxxx.com/wss',
})

第五步:报错解决

(1)nginx报错:bind() to 0.0.0.0:443 failed (10013: An attempt was made to access a socket in a way forbidden by it。
原因:其他进程占用了nginx 的端口
解决:运行 cmd, 输入netstat -aon|findstr “443”,找到 0.0.0.0:443,找到 PID,在任务管理器结束进程。

第六步:HTTP 自动跳转 HTTPS 的安全配置(补充)

server {
	listen 80;
	server_name www.domain.com; #填写绑定证书的域名
	rewrite ^(.*)$ https://$host$1 permanent; #把http的域名请求转成https
}
  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

吟诗作对歌一曲

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值