关于nginx中的rewrite指令的使用
2026/7/11 11:18:21 网站建设 项目流程

依赖模块ngx_http_rewrite_module,默认已安装,可直接使用。

位置server,location,if

作用:使用PCRE正则表达(确保服务器已安装pcre依赖)式改变请求的 uri,返回重定向地址或进行另外的处理。

语法:rewriteregexreplacement[flag];

regex:正则表达式,与uri匹配

replacement:匹配成功后,替换的路径,如果以http://、https://、or$scheme 开头,停止rewrite匹配,执行剩余代码,最终返回重定向地址

flag:可选

  • last:停止继续处理请求,使用重写后的uri地址,在当前server中匹配其它的location块处理;
  • break:与break指令类似,停止继续处理请求,转而去寻找资源文件
  • redirect:返回302临时重定向,停止rewrite匹配,继续执行剩余代码,replacement不以http://、https://、or$scheme 开头
  • permanent:返回301永久重定向,停止rewrite匹配,继续执行剩余代码,replacement不以http://、https://、or$scheme 开头

rewrite regex replacement:

不指定flag时:

若是 http://、 https://、 or $scheme 开头,停止rewrite匹配,继续向下执行其它代码,但会返回302重定向;

否则使用重写后的uri,继续进行处理,并返回结果;

location /user {
return 200 "/user === /user === /user\n";
}

location /url {

#启用rewrite日志
rewrite_log on;
error_log logs/error.log notice;
rewrite ^/url/return.*$ http://test.com;

rewrite ^/url/continue/(.*)\$ /test/user/\$1;
rewrite ^/test/user/(.*)\$ /user/\$1;
#添加请求头
add_header test-continue finish;
#请求携带参数,返回200
if ($args) {
return 200 "/url ==== /url ==== /url\n";
}
}

访问:http://192.168.242.200/url/return?666

访问:http://192.168.242.200/url/continue/no-flag


rewriteregexreplacementlast;

停止继续处理请求,使用重写后的uri地址,在当前server中匹配其它的location块处理;

location /url {
#启用rewrite日志
rewrite_log on;
error_log logs/error.log notice;
rewrite ^/url/continue/(.*)\$ /test/user/\$1last;
rewrite ^/test/user/(.*)\$ /user/\$1;

#添加请求头
add_header test-continue finish;
#请求携带参数,返回200
if ($args) {
return 200 "/url ==== /url ==== /url\n";
}
}

访问:http://192.168.242.200/url/continue/no-flag?666


rewriteregexreplacementbreak;

与break指令类似,停止继续处理请求,转而去寻找资源文件

location /url {
#启用rewrite日志
rewrite_log on;
error_log logs/error.log notice;
rewrite ^/url/continue/(.*)\$ /test/user/\$1break;
rewrite ^/test/user/(.*)\$ /user/\$1;

#添加请求头
add_header test-continue finish;
#请求携带参数,返回200
if ($args) {
return 200 "/url ==== /url ==== /url\n";
}
}

访问:http://192.168.242.200/url/continue/no-flag?666

寻找资源文件:/usr/local/nginx/html/test/user/no-flag


rewriteregexreplacementredirect;

返回302临时重定向,停止rewrite匹配,继续执行剩余代码,replacement不以http://、https://、or$scheme 开头

location /url {
#启用rewrite日志
rewrite_log on;
error_log logs/error.log notice;
rewrite ^/url/continue/(.*)\$ /test/user/\$1redirect;
rewrite ^/test/user/(.*)\$ /user/\$1;

#添加请求头
add_header test-continue finish;
#请求携带参数,返回200
if ($args) {
return 200 "/url ==== /url ==== /url\n";
}
}

访问:http://192.168.242.200/url/continue/no-flag?666


rewriteregexreplacementpermanent;

返回301永久重定向,停止rewrite匹配,继续执行剩余代码,replacement不以http://、https://、or$scheme 开头

location /url {
#启用rewrite日志
rewrite_log on;
error_log logs/error.log notice;
rewrite ^/url/continue/(.*)\$ /test/user/\$1permanent;
rewrite ^/test/user/(.*)\$ /user/\$1;

#添加请求头
add_header test-continue finish;
#请求携带参数,返回200
if ($args) {
return 200 "/url ==== /url ==== /url\n";
}
}

访问:http://192.168.242.200/url/continue/no-flag?666


需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询