nginx反向代理批量实现https协议访问

介绍下在nginx环境下,通过nginx反向代理批量实现https协议访问的方法,有需要的朋友参考下。
为站点增加https访问,开始只配置了www 域名 下的https,发现css和js都无法正常加载。

原因分析:https页面,如果加载http协议的内容,会被认为页面不安全,尤其是IE,刷新一下页面就要弹出一次确认。

后来苦逼的把各个子域名都加入了https配置,nginx.conf里写各个子域名都写了一个443的server配置,每新增一个域名,还得copy一份,如果是修改一下站点的配置,还得改两次以上。

后来跟同事讨论,发现使用反向代理,可以快捷现实,配置如下:
sever{
    listen 80;
    sever_name www.jbxue.com;
    root /home/webroot/www/;
    index index.php index.html;
    # ...
}
sever{
    listen 80;
    sever_name img.jbxue.com;
    root /home/webroot/img/;
    index index.php index.html;
    # ...
}
sever{
    listen 80;
    sever_name static.jbxue.com;
    root /home/webroot/static/;
    index index.php index.html;
    # ...
}
sever{
    listen 80;
    sever_name upload.jbxue.com;
    root /home/webroot/upload/;
    index index.php index.html;
    # ...
}
server {
    listen 443;
    server_name www.jbxue.com img.jbxue.com static.jbxue.com upload.jbxue.com;
    ssl on;
    ssl_certificate /usr/local/nginx/conf/jbxue.com.crt;
    ssl_certificate_key /usr/local/nginx/conf/jbxue.com.key;
 
    location /{
        proxy_pass http://127.0.0.1:80;
        proxy_redirect off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_set_header SSL '1';
        proxy_redirect http:// https://;
    }
}
这样配置好,访问:
https://www.jbxue.com/
https://img.jbxue.com/
https://static.jbxue.com/
https://upload.jbxue.com/

会被反向代理至:
http://www.jbxue.com/
http://img.jbxue.com/
http://static.jbxue.com/
http://upload.jbxue.com/

而不需要对各个域名都配置一个443的站点。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值