- 1. ngx_http_rewrite_module
- 2. Example
- rewrite后uri:/test/,last标识符会从头开始匹配/test/ location,结果符合预期
- rewrite后uri:/test/index.html,last标识符会从头开始匹配/test/ location,结果符合预期
- rewrite后uri:/test/,break停止匹配rewrite module,不会跳出当前location,理应寻找/test/下的index.html
- 暂不清楚为啥返回 test
- rewrite后uri:/test/index.html,break不会跳出当前location,返回/test/下的index.html
- 删除/test/index.html后,由于没有权限访问/test/的目录结构,返回403
- 2.2. Domian Name Redirect
- 2.3. Match User-Agent
- 2.4. Forbidden Access
1. ngx_http_rewrite_module
1.1. set
Directive
Syntax: set $variable value ;
Default: Close
Context: server,location,if
Instroduction
Sets a value for the specified variable. The value can contain text, variables.
If you need set variables on http block, you need use map directive.
1.2. break
Directive
Syntax: break ;
Default: Close
Context: server,location,if
Instroduction
Stops processing the current level set of ngx_http_rewrite_module directives.
1.3. return
Directive
Syntax: return (code [text]) | (code URL) | URL ;
Default: Close
Context: server,location,if
Instroduction
Stops processing and returns the specified code to a client. The non-standard code 444 closes a connection without sending a response header.
In addition, a URL for temporary redirect with the code 302 can be specified as the sole parameter. Such a parameter should start with the “http://“, “https://“, or “$scheme” string.
1.4. if
Directive
Syntax: if (condition) {commands}
Defautl: Close
Context: server,location
Instroduction
If condition is true,the commands will be executed.
- variable
If the value is empty or 0,result is false.Before 1.0.1,any string starts with “0” ,result retrun false.
- Operators
Strings match: = , !=
Regular match: ~ , ~ , !~ , !~
File match: -f , !-f ; -d , !-d ; -e , !-e ; -x ; !-x
If a regular expression includes the “}” or “;” characters, the whole expressions should be enclosed in single or double quotes
1.5. rewrite
Directive
Syntax: rewrite regex replacement [flags] ;
Default: Close
Context: server,location,if
Instroduction
If the specified regular expression matches a request URI, URI is changed as specified in the replacement string. The rewrite directives are executed sequentially in order of their appearance in the configuration file.
If a replacement string starts with “http://“, “https://“, or “$scheme”, the processing stops and the redirect is returned to a client.
Flags:
- last: stops processing the current level set of ngx_http_rewrite_module directives and starts a search for a new location matching the changed URI.
- break: stops processing the current set of ngx_http_rewrite_module directives as with the break directive.
- redirect: returns a temporary redirect with the 302 code; used if a replacement string does not start with “http://“, “https://“, or “$scheme”.
- permanent: return 301 code and new URL to client.
Notice: The rewrite directive’s URI is $uri ,it doesn’t include $args.If you rewrite URL include $args,if directive is only choice.For example:
# Source: localhost/api?cmd=game_config&tdsourcetag=args
# Target: localhost/index.php?c=cmd&m=send_notice&tdsourcetag=args
if ($request-uri ~ ^localhost/api\?cmd=game_config&tdsourcetag=(.+)$) {
return http://localhost/index.php?c=cmd&m=send_notice&tdsourcetag=$1 ;
}
1.6. rewrite_log
Directive
Syntax: rewrite_log on|off ;
Defautl: rewrite_log off ;
Context: http,server,location,if ;
Instroduction
Enables or disables logging of ngx_http_rewrite_module module directives processing results into the error_log at the notice level.
2. Example
2.1. rewrite flags
2.1.1. last & break
- Nginx Configuration
[root@centos-81 conf.d]# cat rewrite.conf
server {
listen 8080 default_server ;
root /opt/website/write ;
rewrite_log on ;
location /location1/ {
rewrite /location1/(.*) /test/$1 last ;
return 200 "location1\r\n" ;
}
location /location2/ {
rewrite /location2/(.*) /test/$1 break ;
return 200 "location2\r\n" ;
}
location /test/ {
return 200 "test\r\n" ;
}
location / {
return 200 "null\r\n" ;
}
}
[root@centos-81 ~]# mkdir -p /opt/website/write/{location1,location2,test}
[root@centos-81 ~]# ls /opt/website/write/|awk ‘{print $1”.index” > “/opt/website/write/“$1”/index.html”}’
- Test
```html
rewrite后uri:/test/,last标识符会从头开始匹配/test/ location,结果符合预期
[root@centos-81 ~]# curl -s 192.168.1.81:8080/location1/ test
rewrite后uri:/test/index.html,last标识符会从头开始匹配/test/ location,结果符合预期
[root@centos-81 ~]# curl -s 192.168.1.81:8080/location1/index.html test
rewrite后uri:/test/,break停止匹配rewrite module,不会跳出当前location,理应寻找/test/下的index.html
暂不清楚为啥返回 test
[root@centos-81 ~]# curl -s 192.168.1.81:8080/location2/ test
rewrite后uri:/test/index.html,break不会跳出当前location,返回/test/下的index.html
[root@centos-81 ~]# curl -s 192.168.1.81:8080/location2/index.html test.index
删除/test/index.html后,由于没有权限访问/test/的目录结构,返回403
[root@centos-81 nginx]# rm -f /opt/website/write/test/index.html [root@centos-81 ~]# curl -s 192.168.1.81:8080/location2/
403 Forbidden
<a name="R591X"></a>
#### 2.1.2. Redirect
- **Nginx Configuration**
```nginx
server {
listen 8080 default_server ;
root /opt/website/write ;
location /location1/ {
if ($uri ~ ^/location1/(.*)$) {
return http://$host:8080/test/$1 ;
}
return 200 "location1\r\n" ;
}
location /location2/ {
rewrite /location2/(.*) /test/$1 redirect ;
return 200 "location2\r\n" ;
}
location /location3/ {
rewrite /location3/(.*) /test/$1 permanent ;
return 200 "location2\r\n" ;
}
location /test/ {
return 200 "test\r\n" ;
}
}
- Test
[root@centos-81 ~]# curl -vL 192.168.1.81:8080/location1/ # code 302
> GET /location1/ HTTP/1.1
> User-Agent: curl/7.29.0
> Host: 192.168.1.81:8080
> Accept: */*
>
< HTTP/1.1 302 Moved Temporarily
......
< Location: http://192.168.1.81:8080/test/
......
> GET /test/ HTTP/1.1
> User-Agent: curl/7.29.0
> Host: 192.168.1.81:8080
> Accept: */*
>
< HTTP/1.1 200 OK
< Server: nginx
< Date: Mon, 08 Jul 2019 00:06:17 GMT
< Content-Type: application/octet-stream
< Content-Length: 6
< Connection: keep-alive
< Keep-Alive: timeout=55
<
test
[root@centos-81 ~]# curl -vL 192.168.1.81:8080/location2/ # code 302
> GET /location2/ HTTP/1.1
> User-Agent: curl/7.29.0
> Host: 192.168.1.81:8080
> Accept: */*
>
< HTTP/1.1 302 Moved Temporarily
......
< Location: http://192.168.1.81:8080/test/
......
<
> GET /test/ HTTP/1.1
> User-Agent: curl/7.29.0
> Host: 192.168.1.81:8080
> Accept: */*
>
< HTTP/1.1 200 OK
......
<
test
[root@centos-81 ~]# curl -vL 192.168.1.81:8080/location3/ # code 301
> GET /location3/ HTTP/1.1
> User-Agent: curl/7.29.0
> Host: 192.168.1.81:8080
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
< Server: nginx
......
< Location: http://192.168.1.81:8080/test/
......
<
> GET /test/ HTTP/1.1
> User-Agent: curl/7.29.0
> Host: 192.168.1.81:8080
> Accept: */*
>
< HTTP/1.1 200 OK
......
<
test
2.1.3. No flags
Nginx Configuration
server {
listen 8080 default_server ;
root /opt/website/write ;
location /location1/ {
rewrite /location1/(.*) /test/$1 ;
return 200 "location1\r\n" ;
}
location /location2/ {
rewrite /location2/(.*) /test/$1 ;
}
location /location3/ {
rewrite /location3/(.*) http://$host:8080/test/$1 ;
return 200 "location3\r\n" ;
}
location /test/ {
return 200 "test\r\n" ;
}
}
Test
[root@centos-81 ~]# curl -L 192.168.1.81:8080/location1/
location1
[root@centos-81 ~]# curl -L 192.168.1.81:8080/location1/index.html
location1
[root@centos-81 ~]# curl -L 192.168.1.81:8080/location2/
test
[root@centos-81 ~]# curl -L 192.168.1.81:8080/location2/index.html
test
[root@centos-81 ~]# curl -IL 192.168.1.81:8080/location3/
HTTP/1.1 302 Moved Temporarily
Server: nginx
Date: Mon, 08 Jul 2019 00:23:02 GMT
Content-Type: text/html
Content-Length: 154
Connection: keep-alive
Keep-Alive: timeout=55
Location: http://192.168.1.81:8080/test/
Expires: Tue, 09 Jul 2019 00:23:02 GMT
Cache-Control: max-age=86400
>
HTTP/1.1 200 OK
Server: nginx
Date: Mon, 08 Jul 2019 00:23:02 GMT
Content-Type: application/octet-stream
Content-Length: 6
Connection: keep-alive
Keep-Alive: timeout=55
[root@centos-81 ~]# curl -IL 192.168.1.81:8080/location3/index.html
HTTP/1.1 302 Moved Temporarily
Server: nginx
Date: Mon, 08 Jul 2019 00:23:07 GMT
Content-Type: text/html
Content-Length: 154
Connection: keep-alive
Keep-Alive: timeout=55
Location: http://192.168.1.81:8080/test/index.html
Expires: Tue, 09 Jul 2019 00:23:07 GMT
Cache-Control: max-age=86400
>
HTTP/1.1 200 OK
Server: nginx
Date: Mon, 08 Jul 2019 00:23:07 GMT
Content-Type: text/html
Content-Length: 6
Connection: keep-alive
Keep-Alive: timeout=55
Expires: Tue, 09 Jul 2019 00:23:07 GMT
Cache-Control: max-age=86400
2.2. Domian Name Redirect
2.2.1. different server directive(better)
Nginx Configuration ```nginx server { listen 80 ; server_name www.heyingsheng.com ; rewrite ^(.*)$ ${scheme}://www.heyang.com$1 permanent ; }
server { listen 80 default_server ; server_name *.heyang.com ; location / { return 200 “heyang.com root dir \r\n” ; } }
- **Test**
[root@centos-81 ~]# curl -I www.heyingsheng.com
```http
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Mon, 08 Jul 2019 12:40:59 GMT
Content-Type: text/html
Content-Length: 178
Connection: keep-alive
Keep-Alive: timeout=55
Location: http://www.heyang.com/
Expires: Tue, 09 Jul 2019 12:40:59 GMT
Cache-Control: max-age=86400
2.2.2. Use sname directive
Nginx Configuration
server {
listen 80 default_server ;
server_name *.heyang.com www.heyingsheng.com ;
if ($host = "www.heyingsheng.com") {
rewrite ^(.*)$ ${scheme}://www.heyang.com$1 permanent ;
}
location / {
return 200 "heyang.com root dir \r\n" ;
}
}
Test
[root@centos-81 ~]# curl -I www.heyingsheng.com
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Mon, 08 Jul 2019 12:51:34 GMT
Content-Type: text/html
Content-Length: 178
Connection: keep-alive
Keep-Alive: timeout=55
Location: http://www.heyang.com/
Expires: Tue, 09 Jul 2019 12:51:34 GMT
Cache-Control: max-age=86400
2.3. Match User-Agent
Nginx Configuration
server {
listen 80 default_server ;
server_name *.heyang.com www.heyingsheng.com ;
if ($http_user_agent ~ "MSIE [4-6]") {
return 200 "Browser is not supported!\r\n" ;
}
location / {
return 200 "heyang.com root dir \r\n" ;
}
}
Test
[root@centos-81 ~]# curl -A "User-Agent:Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)" www.heyang.com
Browser is not supported!
[root@centos-81 ~]# curl -A "User-Agent:Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0" www.heyang.com
heyang.com root dir
2.4. Forbidden Access
Nginx Configuration
server {
listen 80 default_server ;
server_name *.heyang.com www.heyingsheng.com ;
location / {
return 200 "heyang.com root dir \r\n" ;
}
location ~* \.(sh|py)$ {
return 403 "Forbidden!\r\n" ;
}
}
Test
[root@centos-81 ~]# curl -s www.heyang.com/aa/bb.py
Forbidden!
[root@centos-81 ~]# curl -s www.heyang.com/aa/bb.php
heyang.com root dir