码迷,mamicode.com
首页 > Web开发 > 详细

nginx url重写

时间:2016-08-03 15:18:12      阅读:280      评论:0      收藏:0      [点我收藏+]

标签:

 

[root@web01 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;
}
[root@web01 www]# tree /app/www/
/app/www/
├── index.html
├── index.php
└── post
    └── test.html

1 directory, 3 files
[root@web01 www]# cat /app/www/index.html 
clnking@163.com
[root@web01 www]# cat /app/www/post/test.html 
post/test.html

 

[root@web01 www]# curl 192.168.1.24/last.html
clnking@163.com
[root@web01 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
[root@web01 www]# curl 192.168.1.24/break.html
clnking@163.com
[root@web01 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
[root@web01 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>

技术分享

[root@web01 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>

技术分享

[root@web01 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重写

标签:

原文地址:http://www.cnblogs.com/bass6/p/5732689.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!