nginx配置如下:
# cat rewrite.conf
server {
listen 8096;
server_name _;
root /opt/app/code;
location ~ ^/break {
rewrite ^/break /test/ break; # break 后,后面的指令也不会执行了。如果没有 break ,就会执行后面的指令,比如这里的 return。
return 200 'break';
}
location ~ ^/last {
rewrite ^/last /test/ last; # last 后,后面的指令也不会执行了。如果没有 last ,就会执行后面的指令,比如这里的 return。
return 200 'last';
}
location ~ ^/redirect {
rewrite ^/redirect /test/ redirect; # redirect 后,后面的指令也不会执行了。如果没有 redirect,就会执行后面的指令,比如这里的 return。
return 200 'redirect';
}
location ~ ^/permanent {
rewrite ^/permanent /test/ permanent; # permanent 后,后面的指令也不会执行了。如果没有 permanent ,就会执行后面的指令,比如这里的 return。
return 200 'permanent';
}
location /test/ {
default_type application/json;
return 200 '{"status": "sucess"}';
}
}
# ll /opt/app/code/
total 0
演示结果:
- 请求 /test/ 返回预期的结果
# curl 192.168.1.188:8096/test/
{"status": "sucess"}
- 请求 /break 返回 404,因为匹配到 ^/break 后,^/break rewrite 为 /test/ ,就 break 了,访问 /test/ 但 /opt/app/code/ 里并没有这个目录
# curl 192.168.1.188:8096/break
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.10.2</center>
</body>
</html>
- 请求 /last 返回了 location /test/ 的结果,因为 last 会让 rewrite 后的 /test/ 请求在 server 块重新匹配一遍,就配置到了 location /test/
# curl 192.168.1.188:8096/last
{"status": "sucess"}
- redirect 结果(客户端每次请求都会执行一次请求跳转)
# curl -vL 192.168.1.188:8096/redirect
* About to connect() to 192.168.1.188 port 8096 (#0)
* Trying 192.168.1.188... connected
* Connected to 192.168.1.188 (192.168.1.188) port 8096 (#0)
> GET /redirect HTTP/1.1
> User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.27.1 zlib/1.2.3 libidn/1.18 libssh2/1.4.2
> Host: 192.168.1.188:8096
> Accept: */*
>
< HTTP/1.1 302 Moved Temporarily
< Server: nginx/1.10.2
< Date: Wed, 28 Feb 2018 18:46:48 GMT
< Content-Type: text/html
< Content-Length: 161
< Location: http://192.168.1.188:8096/test/
< Connection: keep-alive
<
* Ignoring the response-body
* Connection #0 to host 192.168.1.188 left intact
* Issue another request to this URL: 'http://192.168.1.188:8096/test/'
* Re-using existing connection! (#0) with host 192.168.1.188
* Connected to 192.168.1.188 (192.168.1.188) port 8096 (#0)
> GET /test/ HTTP/1.1
> User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.27.1 zlib/1.2.3 libidn/1.18 libssh2/1.4.2
> Host: 192.168.1.188:8096
> Accept: */*
>
< HTTP/1.1 200 OK
< Server: nginx/1.10.2
< Date: Wed, 28 Feb 2018 18:46:48 GMT
< Content-Type: application/json
< Content-Length: 20
< Connection: keep-alive
<
* Connection #0 to host 192.168.1.188 left intact
* Closing connection #0
{"status": "sucess"}
- permanent结果(不清理缓存的话,客户端每次请求都会用 permanent 后的地址)
# curl -vL 192.168.1.188:8096/permanent
* About to connect() to 192.168.1.188 port 8096 (#0)
* Trying 192.168.1.188... connected
* Connected to 192.168.1.188 (192.168.1.188) port 8096 (#0)
> GET /permanent HTTP/1.1
> User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.27.1 zlib/1.2.3 libidn/1.18 libssh2/1.4.2
> Host: 192.168.1.188:8096
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
< Server: nginx/1.10.2
< Date: Wed, 28 Feb 2018 18:48:44 GMT
< Content-Type: text/html
< Content-Length: 185
< Location: http://192.168.1.188:8096/test/
< Connection: keep-alive
<
* Ignoring the response-body
* Connection #0 to host 192.168.1.188 left intact
* Issue another request to this URL: 'http://192.168.1.188:8096/test/'
* Re-using existing connection! (#0) with host 192.168.1.188
* Connected to 192.168.1.188 (192.168.1.188) port 8096 (#0)
> GET /test/ HTTP/1.1
> User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.27.1 zlib/1.2.3 libidn/1.18 libssh2/1.4.2
> Host: 192.168.1.188:8096
> Accept: */*
>
< HTTP/1.1 200 OK
< Server: nginx/1.10.2
< Date: Wed, 28 Feb 2018 18:48:44 GMT
< Content-Type: application/json
< Content-Length: 20
< Connection: keep-alive
<
* Connection #0 to host 192.168.1.188 left intact
* Closing connection #0
{"status": "sucess"}
网友评论