Nginx——Rewrite 与 Location 的网页匹配

目录

1.Nginx常见模块

1.http

2.server

3.location

2.location 

1.类型

2.常用的正则表达式

3.location常用的匹配规则

4.location优先级

二.REWRITE模块

1.rewrite功能

2.语法规则

3.跳转场景

4.Rewrite 执行顺序

5.基于域名的跳转

6.基于客户端IP访问跳转

7.基于旧域名跳转到新域名后面加目录

8. 基于参数匹配的跳转

9.基于目录下所有的php文件

10.基于普通的一条url

总结

相同点:

不同点:

rewrite会写在location里,执行顺序


1.Nginx常见模块

1.http

http块是Nginx服务器配置中的重要部分,代理、缓存和日志定义等绝大多数的功能和第三方模块的配置都可以放在这模块中。作用包括:文件引入、MIME-Type定义、日志自定义、是否使用sendfile传输文件、连接超时时间、单连接请求数上限等。

2.server

server块,虚拟主机(虚拟服务器)。作用:使得Nginx服务器可以在同一台服务器上只要运行一组Nginx进程,就可以运行多个网站。

3.location

location块是server块的一个指令。作用:基于Nginx服务器接收到的请求字符串,虚拟主机名称(ip,域名)、url匹配,对特定请求进行处理。

2.location 

1.类型

  • 精确匹配:location = /  {.....}
  • 一般匹配:location  /  {.....}
  • 正则匹配:location ~ / {.....}

2.常用的正则表达式

字符 含义
. 匹配任意单个字符,可以是一个汉字
^ 行首锚定, 用于模式的最左侧
$ 行尾锚定,用于模式的最右侧
* 匹配前面字符任意次(包括0次)
? 0或1次
+ 1次或多次(1次或以上)
\ 转义符
\d 只匹配数字
{n} 重复n次
{n,} 至少n次(n次货以上)
{n,m} n到m次
[ ] 定义匹配字符的范围,只匹配一次
[c] 单个字符
[a-z] 匹配任意小写字母
[a-zA-Z0-9] 匹配任意字母和数字
() 表达式的开始和结束位置
| 或运算符a|b

3.location常用的匹配规则

规则表达式 规则含义
= 进行普通字符精确匹配。也就是完全匹配
^~ 表示普通字符匹配。使用前缀匹配。如果匹配成功,则不再匹配其他 location
~ 表示执行一个正则匹配,区分大小写
~* 表示执行一个正则匹配,不区分大小写
!~ 表示执行一个正则匹配,区分大小写取反
!~* 表示执行一个正则匹配,不区分大小写取反

4.location优先级

  • 等号类型(=)的优先级最高。一旦匹配成功,则不再查找其他匹配项。

  • ^~类型表达式。一旦匹配成功,则不再查找其他匹配项。

  • 正则表达式类型(~和~*)的优先级次之。常规字符串匹配类型(不带任何修饰符的匹配),按前缀匹配。

  • 通用匹配(/),如果没有其它匹配,任何请求都会匹配到。

(location = 完整路径) > (location ^~ 完整路径) > (location ~* 完整路径) >(location ~ 完整路径)  > (location /)通用匹配

例1

[root@localhost conf]# vim nginx.conf

[root@localhost conf]# mkdir -p /web/aaa

[root@localhost conf]# vim /web/aaa/index.html

hello ooo!!!

例2

 [root@localhost conf]# vim nginx.conf

[root@localhost conf]# mkdir -p /web/bbb

将图片拖到/web/aaa 和/web/bbb目录下

例3 

[root@localhost conf]# mkdir -p /web/bbb

将图片拖到/web/aaa 和/web/bbb目录下

二.REWRITE模块

1.rewrite功能

rewrite功能就是,使用nginx提供的全局变量或自己设置的变量,结合正则表达式和标记位实现URL重写以及重定向。

rewrite 只能在
server {}
location {}
if {}

中使用,并且默认只能对域名后边的除去传递的参数外的字符串起作用
例如:
http://www.ooo.com/abc/bbs/index.html?a=1&b=2 只针对/abc/bbs/index.html重写

2.语法规则

rewrite <regex> <ip或者域名> [flag];
regex:表示正则匹配规则
replacement:表示跳转后的内容
flag:表示rewrite 支持的 flag 标记

last:本条规则匹配完成后,继续向下匹配新的location URL规则,一般用在server和if中

break:本条规则匹配完成即终止,不再匹配后面的任何规则。一般用在location中

redirect:返回 302 临时重定向,浏览器地址会显示跳转后的 URL 地址,爬虫不会更新 url(因为是临时)。

permanent:返回 301 永久重定向,浏览器地址栏会显示跳转后的 URL 地址,爬虫更新 url。

3.跳转场景

  • 可以调整用户浏览的 URL,看起来更规范,合乎开发及产品人员的需求

  • 为了让搜索引擎搜录网站内容及用户体验更好,企业会将动态 URL 地址伪装成静态地址提供服务

  • 网址换新域名后,让旧的访问跳转到新的域名上。

  • 根据特殊变量、目录、客户端的信息进行 URL 调整等。

4.Rewrite 执行顺序

  • 执行server块里面的rewrite指令。

  • 执行location 匹配。

  • 执行选定的location中的rewrite指令。

5.基于域名的跳转

 [root@localhost conf]# vim /etc/nginx.conf 

 [root@localhost web]# vim /etc/hosts

