关于nginx中的rewrite指令的使用

关于nginx中的rewrite指令的使用
依赖模块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;#请求携带参数返回200if ($args) {return 200 /url /url /url\n;}}访问http://192.168.242.200/url/return?666访问http://192.168.242.200/url/continue/no-flagrewriteregexreplacementlast;停止继续处理请求使用重写后的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;#请求携带参数返回200if ($args) {return 200 /url /url /url\n;}}访问http://192.168.242.200/url/continue/no-flag?666rewriteregexreplacementbreak;与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;#请求携带参数返回200if ($args) {return 200 /url /url /url\n;}}访问http://192.168.242.200/url/continue/no-flag?666寻找资源文件/usr/local/nginx/html/test/user/no-flagrewriteregexreplacementredirect;返回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;#请求携带参数返回200if ($args) {return 200 /url /url /url\n;}}访问http://192.168.242.200/url/continue/no-flag?666rewriteregexreplacementpermanent;返回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;#请求携带参数返回200if ($args) {return 200 /url /url /url\n;}}访问http://192.168.242.200/url/continue/no-flag?666