Nginx的模块与指令
rewrite模块
相关的指令 | 说明 |
---|---|
return | 实现对url的改写,一般与ngx变量一起使用返回指定的状态码 |
rewrite | 实现对url的改写,使用正则匹配uri,进行改写,还有各种标记 |
set | 创建或者修改ngx变量 |
if | 判断 |
1)return指令
格式1 | return code URL;返回状态码+新的URL地址 |
---|---|
格式2 | return code ;返回状态码 |
放哪 | location server if |
案例1:用户访问/admin/就报403错误
#编写配置文件
[root@web01 conf.d]# vim /etc/nginx/conf.d/redirect.conf
server{
listen 80;
server_name rewrite.web.com;
root /code;
index index.html;
location /abc/1.html {
rewrite ^(.*)$ /ccc/bbb/2.html permanent;
}
location /admin/ {
return 403;
}
}
# 检查
nginx -t
##本地DNS域名解析
#重启nginx
systemctl restart nginx
##如果没创建/code/admin/目录会报404错误,
#看报错
[root@web01 conf.d]# tail -f /var/log/nginx/error.log
2024/05/30 17:25:15 [notice] 35867#35867: exit
2024/05/30 17:25:15 [notice] 42825#42825: using the "epoll" event method
2024/05/30 17:25:15 [notice] 42825#42825: nginx/1.22.0
2024/05/30 17:25:15 [notice] 42825#42825: built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
2024/05/30 17:25:15 [notice] 42825#42825: OS: Linux 3.10.0-957.el7.x86_64
2024/05/30 17:25:15 [notice] 42825#42825: getrlimit(RLIMIT_NOFILE): 1024:4096
2024/05/30 17:25:15 [notice] 42826#42826: start worker processes
2024/05/30 17:25:15 [notice] 42826#42826: start worker process 42827
2024/05/30 17:26:08 [error] 42827#42827: *1 open() "/code/admin" failed (2: No such file or directory), client: 10.0.0.1, server: rewrite.web.com, request: "GET /admin HTTP/1.1", host: "rewrite.web.com"
2024/05/30 17:26:08 [error] 42827#42827: *1 open() "/code/favicon.ico" failed (2: No such file or directory), client: 10.0.0.1, server: rewrite.web.com, request: "GET /favicon.ico HTTP/1.1", host: "rewrite.web.com", referrer: "http://rewrite.web.com/admin"
案例2:域名间跳转
[root@web01 conf.d]# vim /etc/nginx/conf.d/redirect.conf
server{
listen 80;
server_name rewrite.web.com;
return 301 http://baidu.com;
# root /code;
# index index.html;
# location /abc/1.html {
# rewrite ^(.*)$ /ccc/bbb/2.html permanent;
# }
#
# location /admin/ {
# return 403;
# }
}
# 检查
nginx -t
##本地DNS域名解析
#重启nginx
systemctl restart nginx
2)if指令
if指令 | |
---|---|
格式 | if(…){…} |
位置 | server |
if 用于进行判断,通过ngx中变量
1.可以比大小
2.可以进行等于和不等于
3.可以进行匹配和过滤
if 指令格式
if(条件){
内容
}
案例3:rewrite.web.com只允许GET、POST、HEAD三种请求方法,其他访问禁止访问
##编写配置文件
[root@web01 conf.d]# vim /etc/nginx/conf.d/redirect.conf
server{
listen 80;
server_name rewrite.web.com;
# return 301 http://baidu.com;
root /code;
if ( $request_method !~ "GET|POST" ){
return 403;
}
location / {
index index.html;
}
# 检查
nginx -t
##本地DNS域名解析
#重启nginx
systemctl restart nginx
##GET请求成功
[root@web01 ~]# curl -H Host:rewrite.web.com http://10.0.0.7
web01
##HEAD请求失败
[root@web01 ~]# curl -I -H Host:rewrite.web.com http://10.0.0.7
HTTP/1.1 403 Forbidden
Server: nginx/1.22.0
Date: Thu, 30 May 2024 11:50:50 GMT
Content-Type: text/html
Content-Length: 153
Connection: keep-alive
3)set指令
用于自己创建或者修改ngx变量
##shell中的写法
name=666
echo $name
##nginx中写法
set $ 变量 值
案例4:设置网站是否为维护状态,若返回503,否则正常访问
##编写配置文件
[root@web01 conf.d]# vim /etc/nginx/conf.d/redirect.conf
server{
listen 80;
server_name rewrite.web.com;
# return 301 http://baidu.com;
root /code;
set $flag 0;
if ( $flag = 1 ){
return 503;
}
if ( $request_method !~ "GET|POST" ){
return 403;
}
location / {
index index.html;
}
}
# 检查
nginx -t
##本地DNS域名解析
#重启nginx
systemctl restart nginx
#检验
[root@web01 conf.d]# curl -H Host:rewrite.web.com http://10.0.0.7
web01
##修改变量flag=1然后检测
[root@web01 conf.d]# sed -i '/set/s#0#1#g' redirect.conf
[root@web01 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 conf.d]# systemctl restart nginx
[root@web01 conf.d]# curl -H Host:rewrite.web.com http://10.0.0.7
<html>
<head><title>503 Service Temporarily Unavailable</title></head>
<body>
<center><h1>503 Service Temporarily Unavailable</h1></center>
<hr><center>nginx/1.22.0</center>
</body>
</html>
4)rewrite指令
- 命令的格式与sed很像 rewrite替换的是URI
rewrite指令 | 说明 |
---|---|
格式 | rewrite 找什么(具体内容/正则/保护分组)替换成什么(具体内容/反向引用) [标记];(标记可以不写,一般默认为redirect) |
放在哪 | server location if |
❗❗ | rewrite匹配的内容是URI |
rewrite指令的301标记和302标记 | |
---|---|
redirect | 302 |
permanent | 301 |
案例5:域名跳转
##编写配置文件
[root@web01 conf.d]# vim /etc/nginx/conf.d/redirect.conf
server{
listen 80;
server_name rewrite.com;
# return 301 http://baidu.com;
root /code;
index index.html;
location /abc/1.html {
rewrite ^(.*)$ /ccc/bbb/2.html permanent;
}
}
# 检查
nginx -t
##本地DNS域名解析
#重启nginx
systemctl restart nginx
案例6:端口跳转(80端口跳转443)
server{
listen 80;
server name rewrite.web.com;
rewrite ^(.*)$ https://$server_name permanent;
}
Rewrite的各种标记
rewrite 指令根据表达式来重定向 URL ,或者修改字符串,可以应用于 server,location,if 环境下,每行 rewrite 指令最后跟一个 flag 标记,支持的 flag 标记有如下表格所示:
flag | 作用 |
---|---|
last | 本条规则匹配完成后,停止匹配,不再匹配后面的规则 |
break | 本条规则匹配完成后,停止匹配,不再匹配后面的规则 |
redirect | 返回302临时重定向,地址栏会显示跳转后的地址 |
permanent | 返回301永久重定向,地址栏会显示跳转后的地址 |
1)配置****redirect
# 编辑nginx页面文件
vim /etc/nginx/conf.d/redirect.conf
vim /etc/nginx/conf.d/redirect.conf
server{
listen 80;
server_name rewrite.web.com;
root /code;
index index.html;
location /test{
rewrite ^(.*)$ http://www.baidu.com redirect;
}
}
# 检测nginx语法
nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
# 重启nginx
systemctl restart nginx
# 本地DNS解析
10.0.0.7 rewrite.web.com
# 浏览器访问
rewrite.web.com/test
2)配置****permanent
# 编辑nginx页面文件
vim /etc/nginx/conf.d/redirect.conf
server{
listen 80;
server_name rewrite.web.com;
root /code;
index index.html;
location /test{
rewrite ^(.*)$ http://www.baidu.com permanent;
}
}
# 检测nginx语法
nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
# 重启nginx
systemctl restart nginx
# 浏览器访问
rewrite.web.com/test
3)last与break区别对比示例
[root@web01 conf.d]# vim /etc/nginx/conf.d/test.conf
server {
listen 80;
server_name rewrite.xxx.com;
root /code;
location ~ ^/break {
rewrite ^/break /test/ break;
}
location ~ ^/last {
rewrite ^/last /test/ last;
}
location /test/ {
default_type application/json;
return 200 "ok";
}
}
# 检测nginx语法
nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
#重启nginx服务
systemctl restart nginx
# 浏览器访问
rewrite.xxx.com/break
rewrite.xxx.com/last
案例
# 用户访问rewrite.xxx.com/abc/1.html 实际上访问 /ccc/bbb/2.html
server{
listen 80;
server_name rewrite.web.com;
root /code;
index index.html;
location /abc/1.html{
rewrite ^(.*)$ /ccc/bbb/2.html permanent;
}
}
# 访问rewrite.xxx.com/2018/ccc/bbb/1.htm1 实际上在访问 /2014/ccc/bbb/2.html
server{
listen 80;
server_name rewrite.web.com;
root /code;
index index.html;
location /2018/ccc/bbb/1.htm1{
rewrite ^(.*)$ //2014/ccc/bbb/2.html permanent;
rewrite /2018/(.*)$/(.*)$/1.html /2014/$1/$2/2.html permanent;
rewrite /2018/ccc/bbb/1.htm1 /2014/ccc/bbb/2.html permanent;
}
}