nginx https作网关配置webapi路由规则

为何有这篇文章?

因为我有多个小程序,分别调用不同的api站点,服务器只能安装一个https单域名证书。

 

1、原webapi接口部署完毕,接口地址比如

http://www.zyiz.net/api/getarticle?id=100
(我们最常用的webapi规则都是  http://xxx.com/api/xxx)

 

2、nginx 配置成https站点模式; (如何搭建,可以参考如下链接)

参考网址:https://github.com/dunwu/nginx-tutorial#%E8%B5%84%E6%BA%90

https://www.cnblogs.com/luxiaoyao/p/10034009.html   (这篇里,生成https证书 章节可以忽略)

http://www.zyiz.net/tech/detail-153635.html 

3、修改nginx配置文件:nginx.conf

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 443 ssl;
        server_name api.uweixin.com; #修改为申请证书绑定的域名
        
        #root html;
        #index index.html index.htm;
        #证书文件名称
        ssl_certificate ../cert/1_api.uweixin.com_bundle.crt; 
        #私钥文件名称
        ssl_certificate_key ../cert/2_api.uweixin.com.key; 
        ssl_session_timeout 30m;
       
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;
        location /api/{
            proxy_pass http://localhost:1666/api/;
        }
        location /zyizapi/{
            proxy_pass  http://www.zyiz.net/api/;
        }
       
        #location / {
                #proxy_pass http://boot; 
                #root html; 
                #index  index.html index.htm;
        #}
        
 
    }
    }

其中,最容易出问题的就是匹配的规则;

location /api/{
            proxy_pass http://localhost:1666/api/;
        }
        location /zyizapi/{
            proxy_pass  http://www.zyiz.net/api/;
        }

看如上代码;我配置了2个webapi接口站点;

注意:(1)location 后面,/api/ 前后都要有杠/;
(2)proxy_pass  http://api.zyiz.net/api/  这个结尾也要有杠/;

4、配置完毕

这样就可以访问通过调用:

https://api.uweixin.com/zyizapi/getarticle?id=100   来调用

http://www.zyiz.net/api/getarticle?id=100

5、我重新加载 nginx的配置文件后,却迟迟未生效,纠结了几个小时;网上的解决方案是 skil 终结nginx的进程重新开启Nginx;我试了下,果然有效果;

taskkill /f /t /im nginx.exe    ----终结Nginx进程。

start nginx   --启动nginx 

nginx -s reload  ---重新加载配置文件。

nginx stop  停止nginx经常不起效果;所以最好是用taskkill 命令终结他。 

posted @ 2021-04-22 23:29  沐雪架构师  阅读(245)  评论(0编辑  收藏  举报