nginx部署ws为wss,以及获取真实IP

ws是websocket的统一资源标识符,wss对于ws相当于https对于http,今天把ws部署到服务器的时候提示错误了:

Mixed Content: The page at 'https://domain.com/' was loaded over HTTPS, but attempted to connect to the insecure WebSocket endpoint 'ws://x.x.x.x:xxxx/'. This request has been blocked; this endpoint must be available over WSS.

服务器使用的是nginx转发80到443上,在443端口绑定的ssl证书。

那么这里使用websocket的服务也肯定要由ws改成wss。所以在前端修改一下:

        if ("WebSocket" in window) {
          let wsClient = new WebSocket("wss://www.域名.com");

后端也需要做相应的修改,修改nginx/conf中的nginx.conf文件:

    # HTTPS server
    #
    server {
        listen       443 ssl;
        server_name  www.域名.com/;

        ssl_certificate      1_www.域名.com_bundle.crt;
        ssl_certificate_key  2_www.域名.com.key;

#这是新添加的
         location /wss/  {
     proxy_pass http://127.0.0.1:3000/;        #通过配置端口指向部署websocket的项目
     proxy_http_version 1.1;    
     proxy_set_header Upgrade $http_upgrade;
     proxy_set_header Connection "Upgrade";
     proxy_set_header X-real-ip $remote_addr;
     proxy_set_header X-Forwarded-For $remote_addr;
     }
#这上面是新添加的

        location / {
             proxy_pass http://127.0.0.1:3000;
             proxy_http_version 1.1;
             proxy_set_header Upgrade $http_upgrade;
             proxy_set_header Connection "upgrade";
        }
    }

在centos中输入pm2 restart all,重启node网站。ok。

输入redis-cli,进入redis,输入keys * 打印所有的key也显示正常。

WebSocket proxying

使用了nginx代理转发wss之后,获取的IP是127.0.0.1。

获取IP的方法是:

    wss.on('connection', function connection(ws, req) {
        // console.log(req);
        const ip = req.connection.remoteAddress;

打印的req为:

这里继续修改nginx的conf为:

         location /wss/  {
     proxy_pass http://127.0.0.1:3000/;        #通过配置端口指向部署ws的项目
     proxy_http_version 1.1;    
     proxy_set_header Upgrade $http_upgrade;
     proxy_set_header Connection "Upgrade";
     proxy_set_header X-Real-IP $remote_addr;  
     proxy_set_header Host $host;  
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      }

修改后重新打印输出为:

这里便有了新添加的几项,里面有我们真实的IP地址,便可以获取到IP了。

How to get the IP address of the client?

When the server runs behind a proxy like NGINX, the de-facto standard is to use the X-Forwarded-For header.

wss.on('connection', function connection(ws, req) {
  const ip = req.headers['x-forwarded-for'].split(/\s*,\s*/)[0];
});

X-Forwarded-For

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Nginx中配置WSS,你可以使用以下配置: ``` server { listen 443 ssl; server_name im-wss-api.xxx.xxx; ssl_certificate ../cert/server.crt; ssl_certificate_key ../cert/server.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:8088; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "Upgrade"; } } ``` 这个配置中,使用了监听443端口,并使用SSL证书进行加密通信。同时,配置了代理转发到本地的8088端口,以实现WSS功能。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [【高效运维】Nginx下配置WSWSS](https://blog.csdn.net/u012069313/article/details/124562355)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [Nginx配置WSWSS域名](https://blog.csdn.net/cljdsc/article/details/128056559)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [系统基于springboot框架,使用Java+vue编写,为前后端分离的微服务项目](https://download.csdn.net/download/Abelon/88250447)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值