小程序模板網(wǎng)

搭建微信小程序基本的https與wss環(huán)境

發(fā)布時(shí)間:2017-11-08 14:29 所屬欄目:小程序開發(fā)教程

年底了寫一篇小程序環(huán)境搭建的文章, 主要是怎么搭建一個(gè)線上環(huán)境以及怎么不改動原有http Api的情況

一、程序演示


  1. 弄個(gè)websocket遠(yuǎn)程播放音樂的案例

1、準(zhǔn)備工作


  1. 域名一個(gè)
  2. 免費(fèi)證書(推薦: 騰訊云、阿里云、便宜ssl 都是免費(fèi)的 配置好后先將證書下載下來)
  3. Centos服務(wù)器一臺
  4. nginx 1.10.2

2、 安裝nginx


  1. 安裝教程 http://www.runoob.com/linux/nginx-install-setup.html
  2. 注意安裝的時(shí)候 編譯 --with-stream --with-stream_ssl_module 兩個(gè)模塊
  3. 如果啟動nginx報(bào)錯看下圖解決

3、 配置nginx實(shí)現(xiàn)ssl反向代理


  1. 將下載好的證書根據(jù)自己的服務(wù)器選擇證書這里選擇nginx證書
  2. 主要用到server.crt以及server.key兩個(gè)證書上傳到服務(wù)器
  3. 這里我們直接上傳到nginx目錄的conf下了


修改nginx.conf(有注釋的地方改 其他的保持原樣就行了)

```
#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 65;
#gzip on;
server {
listen 8090; #這里將原來的80端口改成8090
server_name xxx.xxx.xxx; #這里就寫你自己的域名就行了
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#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;
# }
#}
include /usr/nginx/conf/wss.conf;# 這里我們將反向代理新建一個(gè)文件引入進(jìn)來
client_max_body_size 3m;# 上傳大小單位M 微信小程序上傳大圖片時(shí)可能需要設(shè)置
}


  1. 新建wss.conf


#主要是配置原來的ws 和 http 接口
upstream websocket {
server 10.5.11.xxx:8283;# 遠(yuǎn)程websocket服務(wù)器地址
}
upstream web{
server www.xxx.com;# 遠(yuǎn)程http接口
}
# 通過下面的反向代理到上面的接口去
server {
listen 443;#默認(rèn)https和wss協(xié)議端口
ssl on;
ssl_certificate /usr/nginx/conf/server.crt;#你的上傳到服務(wù)器的證書位置
ssl_certificate_key /usr/nginx/conf/server.key;#你的上傳到服務(wù)器的證書位置
ssl_session_timeout 5m;
ssl_session_cache shared:SSL:50m;
ssl_protocols SSLv3 SSLv2 TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
underscores_in_headers on;#開啟自定義頭信息的下劃線
#wss協(xié)議轉(zhuǎn)發(fā) 小程序里面要訪問的鏈接
location /wss {
proxy_pass http://websocket;#代理到上面的地址去
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}
#https協(xié)議轉(zhuǎn)發(fā) 小程序里面要訪問的鏈接
location /{
proxy_pass http://web;#代理到原有的http的地址去
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
add_header Access-Control-Allow-Origin *;#跨域訪問設(shè)置
}
}

```


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