2.4Nginx Rewrite

链接:https://blog.csdn.net/qq_27517377/article/details/80813019

需求1:过滤请求nginx的URL中包含id=520的请求,转到172.16.1.7的80端口处理

理解if教学示例:
[root@lb01 conf.d]# cat 03_www.etiantian.org.conf
server {
    listen 80;
    server_name www.etiantian.org;
    root /data/www;
    location / {
        index index.html;
        # 如果请求的uri含有id=520,则执行proxy_pass的动作,否则什么都不管
        proxy_set_header Host $http_host;
        if ($request_uri ~* 'id=520') {
        proxy_pass http://172.16.1.7:80;
        }
    }
}

[root@lb01 conf.d]# curl -H"host:www.etiantian.org" 172.16.1.5:80
i am lb01

需求2:将用户请求url.etiantian.org.zh跳转到url.etiantian.org/zh,将用户请求url.etiantian.org.en跳转到url.etiantian.org/en

理解set教学示例:根据客户端浏览器请求头语言进行跳转。
[root@web01 ~]# cat /etc/nginx/conf.d/05_url.etiantian.org.conf 
server {
    listen 80;
    server_name url.etiantian.org.zh url.etiantian.org.en;
    
    location / {
        set $lang zh;
        if ($http_host ~* "zh") {
            set $lang zh;
        }
        
        if ($http_host ~* "en"){
            set $lang en;
        }

        # 这个跳转会执行,lang这个变量是什么取决于用户请求的是什么域名
        rewrite ^/$ http://url.etiantian.org/$lang/ permanent;
    }
}

server {
    listen 80;
    server_name www.etiantian.org;
    root /data/www;
    location / {
        index index.html;
    }               
        default_type text/html;
        charset utf-8;
        if ($http_user_agent ~* "chrome|MSIE|firefox") {
        #return 200 '请更换浏览器\n';
        #return 520;
        return 301 http://www.oldboyedu.com;
    }
}

[root@lb01 conf.d]# curl -A "chrome" -H"host:www.etiantian.org" 172.16.1.5
请更换浏览器

#regex常用正则表达式说明
见书稿
https://www.cnblogs.com/LiuYanYGZ/p/5903946.html

#flag标记说明
last        #本条规则匹配完成后,继续向下匹配新的location URI规则
break       #本条规则匹配完成即终止,不再匹配后面的任何规则
redirect    #返回302临时重定向, 地址栏会显示跳转后的地址
permanent   #返回301永久重定向, 地址栏会显示跳转后的地址

root@web01]# cat www.etiantian.org.conf
server {
    listen 80;
    server_name www.etiantian.org;
    root /data/www;

    location / {
        rewrite /1.html /2.html break;
        rewrite /2.html /3.html;
    }

    location /2.html {
        rewrite /2.html /a.html;
    }

    location /3.html {
        rewrite /3.html /b.html;
    }
}

#测试结果: 当请求/1.html,最终会访问/2.html
#因为:在location{}内部,遇到break,本location{}内以及后面的所有location{}内的所有指令都不再执行。


[root@web01]# cat www.etiantian.org.conf
server {
    listen 80;
    server_name www.etiantian.org;
    root /data/www;

    location / {
        rewrite /1.html /2.html last;
        rewrite /2.html /3.html;
    }

    location /2.html {
        rewrite /2.html /a.html;
    }

    location /3.html {
        rewrite /3.html /b.html;
    }
}

#测试结果:当请求/1.html,最终会访问/a.html
#因为:在location{}内部,遇到last,本location{}内后续指令不再执行
#而重写后的url会对所在的server{...}标签重新发起请求,从头到尾匹配一遍规则,那个匹配则执行哪个。
 

需求3:实现访问http://blog.etiantian.org跳转到http://www.etiantian.org/blog/oldboy.html。
外部跳转时使用这种方法,浏览器地址会变为跳转后的地址,另外,要事先设置http://www.etiantian.org/blog/oldboy.html有结果输出,不然会出现401等权限错误。
(1)配置Nginx rewrite规则
跳转前http://url.etiantian.org对应站点的配置如下:

server {
        listen       80;
        server_name  blog.etiantian.org;
        if ( $http_host ~* "^(.*)\.etiantian\.org$") { 
           set $domain $1; 
           rewrite ^(.*) http://www.etiantian.org/$domain/oldboy.html break; 
          }
}
#要配置的规则内容为:
        if ( $http_host ~* "^(.*)\.etiantian\.org$") { 
           set $domain $1; 
           rewrite ^(.*) http://www.etiantian.org/$domain/oldboy.html break; 
          }

企业级解决方案4:Nginx图片及目录防盗链解决方案


1)我们公司的网站
www.etiantian.org;

#Preventing hot linking of images and other file types
location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip)$ {
    valid_referers none blocked server_names *.etiantian.org etiantian.org;
    if ($invalid_referer) {
        rewrite ^/ http://bbs.etiantian.com/img/nolink.jpg;
    }

需求5: 用户通过手机设备访问www.etiantian.org跳转至 m.etiantian.org 
例:京东网站手机访问https://www.jd.com/跳转到https://m.jd.com 
server {
    listen 80;
    server_name www.etiantian.org;
    root /code;
    location / {
        index index.html;
        if ($http_user_agent ~* "android|phone|iphone") {
            rewrite ^(.*)$ http://m.etiantian.org redirect;
        }
    }
}

需求6: 用户通过http协议请求,能自动跳转至https协议。
server {
    listen 80;
    server_name www.etiantian.org;
    rewrite ^(.*)$ https://$http_host$1;
    #return 301 https://$http_host$request_uri;
}


错误页面优雅显示    
需求7: 当访问服务器遇到403 404 502等错误时,自动转到临时维护的静态页https://404.life/

当访问服务器遇到403 404 502等错误时
1.访问本地页面。
2.跳转URL地址。

server {
    listen 80;
    server_name url.etiantian.org;
    root /code;
    charset utf-8;
    location / {
        index index.html;
    }
    # 接收返回的错误信息,定向到@error_status 
    #error_page 403 404 500 502 @error_status;
    error_page 403 404 500 502 http://www.etiantian.org;
    location @error_status {
        rewrite ^(.*)$ /404.html break;
    }
}

需求1: 根据用户浏览器请求头中携带的语言调度到不同的页面。
例:访问:www.redhat.com    
[root@web01 ~]# cat /etc/nginx/conf.d/url.etiantian.org.conf 
server {
    listen 80;
    server_name url.etiantian.org;
    root /data/www;

    location / {
        index index.html;
        set $language "zh";
        if ($http_accept_language ~* "zh-CN|zh") {
            set $language "zh";
        }

        if ($http_accept_language ~* "en") {
            set $language "en";
        }

        rewrite ^/$  /$language redirect;
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值