主要职能:基于本地服务器路劲的重写
一般用于本地不同文件的加载或路径的改写
rewrite <regex> <replacement> [flag];
正则匹配规则 跳转后的内容 rewrite支持的flag标记
^ 匹配输入字符串的起始位置
$ 匹配输入字符串的结束位置
* 匹配前面的字符零次或多次
+ 匹配前面的字符一次或多次
? 匹配前面的字符零次或一次
. 匹配除“\n”之外的任何单个字符
\ 将后面接着的字符标记为一个特殊字符或一个原义字符或一个向后引用
\d 匹配纯数字
\w 匹配字母或数字或下划线或汉字
\s 匹配任意的空白符
\b 匹配单词的开始或结束
{n} 重复n次
{n,} 重复n次或多次
{n,m} 重复n到m次
{,m} 最多重复m次
[] 定义匹配的字符范围,[a]:匹配单个字符a
() 表达式的开始和结束位置
| 或表达式
| location = / {} | 精确匹配 / |
|---|---|
| location / {} | 一般匹配(因为所有的地址都以 / 开头,所以这条规则将匹配到所有的请求) |
| location ~ / {} | 按照正则表达式的方式匹配 / |
~ 执行一个正则匹配,区分大小写
~* 执行一个正则匹配,不区分大小写
!~ 执行一个正则匹配,区分大小写不匹配
!~* 执行一个正则匹配,不区分大小写不匹配
^~ 普通字符匹配;使用前缀匹配。如果匹配成功,则不再匹配其他location
= 普通字符精确匹配。也就是完全匹配
@ 定义一个命名的location,使用在内部定向时
精确性越高,优先级越大
必选规则
1.location = / {}
=为精确匹配 / ,主机名后面不能带任何字符串,比如访问 / 和 /data,则 / 匹配,/data 不匹配 再比如 location = /abc,则只匹配/abc ,/abc/或 /abcd不匹配。若 location /abc,则即匹配/abc 、/abcd/ 同时也匹配 /abc/。
2.location / {}
因为所有的地址都以 / 开头,所以这条规则将匹配到所有请求比如访问 / 和 /data, 则 / 匹配, /data 也匹配,但若后面是正则表达式会和最长字符串优先匹配(最长匹配)
3.location /documents/ {}
匹配任何以 /documents/ 开头的地址,匹配符合以后,还要继续往下搜索其它 location
只有其它 location后面的正则表达式没有匹配到时,才会采用这一条
4.location /documents/abc {}
匹配任何以 /documents/abc 开头的地址,匹配符合以后,还要继续往下搜索其它 location
只有其它 location后面的正则表达式没有匹配到时,才会采用这一条
5.location ^~ /images/ {}
匹配任何以 /images/ 开头的地址,匹配符合以后,停止往下搜索正则,采用这一条
6.location ~ .(gif|jpg|jpeg)$ {}**
匹配所有以 gif、jpg或jpeg 结尾的请求.然而,所有请求 /images/ 下的图片会被 location ^~ /images/ 处理,因为 ^~ 的优先级更高,所以到达不了这一条正则(匹配5不匹配6)
7.location /images/abc {}
最长字符匹配到 /images/abc,优先级最低,继续往下搜索其它 location,会发现 ^~ 和 ~ 存在
8.location ~ /images/abc {}
匹配以/images/abc 开头的,优先级次之,只有去掉 location ^~ /images/ 才会采用这一条
9.location /images/abc/1.html {}
匹配/images/abc/1.html 文件,如果和正则location ~ /images/abc/1.html 相比,正则优先级更高
旧域名www.yyy.com变更,使用www.yzq.com代替,旧域名不废除,跳转的到新域名,且后面参数不变
未修改前

