本机环境
# more /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
# nginx -v
nginx version: nginx/1.12.2
配置nginx
# vim /etc/nginx/conf.d/rewrite.conf
server {
listen 80;
server_name site1.orag www.site1.org;
location / {
root /www/site1.org;
index index.html index.htm;
}
}
# mkdir -pv /www/site1.org/images
# echo “This is a rewrite test page.”>/www/site1.org/index.html
# cp /usr/share/backgrounds/gnome/*.jpg /www/site1.org/images/
# vim /etc/hosts
192.168.1.175 site1.org
192.168.1.175 www.site1.org
# curl http://www.site1.org
This is a rewrite test page.
# curl -I http://www.site1.org/images/Waves.jpg
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Wed, 01 Nov 2017 03:47:58 GMT
Content-Type: image/jpeg
Content-Length: 458818
Last-Modified: Wed, 01 Nov 2017 03:43:48 GMT
Connection: keep-alive
ETag: “59f942f4-70042”
Accept-Ranges: bytes
修改rewrite.conf文件,添加rewrite指令
location / {
root /www/site1.org;
index index.html index.htm;
rewrite ^/images/(.*)$ /imgs/$1 last;
}
# systemctl reload nginx
# curl -I http://www.site1.org/images/Waves.jpg
HTTP/1.1 404 Not Found
Server: nginx/1.12.2
Date: Wed, 01 Nov 2017 04:02:38 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
# mkdir -pv /www/site1.org/imgs
# mv /www/site1.org/images/Waves.jpg /www/site1.org/imgs/.
# curl -I http://www.site1.org/images/Waves.jpg
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Wed, 01 Nov 2017 04:05:07 GMT
Content-Type: image/jpeg
Content-Length: 458818
Last-Modified: Wed, 01 Nov 2017 03:43:48 GMT
Connection: keep-alive
ETag: “59f942f4-70042”
Accept-Ranges: bytes
# curl -I http://www.site1.org/imgs/Waves.jpg ##这种方式可以访问
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Wed, 01 Nov 2017 04:06:17 GMT
Content-Type: image/jpeg
Content-Length: 458818
Last-Modified: Wed, 01 Nov 2017 03:43:48 GMT
Connection: keep-alive
ETag: “59f942f4-70042”
Accept-Ranges: bytes
模拟rewrite导致的http 500错误
再次对rewrite.conf文件做如下修改,
location / {
root /www/site1.org;
index index.html index.htm;
rewrite ^/images/(.*)$ /imgs/$1 last;
rewrite ^/imgs/(.*)$ /images/$1 ;
}
# systemctl restart nginx
# curl -I http://www.site1.org/imgs/Waves.jpg
HTTP/1.1 500 Internal Server Error
Server: nginx/1.12.2
Date: Wed, 01 Nov 2017 05:23:16 GMT
Content-Type: text/html
Content-Length: 193
Connection: close
# curl -I http://www.site1.org/images/Waves.jpg
HTTP/1.1 500 Internal Server Error
Server: nginx/1.12.2
Date: Wed, 01 Nov 2017 05:23:28 GMT
Content-Type: text/html
Content-Length: 193
Connection: close
通过上述的测试可知,出现了死循环导致的500错误。
Nginx官方给出的参考样例:
server {
… ##rewrite指令位于server上下文
rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 last;
##将/download目录中包含media目录下的任意文件请求重定向为donwload/任意/mp3/任意.mp3
rewrite ^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra last;
##将/download目录中包含audio目录下的任意文件请求重定向为donwload/任意/mp3/任意.mp3
return 403;
…
}
location /download/ { ##rewrite指令位于location上下文
rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 break;
##该last标志应该被替换 break,否则nginx将使10个周期返回500个错误
rewrite ^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra break;
return 403;
}