zoukankan      html  css  js  c++  java
  • 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 命令终结他。 

    作者:沐雪
    文章均系作者原创或翻译,如有错误不妥之处,欢迎各位批评指正。本文版权归作者和博客园共有,如需转载恳请注明。
    如果您觉得阅读这篇博客让你有所收获,请点击右下方【推荐】
    找一找教程网-随时随地学软件编程 http://www.zyiz.net/

  • 相关阅读:
    数据库 封装类CppSQLite3的helloword VC6
    数据库 sqlite 进阶
    数据库 sqlite3_get_table,sqlite3_free_table
    数据库 sqlite3_open,sqlite3_exec,slite3_close
    数据库 SQLite C++ 接口
    数据库 如何在VC6下使用sqlite3
    MFC CButtonST使用技巧(一)(二)(三)
    MFC CButtonST简介
    MFC CButtonST 在你的程序中如何使用CButtonST类
    MFC静态分割后锁定分隔条/限制分隔条的移动范围 方法1
  • 原文地址:https://www.cnblogs.com/puzi0315/p/14691992.html
Copyright © 2011-2022 走看看