301跳转 https_Nginx的跳转、内公网限制以及高级玩法

一、不带www的域名加301跳转

   如果不带www的域名要加301跳转,那也是和绑定域名一样,先绑定不带www的域名,只是不用写网站目录,而是进行301跳转,如

server{    listen 80;    server_name b.net;    rewrite ^/(.*) http://www.b.net/$1 permanent; }

3d95c9a23bb2f6be5328d57e574effc5.png

二、添加404网页

添加404网页,都可直接在里面添加,如:

server  {  listen       80;     server_name www.c.net;             #绑定域名     index index.htm index.html index.php;  #默认文件     root /home/www/c.net;              #网站根目录    #include location.conf;                 #调用其他规则,也可去除    error_page 404  /404.html;  }

f2becc716a15ca8c73e039878cf0dc8d.png

三、限制ip和限制ip段

server{       listen       80;       server_name  xx.qq.com;       root  /home/data/xx;        location / {                auth_basic           "closed site";                auth_basic_user_file /etc/nginx/conf/auth/xx;                 allow 10.19.0.0/16;   # 限制ip段                #allow 192.168.0.54;  # 限制某个ip                allow 127.0.0.1;                deny all;                 try_files $uri $uri/ /index.php?$query_string;                index   index.php;                location ~ \.php$ {                        fastcgi_pass php-fpm-8000;                        include conf/public_config/fcgi.conf;                }                include conf/public_config/secure_base.conf;                include conf/public_config/static.conf;            }        access_log /home/data/logs/xx.access;}

四、公网调用强制跳转到https,内网调用可以正常调用http

因为一个业务系统,既要提供公网访问服务又要提供内部系统调用。所以才有这种需求(有这种需求也是我们安全团队要求强制https的),而我们的内网段都是172.x段的

        ## 公网http调用,强制跳转到https        if ($remote_addr !~ ^(172)\.(19|18)\.(.*)\.(.*)$) {            rewrite ^(.*)$  https://$host$1 permanent;        }

五:返回自定义内容

        ## 公网http无法调用        if ($remote_addr !~ ^(172)\.(19|18)\.(.*)\.(.*)$) {            return 200 "此域名不支持公网调用,相关问题咨询请联系tom";        }

六:我们再玩得更高级一点

因为如果我们的网站内容很多,但是如果修改了配置需要改每个配置文件,所以我们想直接通过引入的方式。

vim  conf/public_config/only_ext_https.conf

## 公网http调用,强制跳转到httpsif ($remote_addr !~ ^(172)\.(19|18)\.(.*)\.(.*)$) {      rewrite ^(.*)$  https://$host$1 permanent;}

我们来看Nginx的配置:

upstream api {        server localhost:8087 max_fails=3 fail_timeout=3s weight=1;} server{       listen       80;       server_name  mp-dev.qq.cn admin-mp-dev.qq.cn;       root /data/data/webroot/mp.qq.cn/dist;      ## 公网http调用,强制跳转到https   include conf/public_config/only_ext_https.conf;         location /download/xx-mall {                alias /data/data/images/upload/xx-mall;        }         location / {                index index.html index.php;                 include conf/public_config/secure_base.conf;                include conf/public_config/static.conf;                include conf/public_config/access_log.conf;           }        access_log /data/data/logs/mp-dev.qq.cn/mp-dev.qq.cn.access.log access;        access_log /data/data/logs/mp-dev.qq.cn/mp-dev.qq.cn.access.logstash_json logstash_json;}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值