最近在开发微信小程序后台服务时碰到http协议被限制的问题,微信小程序强制所有内部访问的接口必须是https协议否则无法请求,然而我们内部配置的开发和线上运行环境均采用http;必须将协议全部配置https才能使微信小程序正常访问
生成对应域名的证书文件
    公司统一使用阿里云dns,可以在阿里云域名解析上生成免费ssl证书,具体参考 2022阿里云免费SSL证书申请全过程(图文详解)-阿里云开发者社区 (aliyun.com)
    各大云厂商也有自己ssl证书 SSL证书_免费SSL证书-付费SSL证书_服务器证书-腾讯云 (tencent.com) 等等。
    如果不想使用云厂商的ssl证书 可以使用 Let’s Encrypt 开源官方 ssl证书 Let’s Encrypt - 免费的SSL/TLS证书 (letsencrypt.org)
    也可以使用 certbot Certbot Instructions | Certbot (eff.org) 工具快速配置https
    
检查nginx
    开始配置前需要检查nginx运行状态和版本
- 首先找到nginx的配置文件位置,采用包直接安装通常配置文件的位置都在 /etc/nginx/nginx.conf文件
 
    nginx配置了多目录配置文件,建议将ssl证书文件放在对应的配置文件中,并且创建ssl目录单独放置ssl证书文件
 
    首先我们打开配置文件 xxx-cloud.conf
    修改相关配置
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 
 | upstream saas_gateway{
 server 192.168.0.82:18760;
 }
 
 upstream ssl_fdfs_group01 {
 server 127.0.0.1:8061 weight=1 max_fails=2 fail_timeout=30s;
 }
 
 upstream ssl_fdfs_group02 {
 server 127.0.0.1:8062 weight=1 max_fails=2 fail_timeout=30s;
 }
 
 server {
 listen 443 ssl http2;
 server_name saas.xxx-cloud.cn;
 
 limit_conn addr 10000;
 limit_req zone=one burst=10000000;
 
 
 
 ssl_certificate /etc/nginx/conf.d/xxx-cloud-ssl/saas.xxx-cloud.cn.pem;
 ssl_certificate_key /etc/nginx/conf.d/xxx-cloud-ssl/saas.xx-cloud.cn.key;
 ssl_session_timeout 5m;
 ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
 ssl_prefer_server_ciphers on;
 ssl_ciphers "EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5";
 ssl_session_cache builtin:2000 shared:SSL:10m;
 
 
 
 index index.html;
 root /opt/saas_web/static/dist/;
 
 location /api {
 client_max_body_size 20m;
 proxy_connect_timeout 30;
 proxy_send_timeout 90;
 proxy_read_timeout 90;
 proxy_pass http://saas_gateway/api;
 proxy_set_header Host $host;
 proxy_set_header X-Real-IP $remote_addr;
 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 }
 
 location /group1{
 proxy_next_upstream http_502 http_504 error timeout invalid_header;
 proxy_pass http://ssl_fdfs_group01;
 expires 30d;
 if ($arg_attname ~* .(pdf|jpg|png|docs|ai|jpeg)$)
 {
 add_header Content-Disposition "attachment; filename=$arg_attname";
 }
 }
 location /group2{
 proxy_next_upstream http_502 http_504 error timeout invalid_header;
 proxy_pass http://ssl_fdfs_group02;
 expires 30d;
 if ($arg_attname ~* .(pdf|jpg|png|docs|ai|jpeg)$)
 {
 add_header Content-Disposition "attachment; filename=$arg_attname";
 }
 }
 
 error_page 404 /404.html;
 location = /40x.html {
 }
 
 error_page 500 502 503 504 /50x.html;
 location = /50x.html {
 }
 }
 
 
 server {
 listen       80;
 server_name  saas.xxx-cloud.cn;
 return 301 https://$host$request_uri;
 
 }
 
 | 
注意 : nginx不要直接重启或者直接装载配置文件;以免发生不必要的错误
    1、首先使用 nginx -t 命令检查配置文件是不是有错误,没有错误继续下一步操作,如果有错误及时查资料修改
    2、使用 nginx -t reload命令加载配置文件    
 
测试通过,配置无误; 加载配置文件,正常访问,小程序也打通。
需要注意:
    由于更改了http到https,如果原先的数据库表中大量存在http协议的静态文件访问,在web端或者任何使用https服务器都不能在页面中访问http链接,需要统一更改成https链接。