[[email protected] www]# cat /app/server/nginx/conf/vhosts/default.conf server { 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.html 1 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.html HTTP/1.1 200 OK Server: nginx/1.4.4 Date: Tue, 02 Aug 2016 01:53:10 GMT Content-Type: text/html Content-Length: 16 Last-Modified: Mon, 18 Jul 2016 11:46:03 GMT Connection: keep-alive ETag: "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.html HTTP/1.1 200 OK Server: nginx/1.4.4 Date: Tue, 02 Aug 2016 01:54:40 GMT Content-Type: text/html Content-Length: 16 Last-Modified: Mon, 18 Jul 2016 11:46:03 GMT Connection: keep-alive ETag: "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的原因了;
时间: 2024-11-17 06:50:03