云计算笔记(7)nginx中最常用的七种地址重写案例

一、配置地址重写(例子)

1、配置网址跳转(用户输入nginx.iso.com跳转到media.iso.com)
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf

  server {
        listen 192.168.200.20:80;
        server_name nginx.iso.com;
        root /var/www/wordpress;
        index index.html;
}
        server {
        listen 80;
        server_name test.iso.com;
        location / {
                if ($host = "test.iso.com") {
                        rewrite ^/(.*)$ http://nginx.iso.com/$1 permanent;
                }
        }
}

2、配置客户端带参数跳转(通过访问tes.iso.com/100-100|200-*.html 转化到nginx.iso.com中)

server {
		        listen 192.168.200.20:80;
		        server_name nginx.iso.com;
		        location / {
		                        root /var/www/wordpress;
		                        index index.html;
		                }       
		        }       
		    server {    
		        listen 192.168.200.20:80;
		        server_name test.iso.com;
		        location / {
		                if ( $request_uri ~ ^/100-(100|200)-(\d+).html$ ) {
		                        rewrite (.*) http://nginx.iso.com permanent;
		                }       
		        }       
		}       

3、配置基于域名的跳转(nginx.iso.com/test/1/index.html 跳转到test.iso.com/test/1/index.html)
用户只要访问nginx.iso.com/test/1/就会跳转到test.iso.com/test/1/(nginx.iso.com/test/不会跳转)

  server {
	        listen 80;
	        server_name nginx.iso.com;
	        root /var/www/wordpress;
	
	        location /test/1/ {
	                if ($host = "nginx.iso.com") {
	                        rewrite ^/(.*)$ http://test.iso.com/$1 permanent;
	                }
	        }
	}

4、配置网站升级,网站管理员IP地址(192.168.200.222)可以访问网站,其他请求全部拒绝

   server {
	        listen 80;
	        server_name nginx.iso.com;
	        location / {
	                root /var/www/wordpress;
	                index index.php index.html;
	                set $rewrite true;
	                if ( $remote_addr = "192.168.200.222" ) {
	                        set $rewrite false;
	                }
	                if ( $rewrite = true ) {
	                        rewrite (.+) /error.html;
	                }
	        location = /error.html {
	                root /error;	需要手动创建
	                }
	        }
	}

5、用户访问nginx.iso.com/upload/index.html 跳转到 media.iso.com 主页上

  server {
	        listen 80;
	        server_name nginx.iso.com;
	        location / {
	                root /var/www/wordpress;
	                index index.php index.html;
	        }
	        location ~* /upload/.*\.php$ {	根目录下upload下的所有.php的文件都会匹配
	                rewrite (.+) http://media.iso.com permanent;	进行跳转,或给用户提醒
	或              return 301 http://media.iso.com
	或              return  200 "The Server Is Upgrade"
	
	        }
	}

6、last和break的应用
[root@nginx /]# vim /usr/local/nginx/conf/nginx.conf

 server {
	        listen 80;
	        server_name nginx.iso.com;
	        root /var/www/wordpress;
	        location / {
	                rewrite /1.html /2.html;
	                rewrite /2.html /3.html;
	        }       
	        location /2.html {
	                rewrite /2.html /a.html;
	        }       
	        location /3.html {
	                rewrite /3.html /b.html;
	        }       
	}  

[root@nginx wordpress]# echo “1.html” > ./1.html
[root@nginx wordpress]# echo “3.html” > ./2.html
[root@nginx wordpress]# echo “2.html” > ./2.html
[root@nginx wordpress]# echo “3.html” > ./3.html
[root@nginx wordpress]# echo “a.html” > ./a.html
[root@nginx wordpress]# echo “b.html” > ./b.html
客户端验证:http://www.benet.com/1.html最终被跳转到b.html
1)last用法

    server {
	        listen 80;
	        server_name nginx.iso.com;
	        root /var/www/wordpress;
	        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;
	        }
	}

客户端验证:http://www.benet.com/1.html最终被跳转到a.html

2)break用法

	        server {
	        listen 80;
	        server_name nginx.iso.com;
	        root /var/www/wordpress;
	        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;
	        }
	}

客户端验证:http://www.benet.com/1.html最终被跳转到2.html
7、常见的变量

$request_uri	除域名外的其他部分
$host			网站的域名
$http_host		域名加其他,相当于$host+$request_uri
$remote_addr	客户端的IP地址
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

H . C . Y

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值