伪静态是一种可以把文件后缀改成任何可能的一种方法,如果我想把 php 文件伪静态成 html 文件,这种相当简单的,下面来介绍 nginx 伪静态配置方法有需要了解的朋友可参考。
Nginx 伪静态配置
nginx 里使用伪静态是直接在 nginx.conf 中写规则的,并不需要像 apache 要开启写模块(mod_rewrite)才能进行伪静态。
nginx 只需要打开 nginx.conf 配置文件,在 server 里面写需要的规则即可。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
server {
listen 80;
server_name bbs.jb51.net;
index index.html index.htm index.php;
root /home/www/bbs;
error_page 404 /404.htm; [[配置404错误页面]]
location ~ .*.(php|php5)?$ {
[[fastcgi_pass]] unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fcgi.conf;
}
[[下面就是伪静态了]]
location /{
rewrite ^(.*)/equip(d+).html$ $1/index.php? m=content&c=index&a=lists&catid=$2 last;
}
access_log access_log off;
}
|
然后重启 nginx 服务器伪静态就生效了,这种维护起来很是不方便我们可以把它写在外部文件如 bbs_nginx.conf 中
在/home/www/bbs 目录下创建 bbs_nginx.conf 文件并写入以下代码:
1
2
3
|
location /{
rewrite ^(.*)/equip(d+).html$ $1/index.php?m=content&c=index&a=lists&catid=$2 last;
}
|
然后在上面的代码后面加上如下代码:
1
|
include /home/www/bbs/bbs_nginx.conf;
|
这样网站根目录中的 bbs_nginx.conf 伪静态规则,即可实现单独管理。
下面是一个实例:
1. 在使用.htaccess 文件的目录下新建一个.htaccess 文件,如下面一个 Discuz 论坛目录:
1
|
vim /var/www/html/jb51/bbs/.htaccess
|
2. 在里面输入规则,我这里输入 Discuz 的伪静态规则(这里仅增加 Discuz 的伪静态规则):
1
2
3
4
5
6
7
8
|
# nginx rewrite rule
rewrite ^(.*)/archiver/((fid|tid)-[w-]+.html)$ $1/archiver/index.php?$2 last;
rewrite ^(.*)/forum-([0-9]+)-([0-9]+).html$ $1/forumdisplay.php?fid=$2&page=$3 last;
rewrite ^(.*)/thread-([0-9]+)-([0-9]+)-([0-9]+).html$ $1/viewthread.php?tid=$2&extra=page%3D$4&page=$3 last;
rewrite ^(.*)/profile-(username|uid)-(.+).html$ $1/viewpro.php?$2=$3 last;
rewrite ^(.*)/space-(username|uid)-(.+).html$ $1/space.php?$2=$3 last;
rewrite ^(.*)/tag-(.+).html$ $1/tag.php?name=$2 last;
# end nginx rewrite rule
|
wq 保存退出。
3. 修改 nginx 配置文件:
1
|
vim /etc/nginx/nginx.conf
|
4. 在需要添加伪静态的虚拟主机的 server{}中引入.htaccess 文件:
1
|
include /var/www/html/jb51/bbs/.htaccess; (备注:把路径改成你.htaccess文件的具体位置)
|
wq 保存退出。
5. 重新加载 nginx 配置文件:
1
|
/etc/init.d/nginx reload
|
Nginx 常用 Rewrite 伪静态规则:
伪静态规则是我们做伪静态的一个非常重的参数了,如果我们能理解得越多就可以快速的写出最优的伪静态代码了,下面给大家整理了一些例子,希望能给你有帮助。
本日志内容来自互联网和平日使用经验,整理一下方便日后参考。 正则表达式匹配,其中:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
* ~ 为区分大小写匹配
* ~* 为不区分大小写匹配
* !~和!~*分别为区分大小写不匹配及不区分大小写不匹配
文件及目录匹配,其中:
* -f和!-f用来判断是否存在文件
* -d和!-d用来判断是否存在目录
* -e和!-e用来判断是否存在文件或目录
* -x和!-x用来判断文件是否可执行
flag标记有:
* last 相当于Apache里的[L]标记,表示完成rewrite
* break 终止匹配, 不再匹配后面的规则
* redirect 返回302临时重定向 地址栏会显示跳转后的地址
* permanent 返回301永久重定向 地址栏会显示跳转后的地址
|
一些可用的全局变量有,可以用做条件判断(待补全)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
$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
|
结合 QeePHP 的例子
1
2
3
4
5
|
if (!-d $request_filename) {
rewrite ^/([a-z-A-Z]+)/([a-z-A-Z]+)/?(.*)$ /index.php?namespace=user&controller=$1&action=$2&$3 last;
rewrite ^/([a-z-A-Z]+)/?$ /index.php?namespace=user&controller=$1 last;
break;
|
多目录转成参数
1
2
3
4
5
|
abc.domian.com/sort/2 => abc.domian.com/index.php?act=sort&name=abc&id=2
if ($host ~* (.*)/.domain/.com) {
set $sub_name $1;
rewrite ^/sort//(/d+)//?$ /index.php?act=sort&cid=$sub_name&id=$1 last;
}
|
目录对换
1
2
3
4
5
6
|
/123456/xxxx -> /xxxx?id=123456
rewrite ^/(/d+)/(.+)/ /$2?id=$1 last;
例如下面设定nginx在用户使用ie的使用重定向到/nginx-ie目录下:
if ($http_user_agent ~ MSIE) {
rewrite ^(.*)$ /nginx-ie/$1 break;
}
|
目录自动加“/”
1
2
3
4
|
if (-d $request_filename){
rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
}
|
禁止 htaccess
1
2
3
4
|
location ~//.ht {
deny all;
}
|
禁止多个目录
1
2
3
4
5
|
location ~ ^/(cron|templates)/ {
deny all;
break;
}
|
禁止以/data 开头的文件 可以禁止/data/下多级目录下.log.txt 等请求;
1
2
3
|
location ~ ^/data {
deny all;
}
|
禁止单个目录 不能禁止.log.txt 能请求
1
2
3
|
location /searchword/cron/ {
deny all;
}
|
伪静态是一种可以把文件后缀改成任何可能的一种方法,如果我想把 php 文件伪静态成 html 文件,这种相当简单的,下面来介绍 nginx 伪静态配置方法有需要了解的朋友可参考。