摁回车之后会跳转到 ooo.com

[root@localhost conf]# vim /etc/nginx.conf 
 35     server {
 36         listen       80;
 37         server_name  www.oyyy.com;
 43         location /index.html {
 44         if ($host = 'www.oyyy.com') {
 45         rewrite ^/(.*)$ http://www.ooo.com/$1 permanent;
 46         }
 47             root /web/ooo;
 48         }
[root@localhost conf]# cd /web/
[root@localhost web]# mkdir ooo
[root@localhost web]# vim ooo/index.html
hello ooo yeah!!!!
[root@localhost web]# vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.68.30 www.oyyy.com www.ooo.com

6.基于客户端IP访问跳转

 [root@www conf]# vim nginx.conf

[root@www conf]# vim /var/www/html/oyyy.html 

http://192.168.68.30/oyyy.html

本机访问需要域名后面添加任意内容

其他主机访问可以直接输入IP地址 

 只有 IP为192.168.68.30 能正常访问,其它地址都是维护页面

[root@www conf]# vim nginx.conf
 30     #keepalive_timeout  0;
 31     keepalive_timeout  65;
 32 
 33     #gzip  on;
 34 
 35     server {
 36         listen       80;
 37         server_name  www.oyyy.com;
 38 
 39         charset utf-8;
 40 
 41         #access_log  logs/host.access.log  main;
 42         set $rewrite true;
 43         if ($remote_addr = "192.168.68.30") {
 44         set $rewrite false;
 45         }
 46         if ($rewrite = true) {
 47                 rewrite (.+) /oyyy.html;
 48         }
 49         location /oyyy.html {
 50                 root   /var/www/html;
 51         }
 52         location / {
 53             root   html;
 54             index  index.html index.htm;
 55         }
[root@www conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@www conf]# mkdir -p /var/www/html
[root@www conf]# vim /var/www/html/oyyy.html
hello oyyy heihei!!!
[root@www conf]# nginx -s reload

7.基于旧域名跳转到新域名后面加目录

[root@www conf]# vim nginx.conf

[root@www html]# mkdir -p bbs/post

[root@www html]# vim bbs/post/oyyy.html

[root@www ~]# vim /etc/hosts

 http://bbs.oyyy.com/post/oyyy.html

会跳转到http://www.oyyy.com/post/oyyy.html

[root@www conf]# vim nginx.conf
 35     server {
 36         listen       80;
 37         server_name  bbs.oyyy.com;
 38     
 39         charset utf-8;
 40         
 41         access_log logs/www.oyyy.com.log;
 42         location /post {
 43                 rewrite (.+) http://www.oyyy.com/bbs/$1 permanent;
 44         }
 45 
 46         location / {
 47             root   html;
 48             index  index.html index.htm;
 49         }
[root@www html]# mkdir -p bbs/post
[root@www html]# vim /post/index.html
hello bbs post heiheihei!!!
[root@www conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@www conf]# nginx -s reload

8. 基于参数匹配的跳转

 [root@www conf]# vim nginx.conf

http://www.oyyy.com/100-200-100.html 或者http://www.oyyy.com/100-200-100.html 会跳转到http://www.oyyy.com

[root@www conf]# vim nginx.conf
 35     server {
 36         listen       80;
 37         server_name  bbs.oyyy.com;
 38 
 39         charset utf-8;
 40 
 41         #access_log logs/www.oyyy.com.log; 
 42         location ~ ^/100-(100|200)-(\d+).html$ {
 43                 rewrite (.+) http://www.oyyy.com permanent;
 44         }
 45         
 46         location / {
 47             root   html;
 48             index  index.html index.htm;
 49         }
[root@www conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@www conf]# nginx -s reload

9.基于目录下所有的php文件

 [root@www conf]# vim nginx.conf

输入http://www.oyyy.com/upload/123.php跳转到www.oyyy.com

 [root@www conf]# vim nginx.conf
 35     server {
 36         listen       80;
 37         server_name  www.oyyy.com;
 38         
 39         charset utf-8;
 40 
 41         #access_log logs/www.oyyy.com.log; 
 42         location ~* /upload/.*\.php$ {
 43                 rewrite (.+) http://www.oyyy.com permanent;
 44         }
 45         location / {
 46             root   html;
 47             index  index.html index.htm;
 48         }
[root@www conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@www conf]# nginx -s reload

10.基于普通的一条url


 35     server {
 36         listen       80;
 37         server_name  www.oyyy.com;
 38         
 39         charset utf-8;
 40        
 41         #access_log logs/www.oyyy.com.log; 
 42         location ~* ^/abc/123.html$ {
 43                 rewrite (.+) http://www.oyyy.com permanent;
 44          }
 45         location / {
 46             root   html;
 47             index  index.html index.htm;
 48         }

输入http://www.oyyy.com/abc/123.html会跳转到www.oyyy.comd的首页 

总结

相同点:

  • 都能实现跳转

不同点:

  • rewrite是在同一域名内更改获取资源的路径
  • location是对一类路径做控制访问或反向代理,还可以proxy_pass到其他机器

rewrite会写在location里,执行顺序

  • 执行server块里面的rewrite指令
  • 执行location匹配
  • 执行选定的location中的rewrite指令

猜你喜欢

转载自blog.csdn.net/oyyy3/article/details/121317366