四、Nginx伪静态配置和常用Rewrite伪静态规则
1. nginx防盗链
location ~* \.(gif|jpg|png)$ {
valid_referers none blocked http://kevin.com:81; #允许访问的来源,即所有来自http://kevin.com:81的gif|jpg|png结尾的文件
if ($invalid_referer) {
rewrite ^/ http://kevin.com:81/c2_.html;
#return 403;
}
}
2. 伪静态重写
location / {
rewrite c(\d+)_(.*).html /index.php?c=user&m=index&id=$1&title=$2 last;
root /usr/share/nginx/html/sta;
index index.html index.htm index.php;
}
******** 对于生成大量的大量html文件,推荐使用rsync工具进行批量删除
1) 建立一个空目录
# mkdir -p /root/kevin/tmp/
2) 确认需要清空的目标目录(即需要删除的大量html文件所在的目录),比如/root/kevin/tmp1/
3)使用rsync同步删除(注意目录后面的“/”),整体效率会快一个数量级的样子。
# rsync --delete-before -a -H /root/kevin/tmp/ /root/kevin/tmp1/
选项说明:
–delete-before 接收者在传输之前进行删除操作
–progress 在传输时显示传输过程
-a 归档模式,表示以递归方式传输文件,并保持所有文件属性
-H 保持硬连接的文件
-v 详细输出模式
-stats 给出某些文件的传输状态
如下操作可以把某个超大文件删掉,比如删除/mnt/ops/web.html文件
1)建立空文件/mnt/ops/html.txt
2)# rsync --delete-before -a -H -v --progress --stats /mnt/ops/html.txt /mnt/ops/web.html
3)这样置为空后就可以快速删掉
原理:
把文件系统的目录与书籍的目录做类比,rm删除内容时,将目录的每一个条目逐个删除(unlink),需要循环重复操作很多次;
rsync删除内容时,建立好新的空目录,替换掉老目录,基本没开销。
测试大文件删除
1)生成文件
# for i in $(seq 1 500000); do echo test >>/opt/test/$i.txt; done
测试结果:
10万文件rm删除第一次11s左右,第二次9s左右;rsync测试两次9s左右
50万文件rm删除报错;rsync测试两次一个5分左右,一个4分左右
rm删除命令
# time rm -f /opt/test/*.txt
rsync命令删除
# mkdir /opt/test1
# rsync --delete-before -a -H -v --progress --stats /opt/test1/ /opt/test/
******** nginx php thinkphp5 伪静态配置示例
server {
listen 80;
server_name 127.0.0.1 localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
if (!-e $request_filename) {
#一级目录
#rewrite ^/(.*)$ /index.php?s=$1 last;
#二级目录
rewrite ^/TWcloud/public/(.*)$ /TWcloud/public/index.php?s=$1 last;
break;
}
root /Applications/MxSrvs/www;
index index.html index.htm index.php;
}
location ~ \.php { #去掉$
root /Applications/MxSrvs/www;
fastcgi_pass 127.0.0.1:10080;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(.*)$; #增加这一句
fastcgi_param PATH_INFO $fastcgi_path_info; #增加这一句
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
******** thinkphp 5.0的nginx伪静态规则配置示例 [踩过坑点,经测试实现]
server {
listen 80;
server_name www.kevin.com;
root /data/www/web;
location / {
index index.html index.htm index.php default.php;
#重点就是加入下面这个if
if (!-e $request_filename){
rewrite ^(.*)$ /index.php?s=$1 last; break;
}
}
location ~ .php(.*)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.html;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_split_path_info ^(.+.php)(.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
include fastcgi_params;
}
}
******** 先来看一个nginx伪静态配置示例
场景一:
把
http://www.abc.com/index.php/front/index/index
重写成
http://www.abc.com/a.html
场景二:
把下面带参数的第1、2个url解析成第3个url
1.http://www.abc.com/index.php/front/index/parse/name/yangxignyi/age/18
2.http://www.abc.com/index.php/front/index/parse?name=yangxignyi&age=18
3.http://www.abc.com/parse-yangxignyi-18.html
服务器配置文件:
server{
listen 80;
server_name www.abc.com;
root /data/www/web;
location / {
index index.php index.htm /public/index.html;
autoindex off;
include abc.conf;
#rewrite a.html /index.php/front/index/index last;
}
location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}
伪静态配置文件可以直接写在location /{} 里面的,不推荐这样做!!
建议新增加个rewrite.conf文件,写伪静态文件会好点,include 引入进来就行了,这样可以在rewrite.conf里面写n多配置
location / {
index index.php index.htm /public/index.html;
autoindex off;
include rewrite.conf;
#rewrite a.html /index.php/front/index/index last;
}
#rewrite.conf (这个文件自己创建就行了,文件内容写规则),伪静态配置如下:
#场景一的规则
#http://www.abc.com/index.php/front/index/index
rewrite a.html /index.php/front/index/index last;
#场景二的规则
#1.http://www.abc.com/index.php/front/index/parse/name/yangxignyi/age/18
#2.http://www.abc.com/index.php/front/index/parse?name=yangxignyi&age=18
#3.http://www.abc.com/parse-yangxingyi-18.html
rewrite parse-(\w+)-(\d+).html /index.php/front/index/parse/name/$1/age/$2 last;
如上配置中的\w是数字字母下划线的意思,\d是数字的意思 +是最少一个{1,} 1到无穷大{1,3} 这样是1-3位数。
3. nginx伪静态的Rewrite重写的正则匹配规则
正则表达式匹配 :
~ 为区分大小写的匹配
~* 不区分大小写的匹配(匹配firefox的正则同时匹配FireFox)
!~ 区分大小写的不匹配
!~* 不区分大小写的不匹配
\ 将后面接着的字符标记为一个特殊字符或者一个原义字符或一个向后引用
. 匹配除换行符以外的任意字符,即匹配除"\n"之外的所有单个字符
\w 匹配字母或数字或下划线或汉字
\s 匹配任意的空白符
\d 匹配数字
\b 匹配单词的开始或结束
^ 匹配字符串的开始,即匹配输入字符串的起始位置
$ 匹配字符串的结束,即匹配输入字符串的结束位置
* 重复零次或更多次,即匹配前面的字符零次或者多次
+ 重复一次或更多次,即匹配前面字符串一次或者多次
? 重复零次或一次,即匹配前面字符串的零次或者一次
{n} 重复n次
{n,} 重复n次或更多次
{n,m} 重复n到m次
*? 重复任意次,但尽可能少重复
+? 重复1次或更多次,但尽可能少重复
?? 重复0次或1次,但尽可能少重复
{n,m}? 重复n到m次,但尽可能少重复
{n,}? 重复n次以上,但尽可能少重复
\W 匹配任意不是字母,数字,下划线,汉字的字符
\S 匹配任意不是空白符的字符
\D 匹配任意非数字的字符
\B 匹配不是单词开头或结束的位置
[^x] 匹配除了x以外的任意字符
文件及目录匹配,其中:
-f和!-f 用来判断是否存在文件
-d和!-d 用来判断是否存在目录
-e和!-e 用来判断是否存在文件或目录
-x和!-x 用来判断文件是否可执行
flag标记有:
last 相当于Apache里的[L]标记,表示完成rewrite。即本条规则匹配完成后继续向下匹配新的location URI规则
break 终止匹配, 不再匹配后面的规则。即本条规则匹配完成后终止,不在匹配任何规则
redirect 返回302临时重定向 地址栏会显示跳转后的地址
permanent 返回301永久重定向 地址栏会显示跳转后的地址
$args 此变量与请求行中的参数相等
$content_length 等于请求行的“Content_Length”的值。
$content_type 等同与请求头部的”Content_Type”的值
$document_root 等同于当前请求的root指令指定的值
$document_uri 与 $uri 一样
$host 与请求头部中“Host”行指定的值或是request到达的server的名字(没有Host行)一样
$limit_rate 允许限制的连接速率
$request_method 等同于request的method,通常是“GET”或“POST”
$remote_addr 客户端ip
$remote_port 客户端port
$remote_user 等同于用户名,由ngx_http_auth_basic_module认证
$request_filename 当前请求的文件的路径名,由root或alias和URI request组合而成
$request_body_file
$request_uri 含有参数的完整的初始URI
$query_string 与 $args一样
$server_protocol 等同于request的协议,使用“HTTP/1.0”或“HTTP/1.1”
$server_addr request 到达的server的ip,一般获得此变量的值的目的是进行系统调用。为了避免系统调用,有必要在listen指令中指明ip,并使用bind参数。
$server_name 请求到达的服务器名
$server_port 请求到达的服务器的端口号
$uri 等同于当前request中的URI,可不同于初始值,例如内部重定向时或使用index
总的来说,location中的rewrite,不写last和break,那么流程就是依次执行这些rewrite
1. rewrite btrak:表示url重写后,直接使用当前资源,不再执行location里余下的语句,完成本次请求后,地址url不变。
2. rewrite last:表示url重写后,马上发起一个新的请求,再次进入server块,重试location匹配,超过10次匹配不到就报500错误,地址栏url不变。
3. rewrite redirect:表示返回302临时重定向,地址栏显示重定向后的url,爬虫不会重写url,因为是临时。
4. rewrite permanent:表示返回301永久重定向,地址栏显示重定向后的url,爬虫重写url。
rewrite重写跳转的应用场景
1. 调整用户浏览的URL,看起来规范
2. 为了让搜索引擎收录网站内容,让用户体验更好
3. 网站更换新域名后
4. 根据特殊的变量、目录、客户端信息进行跳转
******** 常用301跳转的rewrite重写示例
1)301跳转
之前常用起别名的方式做到了不同地址访问同一个虚拟主机的资源,现在可以用一个更好的方式做到这一点,那就是跳转的方法来实现。
比如www.kevin.com虚拟主机为例子,修改配置文件如下:
server { # 添加个server区块做跳转
listen 80;
server_name kevin.com;
rewrite ^/(.*) http://www.kevin.com/$1 permanent;
}
server {
listen 80;
server_name www.kevin.com;
location / {
root html/brian;
index index.html index.htm;
}
access_log logs/brian.log main gzip buffer=128k flush=5s;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
这样,当访问域名http://kevin.com时,就301永久跳转到http://www.kevin.com域名了,跳转后的地址是http://www.kevin.com
2)域名跳转:
不仅可以做相同虚拟主机的资源域名跳转,也能做不同虚拟主机的域名跳转,下面就跳转下当访问kevin.com域名的时候跳转到www.baidu.com的页面:
修改配置文件:
server { #添加个server区块做跳转
listen 80;
server_name kevin.com;
rewrite ^/(.*) http://www.kevin.com/$1 permanent;
}
server {
listen 80;
server_name www.kevin.com;
location / {
root html/brian;
index index.html index.htm;
}
access_log logs/brian.log main gzip buffer=128k flush=5s;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
这样,当访问http://kevin.com时,就跳转到了http://www.baidu.com,跳转后的地址是http://www.baidu.com
4. nginx伪静态常用配置示例
nginx里使用伪静态是直接在nginx.conf 中写规则的,并不需要像apache要开启写模块(mod_rewrite)才能进行伪静态。
nginx只需要打开nginx.conf配置文件,在server里面写需要的规则即可。
1)配置案例1
server {
listen 80;
server_name www.kevin.com;
index index.html index.htm index.php;
rewrite ^/wangla.html$ http://www.baidu.com/ permanent;
rewrite ^/(\d+).html$ http://www.qq.com/ permanent;
rewrite ^/(\w+).html$ http://wd.gyyx.cn/index_wd_v5.htm permanent;
}
以上添加了几条重写规则
访问www.kevin.com/wangla.html跳转到百度
访问www.kevin.com/纯数字至少一个数字.html跳转到QQ官网
访问www.kevin.com/匹配字母或数字或下划线组合.html 跳转到问道官网。
2)配置案例2
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文件并写入以下代码:
location /{
rewrite ^(.*)/equip(d+).html$ $1/index.php?m=content&c=index&a=lists&catid=$2 last;
}
然后在上面的代码后面加上如下代码:
include /home/www/bbs/bbs_nginx.conf;
这样网站根目录中的bbs_nginx.conf伪静态规则,即可实现单独管理。
下面是一个实例:
在使用.htaccess文件的目录下新建一个.htaccess文件,如下面一个Discuz论坛目录:
# vim /var/www/html/jb51/bbs/.htaccess
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;
接着修改nginx配置文件:
# vim /etc/nginx/nginx.conf
在需要添加伪静态的虚拟主机的server{}中引入.htaccess文件:
include /var/www/html/jb51/bbs/.htaccess;
最后重新加载nginx配置文件:
# /etc/init.d/nginx reload
3)下面是一些rewrite配置集锦,供运维参考:
=======================================================================================
rewrite "^/(.{6})(\d{3})(.+)/php/" http://www.kevin.com/qq$2.apk break;
中间用到了{6}指前面的字符得复6次
结合PHP的例子
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;
多目录转成参数。
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;
}
目录对换
/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;
}
目录自动加"/"
if (-d $request_filename){
rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
}
禁止htaccess
location ~/\.ht {
deny all;
}
禁止多个目录
location ~ ^/(cron|templates)/ {
deny all;
break;
}
禁止以/data开头的文件
可以禁止/data/下多级目录下.log.txt等请求;
location ~ ^/data {
deny all;
}
禁止单个目录
不能禁止.log.txt能请求
location /searchword/cron/ {
deny all;
}
禁止单个文件
location ~ /data/sql/data.sql {
deny all;
}
给favicon.ico和robots.txt设置过期时间;
这里为favicon.ico为99 天,robots.txt为7天并不记录404错误日志
location ~(favicon.ico) {
log_not_found off;
expires 99d;
break;
}
location ~(robots.txt) {
log_not_found off;
expires 7d;
break;
}
设定某个文件的过期时间;这里为600秒,并不记录访问日志
location ^~ /html/scripts/loadhead_1.js {
access_log off;
root /opt/lampp/htdocs/web;
expires 600;
break;
}
文件反盗链并设置过期时间
这里的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访问网站,并加上密码
root /opt/htdocs/www;
allow 218.197.16.14;
allow 22.133.11.22;
allow 21.112.69.14;
deny all;
auth_basic "C1G_ADMIN";
auth_basic_user_file htpasswd;
将多级目录下的文件转成一个文件,增强seo效果
/job-123-456-789.html 指向/job/123/456/789.html
配置如下:
rewrite ^/job-([0-9]+)-([0-9]+)-([0-9]+)\.html$ /job/$1/$2/jobshow_$3.html last;
--------------------------------------------------------------------------------
将根目录下某个文件夹指向2级目录
如/shanghaijob/ 指向 /area/shanghai/
如果你将last改成permanent,那么浏览器地址栏显是 /location/shanghai/
配置如下:
rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2 last;
上面例子有个问题是访问/shanghai 时将不会匹配
rewrite ^/([0-9a-z]+)job$ /area/$1/ last;
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不是的,所以没有效果。
if (-d $request_filename){
rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
}
知道原因后就好办了,手动跳转吧
rewrite ^/([0-9a-z]+)job$ /$1job/ permanent;
rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2 last;
--------------------------------------------------------------------------------
文件和目录不存在的时候重定向:
if (!-e $request_filename) {
proxy_pass http://127.0.0.1/;
}
域名跳转
server {
listen 80;
server_name jump.c1gstudio.com;
index index.html index.htm index.php;
root /opt/lampp/htdocs/www;
rewrite ^/ http://www.c1gstudio.com/;
access_log off;
}
多域名转向
server_name http://www.c1gstudio.com/ http://www.c1gstudio.net/;
index index.html index.htm index.php;
root /opt/lampp/htdocs;
if ($host ~ "c1gstudio\.net") {
rewrite ^(.*) http://www.c1gstudio.com$1/ permanent;
}
三级域名跳转
if ($http_host ~* "^(.*)\.i\.c1gstudio\.com$") {
rewrite ^(.*) http://top.yingjiesheng.com$1/;
break;
}
域名镜向
server
{
listen 80;
server_name mirror.c1gstudio.com;
index index.html index.htm index.php;
root /opt/lampp/htdocs/www;
rewrite ^/(.*) http://www.c1gstudio.com/$1 last;
access_log off;
}
某个子目录作镜向
location ^~ /zhaopinhui {
rewrite ^.+ http://zph.c1gstudio.com/ last;
break;
}
discuz ucenter home (uchome) rewrite伪静态配置
rewrite ^/(space|network)-(.+)\.html$ /$1.php?rewrite=$2 last;
rewrite ^/(space|network)\.html$ /$1.php last;
rewrite ^/([0-9]+)$ /space.php?uid=$1 last;
discuz 7 rewrite伪静态配置
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\=$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;
给discuz某版块单独配置域名
server_name bbs.c1gstudio.com news.c1gstudio.com;
location = / {
if ($http_host ~ news\.c1gstudio.com$) {
rewrite ^.+ http://news.c1gstudio.com/forum-831-1.html last;
break;
}
}
discuz ucenter 头像 rewrite 优化
location ^~ /ucenter {
location ~ .*\.php?$
{
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fcgi.conf;
}
location /ucenter/data/avatar {
log_not_found off;
access_log off;
location ~ /(.*)_big\.jpg$ {
error_page 404 /ucenter/images/noavatar_big.gif;
}
location ~ /(.*)_middle\.jpg$ {
error_page 404 /ucenter/images/noavatar_middle.gif;
}
location ~ /(.*)_small\.jpg$ {
error_page 404 /ucenter/images/noavatar_small.gif;
}
expires 300;
break;
}
}
jspace rewrite伪静态配置
location ~ .*\.php?$
{
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fcgi.conf;
}
location ~* ^/index.php/
{
rewrite ^/index.php/(.*) /index.php?$1 break;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fcgi.conf;
}
Nginx伪静态配置示例2
公司域名bo.kevin.com下有多个子项目目录结构大致是bo.kevin.com/own/xys |bo.kevin.com/2017/abc |bo.kevin.com/2018/def有二级也有三级目录,
应开发需求某项目访问地址是:bo.kevin.com/own/xys/index.php/admin/login 需要把index.php隐藏为bo.kevin.com/own/xys/index/admin/login进行访问。
设定以下三种场景:
场景一
将 http://www.abc.com/index.php/front/index/index
重写成 http://www.abc.com/a.html
场景二
将 http://www.abc.com/index.php/front/index/parse?name=itboy&age=18
重写成 http://www.abc.com/parse-itboy-18.html
场景三(同一域名下,需要匹配随时新增的二三级目录,并隐藏index.php的.php后缀)
将 http://bo.kevin.com/own/xys/index.php/admin/login 以及 http://bo.kevin.com/2018/gdhp/index.php/login
重写成 http://bo.kevin.com/own/xys/index/admin/login 以及 http://bo.kevin.com/2018/gdhp
建议在nginx/conf目录下新建rewrite.conf配置文件中编写伪静态规则,写完后在域名.conf文件中插入rewrite.conf文件即可(用include rewrite.conf插入)。
nginx配置文件如下:
server
{
listen 80;
server_name bo.kevin.com;
index index.html index.php index.htm index.php default.html default.htm default.php;
root /var/www/apps/bo.kevin.com;
include rewrite.conf;
include none.conf;
error_page 502 /502.html;
include enable-php-pathinfo.conf;
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
location ~ /\.
{
deny all;
}
access_log /var/www/wwwlogs/bo.kevin.com.log;
error_log /var/www/wwwlogs/error.bo.kevin.com.log;
}
rewrite.conf文件内容如下:
location /{
#场景一
#http://www.abc.com/index.php/front/index/index 变成 http://www.abc.com/a.html
rewrite a.html /index.php/front/index/index last;
#场景二
#http://www.abc.com/index.php/front/index/parse?name=itboy&age=18 变成 http://www.abc.com/parse-itboy-18.html
rewrite parse-(\w+)-(\d+).html /index.php/front/index/parse/name/$1/age/$2 last;
#场景三
#http://bo.kevin.com/own/xys/index.php/admin/login 以及 http://bo.kevin.com/2018/gdhp/index.php/login
#变成 http://bo.kevin.com/own/xys/index/admin/login 以及 http://bo.kevin.com/2018/gdhp/index/login
rewrite ^/(\w+)/(\w+)/(.*)$ /$1/$2/index.php?s=$3 last; #针对own目录伪静态规则,$1对应(\w+)部分,$2对应第二个(\w+)部分,$3对应(.*)部分,$代表直至最后
rewrite ^/(\d+)/(\w+)/(.*)$ /$1/$2/index.php?s=$3 last; #针对后期的2018下的子项目伪静态规则
}
说明:
其实都是很简单的对号入座原理而已,拿场景二来说明,第一个正则(\w+)对应的就是$1,第二个正则(\d+)对应的就是$2,
另外,\w是数字字母下划线的意思,\d是数字的意思 +是最少一个{1,} 1到无穷大{1,3} 这样是1-3位数。
Nginx上支持.htaccess伪静态的配置示例
# vim /usr/local/nginx/conf/vhost/web.conf
........
include /var/www/web/.htaccess
# vim /var/www/web/.htaccess
rewrite ^/show-([0-9]+)-([0-9]+)\.html$ /index.php?action=show&id=$1&page=$2;
rewrite ^/category-([0-9]+)-([0-9]+)\.html$ /index.php?action=index&cid=$1&page=$2;
rewrite ^/archives-([0-9]+)-([0-9]+)\.html$ /index.php?action=index&setdate=$1&page=$2;
rewrite ^/(archives|search|reg|login|index|links)\.html$ /index.php?action=$1;
rewrite ^/(comments|tagslist|trackbacks|index)-([0-9]+)\.html$ /index.php?action=$1&page=$2;
if ($host != 'www.shibo.com' ) {
rewrite ^/(.*)$ http://www.shibo.com/$1 permanent;
}
error_page 404 http://www.shibo.com/;
# /usr/local/nginx/sbin/nginx -s reload
nginx伪静态配置示例3 [去除框架的Index.php]
在nginx.conf中Location/{}中加上下面这个if判断就可以了:
location /{
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=$1 last; break;
}
}
===============================================================================
如下面一个配置
if (!-e $request_filename) {
rewrite ^/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)\.html /index.php?m=$1&c=$2&a=$3&$4=$5&$6=$7 last;
break;
}
Nginx 伪静态配置示例4
访问 http://www.kevin.com/sort/15.html -> http://www.kevin.com/1.php?id=15
location / {
root /usr/share/nginx/html/1;
index index.html;
rewrite /sort/(.*)\.html /1.php?id=$1 last;
}
================================================================================
再如下面一个配置
server {
listen 80 default_server;
server_name _;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
rewrite ^(.*)list-([0-9]+)-([0-9]+)\.html$ $1list.php?page=$2&id=$3;
}
}
策略:RewriteRule ^(.*)list-([0-9]+)-([0-9]+)\.html$ $1list.php?page=$2&id=$3
请求路径:http://www.abc.com/list-123-456.html
以上策略分成两段:
第一段是使用正则表达式去匹配请求访问的路径,第二段是将匹配后的参数转化为真实访问的路径。
策略执行时:^(.*)list-([0-9]+)-([0-9]+)\.html$ 与 /list-123-456.html 这个字符串进行匹配:
^和$字符分别代表了匹配输入字符串的开始和结束;
()中的匹配到的内容会被按顺序分配到变量$1 $2 $3中;
.*匹配任意字符串,且长度从0个到多个,故$1值为/;
[0-9]+匹配字符0-9,长度1个到多个,故$2和$3分别是123和456;
所以最后真实访问的动态地址为 /list.php?page=123&id=456
================================================================================
再如下面一个配置
1)/a/b?c=d => index.php?_a=a&_m=b&c=d
2)/xxx/detail-yyy.html => index.php?_a=xxx&_m=detail&id=yyy
配置如下:
server {
listen 80;
server_name my.xh5.com;
location / {
root /mnt/hgfs/web/my.xueh5.com/src/;
index index.html index.htm index.php;
if ($args ~ "^(.*)$"){
set $rule_0 1$rule_0;
set $bref_1 $1;
}
if ($rule_0 = "1"){
rewrite ^([0-9a-zA-Z]*)/detail-([0-9]*)\.html$ /index.php?_a=$1&_m=detail&id=$2&$bref_1 last;
rewrite ^/([0-9a-zA-Z]*)/([0-9a-zA-Z.]*)$ /index.php?_a=$1&_m=$2&$bref_1 last;
rewrite ^/([0-9a-zA-Z]+)$ /index.php?_a=$1&_m=index&$bref_1 last;
rewrite ^/$ /index.php?_a=index&_m=index&$bref_1 last;
}
}
location ~ \.php$ {
root /mnt/hgfs/web/my.xueh5.com/src/;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
5. Nginx伪静态常用规则配置总结
1)WordPress伪静态
if (-f $request_filename/index.html){
rewrite (.) $1/index.html break;
}
if (-f $request_filename/index.php){
rewrite (.) $1/index.php;
}
if (!-f $request_filename){
rewrite (.*) /index.php;
}
2)PHPCMS伪静态
rewrite ^/caipu-([0-9]+)-([0-9]+)-([0-9]+).html /index.php?m=content&c=index&a=show&catid=$1&id=$2&page=$3 last;
rewrite ^/content-([0-9]+)-([0-9]+)-([0-9]+).html /index.php?m=content&c=index&a=show&catid=$1&id=$2&page=$3 last;
rewrite ^/list-([0-9]+)-([0-9]+).html /index.php?m=content&c=index&a=lists&catid=$1&page=$2 last;
rewrite ^/tag-([^.])-([0-9]+)-([0-9]+).html /index.php?m=content&c=tag&catid=$2&tag=$1&page=$3 last;
rewrite ^/comment-([0-9]+)-([0-9]+)-([0-9]+).html /index.php?m=comment&c=index&a=init&commentid=content_$1-$2-$3 last;
rewrite ^/([^.]).html /index.php?m=member&c=index&a=$1 last;
3)DEDECMS伪静态
rewrite "^/index.html$" /index.php last;
rewrite "^/list-([0-9]+).html$" /plus/list.php?tid=$1 last;
rewrite "^/list-([0-9]+)-([0-9]+)-([0-9]+).html$" /plus/list.php?tid=$1&totalresult=$2&PageNo=$3 last;
rewrite "^/view-([0-9]+)-1.html$" /plus/view.php?arcID=$1 last;
rewrite "^/view-([0-9]+)-([0-9]+).html$" /plus/view.php?aid=$1&pageno=$2 last;
rewrite "^/tags.html$" /tags.php last;
rewrite "^/tag-([0-9]+)-([0-9]+).html$" /tags.php?/$1/$2/ last;
4)Discuz7伪静态
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;
5)DiscuzX伪静态
rewrite ^([^.])/topic-(.+).html$ $1/portal.php?mod=topic&topic=$2 last;
rewrite ^([^.])/article-([0-9]+)-([0-9]+).html$ $1/portal.php?mod=view&aid=$2&page=$3 last;
rewrite ^([^.])/forum-(\w+)-([0-9]+).html$ $1/forum.php?mod=forumdisplay&fid=$2&page=$3 last;
rewrite ^([^.])/thread-([0-9]+)-([0-9]+)-([0-9]+).html$ $1/forum.php?mod=viewthread&tid=$2&extra=page%3D$4&page=$3 last;
rewrite ^([^.])/group-([0-9]+)-([0-9]+).html$ $1/forum.php?mod=group&fid=$2&page=$3 last;
rewrite ^([^.])/space-(username|uid)-(.+).html$ $1/home.php?mod=space&$2=$3 last;
rewrite ^([^.]*)/([a-z]+)-(.+).html$ $1/$2.php?rewrite=$3 last;
if (!-e $request_filename) {
return 404;
}
6)ECSHOP伪静态
if (!-e $request_filename)
{
rewrite "^/index.html" /index.php last;
rewrite "^/category$" /index.php last;
rewrite "^/feed-c([0-9]+).xml$" /feed.php?cat=$1 last;
rewrite "^/feed-b([0-9]+).xml$" /feed.php?brand=$1 last;
rewrite "^/feed.xml$" /feed.php last;
rewrite "^/category-([0-9]+)-b([0-9]+)-min([0-9]+)-max([0-9]+)-attr([^-])-([0-9]+)-(.+)-([a-zA-Z]+)(.).html$" /category.php?id=$1&brand=$2&price_min=$3&price_max=$4&filter_attr=$5&page=$6&sort=$7&order=$8 last;
rewrite "^/category-([0-9]+)-b([0-9]+)-min([0-9]+)-max([0-9]+)-attr([^-])(.).html$" /category.php?id=$1&brand=$2&price_min=$3&price_max=$4&filter_attr=$5 last;
rewrite "^/category-([0-9]+)-b([0-9]+)-([0-9]+)-(.+)-([a-zA-Z]+)(.).html$" /category.php?id=$1&brand=$2&page=$3&sort=$4&order=$5 last;
rewrite "^/category-([0-9]+)-b([0-9]+)-([0-9]+)(.).html$" /category.php?id=$1&brand=$2&page=$3 last;
rewrite "^/category-([0-9]+)-b([0-9]+)(.).html$" /category.php?id=$1&brand=$2 last;
rewrite "^/category-([0-9]+)(.).html$" /category.php?id=$1 last;
rewrite "^/goods-([0-9]+)(.).html" /goods.php?id=$1 last;
rewrite "^/article_cat-([0-9]+)-([0-9]+)-(.+)-([a-zA-Z]+)(.).html$" /article_cat.php?id=$1&page=$2&sort=$3&order=$4 last;
rewrite "^/article_cat-([0-9]+)-([0-9]+)(.).html$" /article_cat.php?id=$1&page=$2 last;
rewrite "^/article_cat-([0-9]+)(.).html$" /article_cat.php?id=$1 last;
rewrite "^/article-([0-9]+)(.).html$" /article.php?id=$1 last;
rewrite "^/brand-([0-9]+)-c([0-9]+)-([0-9]+)-(.+)-([a-zA-Z]+).html" /brand.php?id=$1&cat=$2&page=$3&sort=$4&order=$5 last;
rewrite "^/brand-([0-9]+)-c([0-9]+)-([0-9]+)(.).html" /brand.php?id=$1&cat=$2&page=$3 last;
rewrite "^/brand-([0-9]+)-c([0-9]+)(.).html" /brand.php?id=$1&cat=$2 last;
rewrite "^/brand-([0-9]+)(.).html" /brand.php?id=$1 last;
rewrite "^/tag-(.).html" /search.php?keywords=$1 last;
rewrite "^/snatch-([0-9]+).html$" /snatch.php?id=$1 last;
rewrite "^/group_buy-([0-9]+).html$" /group_buy.php?act=view&id=$1 last;
rewrite "^/auction-([0-9]+).html$" /auction.php?act=view&id=$1 last;
rewrite "^/exchange-id([0-9]+)(.).html$" /exchange.php?id=$1&act=view last;
rewrite "^/exchange-([0-9]+)-min([0-9]+)-max([0-9]+)-([0-9]+)-(.+)-([a-zA-Z]+)(.).html$" /exchange.php?cat_id=$1&integral_min=$2&integral_max=$3&page=$4&sort=$5&order=$6 last;
rewrite ^/exchange-([0-9]+)-([0-9]+)-(.+)-([a-zA-Z]+)(.).html$" /exchange.php?cat_id=$1&page=$2&sort=$3&order=$4 last;
rewrite "^/exchange-([0-9]+)-([0-9]+)(.).html$" /exchange.php?cat_id=$1&page=$2 last;
rewrite "^/exchange-([0-9]+)(.).html$" /exchange.php?cat_id=$1 last;
}
7)PHPWind伪静态
rewrite ^(.)-htm-(.)$ $1.php?$2 last;
rewrite ^(.*)/simple/([a-z0-9_]+.html)$ $1/simple/index.php?$2 last;
8)SaBlog2.0伪静态
只带月份的归档
rewrite "^/date/([0-9]{6})/?([0-9]+)?/?$" /index.php?action=article&setdate=$1&page=$2 last;
无分类翻页
rewrite ^/page/([0-9]+)?/?$ /index.php?action=article&page=$1 last;
分类
rewrite ^/category/([0-9]+)/?([0-9]+)?/?$ /index.php?action=article&cid=$1&page=$2 last;
rewrite ^/category/([^/]+)/?([0-9]+)?/?$ /index.php?action=article&curl=$1&page=$2 last;
归档、高级搜索
rewrite ^/(archives|search|article|links)/?$ /index.php?action=$1 last;
全部评论、标签列表、引用列表 带分页
rewrite ^/(comments|tagslist|trackbacks|article)/?([0-9]+)?/?$ /index.php?action=$1&page=$2 last;
tags
rewrite ^/tag/([^/]+)/?([0-9]+)?/?$ /index.php?action=article&item=$1&page=$2 last;
文章
rewrite ^/archives/([0-9]+)/?([0-9]+)?/?$ /index.php?action=show&id=$1&page=$2 last;
RSS rewrite ^/rss/([0-9]+)?/?$ /rss.php?cid=$1 last;
rewrite ^/rss/([^/]+)/?$ /rss.php?url=$1 last;
用户 rewrite ^/uid/([0-9]+)/?([0-9]+)?/?$ /index.php?action=article&uid=$1&page=$2 last;
rewrite ^/user/([^/]+)/?([0-9]+)?/?$ /index.php?action=article&user=$1&page=$2 last;
地图文件
rewrite sitemap.xml sitemap.php last;
自定义链接
rewrite ^(.*)/([0-9a-zA-Z-_]+)/?([0-9]+)?/?$ $1/index.php?action=show&alias=$2&page=$3 last;
9)SHOPEX伪静态
if (!-e $request_filename) {
rewrite ^/(.+.(html|xml|json|htm|php|jsp|asp|shtml))$ /index.php?$1 last;
}
10)Typecho伪静态
if (-f $request_filename/index.html){
rewrite (.) $1/index.html break;
}
if (-f $request_filename/index.php){
rewrite (.) $1/index.php;
}
if (!-f $request_filename){
rewrite (.*) /index.php;
}
Nginx中的rewrite只能放在server{},location{},if{}中,并且只能对域名后边的除去传递的参数外的字符串起作用。
6. rewrite伪静态重写的执行顺序 [如下,前面的执行优先级依次大于后面的]
location =
location 完整路径
location ^~ 路径
location ~,或者location~* 正则顺序
location 部分起始路径
location /
- 上一篇:没有了!
- 下一篇:详解 nginx 伪静态配置(三)
请立即点击咨询我们或拨打咨询热线: 13718557531,我们会详细为你一一解答你心中的疑难。项目经理在线