/ 中存储网

Nginx和Apache的伪静态设置

2014-02-07 16:27:01 来源:kejihao

apache URL重写转换成 NGINX URL重写 工具

http://www.anilcetin.com/convert-apache-htaccess-to-nginx/

首先Apache的Rewite规则差别不是很大,但是Nginx的Rewrite规则比Apache的简单灵活多了

Nginx可以用if进行条件匹配,语法规则类似C

if ($http_user_agent ~ MSIE) {

   rewrite ^(.*)$   /msie/$1

break;

}

官方文档请点击这里

Rewrite的Flags

Flags can be any of the following:

* last - completes processing of rewrite directives, after which searches for corresponding URI and location

* break - completes processing of rewrite directives

*redirect - returns temporary redirect with code 302; it is used if the substituting line begins with http://

* permanent - returns permanent redirect with code 301

last - 完成重写指令后,搜索相应的URI和位置。相当于Apache里的[L]标记,表示完成rewrite,不再匹配后面的规则。

break - 中止Rewirte,不在继续匹配。

redirect - 返回临时重定向的HTTP状态302

permanent - 返回永久重定向的HTTP状态301

ZEND Framework的重定向规则:

案例一:

全部重定向到 /index.php

rewrite ^/(.*) /index.php?$1&;

案例二:

如果文件或目录不存在则重定向到index.php

if (!-e $request_filename) {

rewrite ^/(.*) /index.php?$1&;

}

Wordpress的重定向规则:

案例一:

http://www.wemvc.com/12 重定向到 http://www.wemvc.com/index.php?p=12

if (!-e $request_filename) {

rewrite ^/(.+)$ /index.php?p=$1 last;

}

案例二:

与zendframework配置很像

if (!-e $request_filename) {

rewrite ^/(.*)     /index.php?$1&;

}

以下为Discuz完整的Rewrite for Nginx规则

if (!-f $request_filename) {//如果文件不存在,说明是模拟静态文件,URL重写了

rewrite ^/archiver/((fid|tid)-[w-]+.html)$ /archiver/index.php?$1 last;

rewrite ^/forum-([0-9]+)-([0-9]+).html$ /forumdisplay.php?fid=$1&page=$2 last;

rewrite ^/thread-([0-9]+)-([0-9]+)-([0-9]+).html$ /viewthread.php?tid=$1&extra=page%3D$3&page=$2 last;

rewrite ^/space-(username|uid)-(.+).html$ /space.php?$1=$2 last;rewrite ^/tag-(.+).html$ /tag.php?name=$1 last;

}

文件及目录匹配,其中:

-f和!-f用来判断是否存在文件

-d和!-d用来判断是否存在目录

-e和!-e用来判断是否存在文件或目录

-x和!-x用来判断文件是否可执行

正则表达式全部符号解释

~ 为区分大小写匹配

~* 为不区分大小写匹配

!~和!~* 分别为区分大小写不匹配及不区分大小写不匹配

(pattern) 匹配 pattern 并获取这一匹配。所获取的匹配可以从产生的 Matches 集合得到,在VBScript 中使用 SubMatches 集合,在JScript 中则使用 $0…$9 属性。要匹配圆括号字符,请使用 ‘(’ 或 ‘)’。

^ 匹配输入字符串的开始位置。

$ 匹配输入字符串的结束位置。

###### bbs.uenu.com ###################

server {

listen       80;

server_name bbs.uenu.com;

location / {

root   E:/home/bbs/htdocs;

index index.html index.htm index.php default.php;

rewrite ^/archiver/((fid|tid)-[w-]+.html)$ /archiver/index.php?$1 last;

rewrite ^/forum-([0-9]+)-([0-9]+).html$ /forumdisplay.php?fid=$1&page=$2 last;

rewrite ^/thread-([0-9]+)-([0-9]+)-([0-9]+).html$ /viewthread.php?tid=$1&extra=page%3D$3&page=$2 last;

rewrite ^/space-(username|uid)-(.+).html$ /space.php?$1=$2 last;

rewrite ^/tag-(.+).html$ /tag.php?name=$1 last;

}

error_page   500 502 503 504 /50x.html;

location = /50x.html {

root   html;

}

location ~ .*.php?$ {

root           E:/home/bbs/htdocs;

fastcgi_pass   127.0.0.1:9000;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME          E:/home/bbs/htdocs$fastcgi_script_name;

include        fastcgi_params;

}

}

############################nginx rewrite参数详解############################

一些可用的全局变量有,可以用做条件判断(待补全)

$args

$content_length

$content_type

$document_root

$document_uri

$host

$http_user_agent

$http_cookie

$limit_rate

$request_body_file

$request_method

$remote_addr

$remote_port

$remote_user

$request_filename

$request_uri

$query_string

$scheme

$server_protocol

$server_addr

$server_name

$server_port

$uri

举例:

abc.domian.com/sort/2 => abc.domian.com/index.php?act=sort&name=abc&id=2

主机名  不区分大小写 和 (.*).domain.com 进行匹配

if ($host ~* (.*).domain.com) {

set $sub_name $1;

rewrite ^/sort/(d+)/?$ /index.php?act=sort&cid=$sub_name&id=$1 last;

}

测试可惜不能用else,另外,也不知如何测试重定向的部分条件打印输出。

只能通过重新生效配置之后查看效果才能知道ok与否。唉,菜鸟献上,大虾指点

续:

结合QeePHP的例子

请求的目录不存在

