小程序模板網(wǎng)

nginx簡單配置同時(shí)支持微信小程序https/wss協(xié)議

發(fā)布時(shí)間:2018-05-08 14:19 所屬欄目:小程序開發(fā)教程

微信小程序需要使用https與wss能才進(jìn)行連接,雖然開發(fā)模式下可以使用http與ws,但發(fā)布的時(shí)候還是需要安全協(xié)議,網(wǎng)上的各種配置是超多超復(fù)雜的,這里已經(jīng)對(duì)nginx指定版本進(jìn)行最簡單的配置,可用。 

 

使用教程

nginx版本

 

				
  1. $ nginx -v
  2. nginx version: nginx/1.12.2

系統(tǒng)Centos7

 

				
  1. $ uname -r
  2. 4.14.11-1.el7.elrepo.x86_64

cat /etc/nginx/conf.d/test.conf

 

				
  1. server {
  2. listen 80;
  3. server_name test.dounine.com;
  4. return 301 https://$host$request_uri;
  5. }
  6.  
  7. server {
  8. listen 443;
  9. server_name test.dounine.com;
  10. ssl on;
  11. ssl_certificate /etc/nginx/ssls/test.xxxx.pem;
  12. ssl_certificate_key /etc/nginx/ssls/test.xxxx.key;
  13.  
  14. location / {
  15. client_max_body_size 100m;
  16. proxy_pass http://localhost:7777;
  17. proxy_set_header Host $host;
  18. proxy_set_header X-Real-IP $remote_addr;
  19. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  20. proxy_set_header Upgrade $http_upgrade;
  21. proxy_set_header Connection "Upgrade";
  22. }
  23.  
  24. }

微信小程序代碼

 

				
  1. wx.connectSocket({
  2. url: 'wss://test.dounine.com/ws'
  3. });
  4. wx.onSocketOpen(function(res) {
  5. console.info('websocket連接成功');
  6. });
  7. wx.onSocketClose(function(res) {
  8. console.log('WebSocket 已關(guān)閉!')
  9. });
  10. wx.onSocketError(function(res){
  11. console.log('WebSocket連接打開失敗,請(qǐng)檢查!')
  12. });
  13. wx.onSocketMessage(function(res) {
  14. console.log('收到服務(wù)器內(nèi)容:' + res.data)
  15. })


易優(yōu)小程序(企業(yè)版)+靈活api+前后代碼開源 碼云倉庫:starfork
本文地址:http://m.u-renovate.com/wxmini/doc/course/24339.html 復(fù)制鏈接 如需定制請(qǐng)聯(lián)系易優(yōu)客服咨詢:800182392 點(diǎn)擊咨詢
QQ在線咨詢
AI智能客服 ×