搭建微信小程序基本的https与wss环境

原文地址:

http://www.wxappclub.com/topic/842

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

 
  
  1. ```
  2. #user nobody;
  3. worker_processes 1;
  4. #error_log logs/error.log;
  5. #error_log logs/error.log notice;
  6. #error_log logs/error.log info;
  7. #pid logs/nginx.pid;
  8. events {
  9. worker_connections 1024;
  10. }
  11. http {
  12. include mime.types;
  13. default_type application/octet-stream;
  14. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  15. # '$status $body_bytes_sent "$http_referer" '
  16. # '"$http_user_agent" "$http_x_forwarded_for"';
  17. #access_log logs/access.log main;
  18. sendfile on;
  19. #tcp_nopush on;
  20. #keepalive_timeout 0;
  21. keepalive_timeout 65;
  22. #gzip on;
  23. server {
  24. listen 8090; #这里将原来的80端口改成8090
  25. server_name xxx.xxx.xxx; #这里就写你自己的域名就行了
  26. #charset koi8-r;
  27. #access_log logs/host.access.log main;
  28. location / {
  29. root html;
  30. index index.html index.htm;
  31. }
  32. #error_page 404 /404.html;
  33. # redirect server error pages to the static page /50x.html
  34. #
  35. error_page 500 502 503 504 /50x.html;
  36. location = /50x.html {
  37. root html;
  38. }
  39. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  40. #
  41. #location ~ .php$ {
  42. # proxy_pass http://127.0.0.1;
  43. #}
  44. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  45. #
  46. #location ~ .php$ {
  47. # root html;
  48. # fastcgi_pass 127.0.0.1:9000;
  49. # fastcgi_index index.php;
  50. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  51. # include fastcgi_params;
  52. #}
  53. # deny access to .htaccess files, if Apache's document root
  54. # concurs with nginx's one
  55. #
  56. #location ~ /.ht {
  57. # deny all;
  58. #}
  59. }
  60. # another virtual host using mix of IP-, name-, and port-based configuration
  61. #
  62. #server {
  63. # listen 8000;
  64. # listen somename:8080;
  65. # server_name somename alias another.alias;
  66. # location / {
  67. # root html;
  68. # index index.html index.htm;
  69. # }
  70. #}
  71. # HTTPS server
  72. #
  73. #server {
  74. # listen 443 ssl;
  75. # server_name localhost;
  76. # ssl_certificate cert.pem;
  77. # ssl_certificate_key cert.key;
  78. # ssl_session_cache shared:SSL:1m;
  79. # ssl_session_timeout 5m;
  80. # ssl_ciphers HIGH:!aNULL:!MD5;
  81. # ssl_prefer_server_ciphers on;
  82. # location / {
  83. # root html;
  84. # index index.html index.htm;
  85. # }
  86. #}
  87. include /usr/nginx/conf/wss.conf;# 这里我们将反向代理新建一个文件引入进来
  88. client_max_body_size 3m;# 上传大小单位M 微信小程序上传大图片时可能需要设置
  89. }
 
  
  1. 新建wss.conf
 
  
  1. #主要是配置原来的ws 和 http 接口
  2. upstream websocket {
  3. server 10.5.11.xxx:8283;# 远程websocket服务器地址
  4. }
  5. upstream web{
  6. server www.xxx.com;# 远程http接口
  7. }
  8. # 通过下面的反向代理到上面的接口去
  9. server {
  10. listen 443;#默认httpswss协议端口
  11. ssl on;
  12. ssl_certificate /usr/nginx/conf/server.crt;#你的上传到服务器的证书位置
  13. ssl_certificate_key /usr/nginx/conf/server.key;#你的上传到服务器的证书位置
  14. ssl_session_timeout 5m;
  15. ssl_session_cache shared:SSL:50m;
  16. ssl_protocols SSLv3 SSLv2 TLSv1 TLSv1.1 TLSv1.2;
  17. ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
  18. underscores_in_headers on;#开启自定义头信息的下划线
  19. #wss协议转发 小程序里面要访问的链接
  20. location /wss {
  21. proxy_pass http://websocket;#代理到上面的地址去
  22. proxy_http_version 1.1;
  23. proxy_set_header Upgrade $http_upgrade;
  24. proxy_set_header Connection "Upgrade";
  25. }
  26. #https协议转发 小程序里面要访问的链接
  27. location /{
  28. proxy_pass http://web;#代理到原有的http的地址去
  29. proxy_set_header X-Real-IP $remote_addr;
  30. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  31. add_header Access-Control-Allow-Origin *;#跨域访问设置
  32. }
  33. }
  34. ```

你可能感兴趣的:(搭建微信小程序基本的https与wss环境)