1. if (!-d $request_filename) {

2. rewrite ^/([a-z-A-Z]+)/([a-z-A-Z]+)/?(.*)$

3. /index.php?namespace=user&controller=$1&action=$2&$3

4. last;

5. rewrite ^/([a-z-A-Z]+)/?$ /index.php?namespace=user&controller=$1

6. last;

7. break;

多目录转成参数

abc.domian.com/sort/2 =>

abc.domian.com/index.php?act=sort&name=abc&id=2

1. if ($host ~* (.*).domain.com) {

2. set $sub_name $1;

3. rewrite ^/sort/(d+)/?$ /index.php?act=sort&cid=$sub_name&id=$1

4. last;

5. }

目录对换

/123456/xxxx -> /xxxx?id=123456

1. rewrite ^/(d+)/(.+)/ /$2?id=$1 last;

例如下面设定nginx在用户使用ie的使用重定向到/nginx-ie目录下:

1. if ($http_user_agent ~ MSIE) {

2. rewrite ^(.*)$ /nginx-ie/$1 break;

3. }

目录自动加“/”

1. if (-d $request_filename){

2. rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent; 301永久重定向

3. }

禁止htaccess

1. location ~/.ht {

2. deny all;

3. }

禁止多个目录

1. location ~ ^/(cron|templates)/ {

2. deny all;

3. break;

4. }

禁止以/data开头的文件

可以禁止/data/下多级目录下.log.txt等请求;

1. location ~ ^/data {

2. deny all;

3. }

禁止单个目录

不能禁止.log.txt能请求

1. location /searchword/cron/ {

2. deny all;

3. }

禁止单个文件

1. location ~ /data/sql/data.sql {

2. deny all;

3. }

给favicon.ico和robots.txt设置过期时间;

这里为favicon.ico为99天,robots.txt为7天并不记录404错误日志

1. location ~(favicon.ico) {

2. log_not_found off;

3. expires 99d;

4. break;

5. }

6.

7. location ~(robots.txt) {

8. log_not_found off;

9. expires 7d;

10. break;

11. }

设定某个文件的过期时间;这里为600秒,并不记录访问日志

1. location ^~ /html/scripts/loadhead_1.js {

2. access_log off;

3. root /opt/lampp/htdocs/web;

4. expires 600;

5. break;

6. }

文件反盗链并设置过期时间 【可参考:Nginx的防盗链配置】

这里的return 412为自定义的http状态码,默认为403,方便找出正确的盗链的请求

“rewrite ^/ http://leech.c1gstudio.com/leech.gif;”显示一张防盗链图片

“access_log off;”不记录访问日志,减轻压力

“expires 3d”所有文件3天的浏览器缓存

location ~* ^.+.(jpg|jpeg|gif|png|swf|rar|zip|css|js)$ {

valid_referers none blocked *.c1gstudio.com *.c1gstudio.net localhost 208.97.167.194;

if ($invalid_referer) {

         rewrite ^/ http://leech.c1gstudio.com/leech.gif;

         return 412;

        break;

     }

access_log off;

     root /opt/lampp/htdocs/web;

   expires 3d;

     break;

}

只充许固定ip访问网站,并加上密码

1. root /opt/htdocs/www;

2. allow 208.97.167.194;

3. allow 222.33.1.2;

4. allow 231.152.49.4;

5. deny all;

6. auth_basic "C1G_ADMIN";

7. auth_basic_user_file htpasswd;

将多级目录下的文件转成一个文件,增强seo效果

/job-123-456-789.html

指向/job/123/456/789.html

1. rewrite ^/job-([0-9]+)-([0-9]+)-([0-9]+).html$ /job/$1/$2/jobshow_$3.html last;

将根目录下某个文件夹指向2级目录

如/shanghaijob/ 指向/area/shanghai/

如果你将last改成permanent,那么浏览器地址栏显是/location/shanghai/

1. rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2 last;

上面例子有个问题是访问/shanghai 时将不会匹配

1. rewrite ^/([0-9a-z]+)job$ /area/$1/ last;

2. rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2 last;

这样/shanghai

也可以访问了,但页面中的相对链接无法使用,

如./list_1.html真实地址是/area/shanghia/list_1.html会变成/list_1.html,导至无法访问。

那我加上自动跳转也是不行咯

(-d $request_filename)它有个条件是必需为真实目录,而我的rewrite不是的,所以没有效果

1. if (-d $request_filename){

2. rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;

3. }

知道原因后就好办了,让我手动跳转吧

1. rewrite ^/([0-9a-z]+)job$ /$1job/ permanent;

2. rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2 last;

文件和目录不存在的时候重定向:

1. if (!-e $request_filename) {

2. proxy_pass http://127.0.0.1;

3. }

域名跳转

1. server

2. {

3. listen 80;

4. server_name jump.c1gstudio.com;

5. index index.html index.htm index.php;

6. root /opt/lampp/htdocs/www;

7. rewrite ^/ http://www.c1gstudio.com/;

8. access_log off;

9. }

三级域名跳转

1. if ($http_host ~* "^(.*).i.c1gstudio.com$") {

2. rewrite ^(.*) http://top.yingjiesheng.com$1;

3. break;

4. }

域名镜向

1. server

2. {

3. listen 80;

4. server_name mirror.c1gstudio.com;

5. index index.html index.htm index.php;

6. root /opt/lampp/htdocs/www;

7. rewrite ^/(.*) http://www.c1gstudio.com/$1 last;

8. access_log off;

9. }

某个子目录作镜向

1. location ^~ /zhaopinhui {

2. rewrite ^.+ http://zph.c1gstudio.com/ last;

3. break;

4. }