nginx配置https一个域名不同路径分别访问前端和后端两个项目

可参考:

https://help.aliyun.com/document_detail/98728.html?spm=5176.2020520163.cas.15.4ed756a7Oi0HTv
https://www.cnblogs.com/phpper/p/6441475.html

背景:

https://www.liyongquan.com 主域名是经过配置ssl安全证书的指向前端页面,

http://api.liyongquan.com 子域名是未经过配置ssl指向后端接口项目的。

此时页面访问接口会跨协议。因此配置接口请求url为https://www.liyongquan.com/api/

www.liyongquan.com.conf 配置文件内容如下:

server {
    listen 80;
    server_name  www.liyongquan.com;
    return 301 https://$http_host$request_uri;
}

server {
        listen       443 ssl;
        server_name  www.liyongquan.com;
        client_max_body_size 125M;
        access_log /data/logs/www/www.liyongquan.com.access.log;
        error_log /data/logs/www/www.liyongquan.com.error.log;

        ssl_certificate ./cert/www.liyongquan.com.pem;
        ssl_certificate_key ./cert/www.liyongquan.com.key;
        ssl_session_timeout 5m;
        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;

        #rewrite ^(.*)$ https://$host$1 permanent;
        location / {
           root /data/web/zhanfu-build/;
           index index.html;
           #try_files $uri $uri/ /index.html;
        }
        location ^~ /api {
            proxy_pass_header Server;
            proxy_set_header Host $http_host;
            proxy_redirect off;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header Referer $http_referer;
            proxy_set_header X-Scheme $scheme;
            proxy_pass http://127.0.0.1:7000;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
        }
}

www.zhanfu6.api.com.conf 配置文件内容如下:

server {
        listen       7000;

        client_max_body_size 125M;
        root /data/web/zhanfu-server/public;
        access_log /data/logs/www/www.liyongquan.api.com.access.log;
        error_log /data/logs/www/www.liyongquan.api.com.error.log;
        index index.html index.htm index.php;
        location / {
                try_files $uri $uri/ /index.php?$query_string;
        }

        location ~ .*\.(php|php5)?$ {
                fastcgi_pass  UNIX:/var/run/php-fpm/php-cgi.sock;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
                include        fastcgi.conf;
        }
        location = /50x.html {
        }

}

测试配置是否成功:nginx -t
平滑重启生效:nginx -s reload

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值