[root@yzq nginx]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.28.100 www.yyy.com www.yzq.com
[root@yzq ~]# mkdir -p /var/log/nginx ##开启日志功能,自定义一个日志存放路径
[root@yzq conf]# vim nginx.conf
[root@yzq 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

server {
listen 80;
server_name www.yyy.com; ##域名修改
#charset koi8-r;
access_log /var/log/nginx/www.yyy.com-access.log; ##>开启对日志保存路径进行修改
location / { ##原有location位置插入
if ($host = `www.yyy.com`){ ##$host(内置变量)为rewrite全局变量,代表请求主机头字段或者主机名
rewrite ^/(.*)$ http://www.yzq.com/$1 permanent
##$1为
}
root html;
index index.html index.htm;
}
[root@yzq conf]# systemctl restart nginx

(上一个实验配置删除)
今天公司业务新版本上线,要求所有 IP 访问任何内容都显示一个固定维护页面,只有公司 IP :192.168.28.100访问正常。
[root@yzq html]# vim weihu.html
[root@yzq html]# cat weihu.html
this is weihu
[root@yzq conf]# vim nginx.conf
[root@yzq 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
server {
listen 80;
server_name www.yyy.com;
#charset koi8-r;
access_log /var/log/nginx/www.yyy.com-access.log;
#设置是否合法的IP标记
set $rewrite true; #设置变量$rewrite,变量值为布尔值true
#判断是否为合法IP
#remode_addr表示客户端
if ($remote_addr = "192.168.226.100"){ #当客户端IP为192.168.28.100时,将变量值设为false,不进行重写
set $rewrite false;
}
#除了合法IP,其它都是非法IP,进行重写跳转维护页面
if ($rewrite = true){ #当变量值为true时,进行重写
rewrite (.+) /weihu.html; #将域名后边的路径重写成/weihu.html,例如www.yyy.com/weihu.html
}
location =/weihu.html { #网页返回/var/www/html/weihu.html的内容
root /var/www/html;
}
location / {
root html;
index index.html index.htm;
重启服务
[root@yzq html]# systemctl restart nginx
其它主机访问

本机访问

访问的是 http://bbs.yyy.com/post/1.html,现在需要将这个域名下面的访问都跳转到http://www.yzq.com/bbs/post/1.html
创建http://bbs.yyy.com/post/1.html路径
[root@yzq html]# cd /usr/local/nginx
[root@yzq nginx]# ls
client_body_temp fastcgi_temp logs sbin uwsgi_temp
conf html proxy_temp scgi_temp
[root@yzq nginx]# cd html/
[root@yzq html]# mkdir -p bbs/post
[root@yzq html]# ls
50x.html bbs error.png index.html renwu.jpg
[root@yzq html]# cd bbs/
[root@yzq bbs]# ls
post
[root@yzq bbs]# cd post/
[root@yzq post]# vim post
[root@yzq post]# vim 1.html
[root@yzq post]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.28.100 bbs.yyy.com www.yzq.com
[root@yzq post]# vim /usr/local/nginx/conf/nginx.conf
[root@yzq post]# 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@yzq post]# systemctl restart nginx
访问bbs.yyy.com/post/1.html跳转到www.yzq.com/bbs/post/1.html

访问http://www.yyy.com/100-(100|200)-100.html会跳转到https://www.yzq.com
[root@yzq post]# vim /usr/local/nginx/conf/nginx.conf
[root@yzq post]# 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@yzq post]# systemctl restart nginx

要求访问http://www.yyy.com/upload/123.php跳转到www.yzq.com(场景:注册/登录)
[root@yzq post]# vim /usr/local/nginx/conf/nginx.conf
[root@yzq post]# 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@yzq post]# systemctl reload nginx.service

访问 www.yyy.com/asda/123.html 跳转到首页www.yyy.com
[root@yzq post]# vim /usr/local/nginx/conf/nginx.conf
[root@yzq post]# 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@yzq post]# systemctl reload nginx.service

调整用户浏览的URL,看起来更规范,合乎开发及产品人员的需求,网址换新域名后,让旧访问跳转到新的域名上