標籤:
[[email protected] www]# cat /app/server/nginx/conf/vhosts/default.confserver { listen 80 default_server; server_name localhost; root /app/www; index index.php index.htm index.html; rewrite /last.html /index.html last; rewrite /break.html /index.html break; rewrite /redirect.html /index.html redirect;#302臨時重新導向 rewrite /permanent.html /index.html permanent;#301永久重新導向 rewrite ^/html/(.+?).html$ /post/$1.html permanent; location ~ .*\.(php|php5)?$ { #fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi.conf; } access_log /app/log/nginx/access/default.log;}[[email protected] www]# tree /app/www//app/www/├── index.html├── index.php└── post └── test.html1 directory, 3 files[[email protected] www]# cat /app/www/index.html [email protected]163.com[[email protected] www]# cat /app/www/post/test.html post/test.html
[[email protected] www]# curl 192.168.1.24/last.html[email protected]163.com[[email protected] www]# curl --head 192.168.1.24/last.htmlHTTP/1.1 200 OKServer: nginx/1.4.4Date: Tue, 02 Aug 2016 01:53:10 GMTContent-Type: text/htmlContent-Length: 16Last-Modified: Mon, 18 Jul 2016 11:46:03 GMTConnection: keep-aliveETag: "578cc17b-10"Accept-Ranges: bytes
[[email protected] www]# curl 192.168.1.24/break.html[email protected]163.com[[email protected] www]# curl --head 192.168.1.24/break.htmlHTTP/1.1 200 OKServer: nginx/1.4.4Date: Tue, 02 Aug 2016 01:54:40 GMTContent-Type: text/htmlContent-Length: 16Last-Modified: Mon, 18 Jul 2016 11:46:03 GMTConnection: keep-aliveETag: "578cc17b-10"Accept-Ranges: bytes
[[email protected] www]# curl 192.168.1.24/redirect.html<html><head><title>302 Found</title></head><body bgcolor="white"><center><h1>302 Found</h1></center><hr><center>nginx/1.4.4</center></body></html>
[[email protected] www]# curl 192.168.1.24/permanent.html<html><head><title>301 Moved Permanently</title></head><body bgcolor="white"><center><h1>301 Moved Permanently</h1></center><hr><center>nginx/1.4.4</center></body></html>
[[email protected] www]# curl 192.168.1.24/html/test.html<html><head><title>301 Moved Permanently</title></head><body bgcolor="white"><center><h1>301 Moved Permanently</h1></center><hr><center>nginx/1.4.4</center></body></html>
注意:因為301和302不能簡單的只返回狀態代碼,還必須有重新導向的URL,這就是return指令無法返回301,302的原因了;
nginx url重寫