一、什么是伪静态
伪静态即是网站本身是动态网页如.php、.asp、.aspx等格式动态网页有时这类动态网页还跟"?"加参数来读取数据库内不同资料,伪静态就是做url重写操作(即rewrite)。很典型的案例即是discuz论坛系统,后台就有一个设置伪静态功能,开启伪静态后,动态网页即被转换重写成静态网页类型页面,通过浏览器访问地址和真的静态页面没区别。但是记住:做伪静态的前提就是服务器要支持伪静态重写URL Rewrite功能。
考虑搜索引擎优化(即SEO),将动态网页通过服务器处理成静态页面,如www.kevin.com/jk/fd.php?=12这样的动态网页处理成www.kevin.com/jk-fd-12.html这样格式静态页面,常见的论坛帖子页面,都是经过伪静态处理成静态页面格式html页面。由于网站所用的程序语言不易被发现,经过重写来伪静态来将动态网页的程序后缀变为html的静态页面格式。伪静态是一种可以把文件后缀改成任何可能的一种方法,比如如果想把php文件伪静态成html文件,这种配置相当简单的,后面会提到相应配置。
二、真静态、伪静态优点和缺点
真静态(html)优点;1)减少服务器对数据响应的负荷;2)加载不用调动数据库,响应速度快。
真静态缺点:1)维护不方便,每次都要手动生成;2)空间占用比较大,容易造成磁盘压力;3)生成的文件多,服务器对html文件的响应负担也较重。
伪静态(url重写)优点:1)可以方便的实现对化化引擎的优化,并且比生成静态更加方便;2)占空间比较小;3)首页每天都自动变化,不用维护。网站首页一般都有热点排行之类的,你可以设为,24小时排行,一周排行,再加上最新文章,最新点评等。这样首页天天是有变化的;4)便于广告的轮显。比如:你可以把 art1234.aspx,这个虚成n个页,如art_1234.aspx,news_1234.aspx,top_1234.aspx,在不同的页面放 不同的广告。总之是动态的,就可以随意动。
伪静态缺点:1)如果流量稍大一些使用伪静态就出现CPU使用超负荷,因为伪静态是用正则判断而不是真实地址,分辨到底显示哪个页面的责任也由直接指定转由CPU来判断了,所以CPU占有量的上升,确实是伪静态最大的弊病;2)伪静态效率不如生成html的,因为它不是真正意义上的静态页,所以每次请求都是要去读取数据库的信息(这个可以用缓存技术来补偿一下)。
三、真静态、伪静态原理与实现方案
1、伪静态
伪静态是相对于真静态而言的,就是把一些asp,php等结尾url通过apche或nginx的重写规则,变成以html一类的静态页面形式。伪静态不是真正的静态,它和动态地址一样要读取数据库。伪静态最主要的作用就是利于seo,百度spider(百度蜘蛛)喜欢抓取静态页面,可容易使百度spider陷入死循环;并发量高的时候会加大服务器的压力,所以用的时候要注意。
伪静态就是利用apche,nginx重写规则,对url地址重写实现的!伪静态实现原理:
1) Apache伪静态前提是要打开apache的重写模块 (即打开"LoadModule rewrite_module modules/mod_rewrite.so"这一行);
2) Nginx默认就支持伪静态;
伪静态有两种配置方式
1) 在配置虚拟主机的时候设置;
2) 在web根目录下创建一个.htaccess文件,在这个文件里面配置;
2、真静态
在网站设计中,纯粹HTML(标准通用标记语言下的一个应用)格式的网页通常被称为"静态网页",静态网页是标准的HTML文件,它的文件扩展名是.htm、.html,可以包含文本、图像、声音、FLASH动画、客户端脚本和ActiveX控件及JAVA小程序等。静态网页是网站建设的基础,早期的网站一般都是由静态网页制作的。静态网页是相对于动态网页而言,是指没有后台数据库、不含程序和不可交互的网页。静态网页相对更新起来比较麻烦,适用于一般更新较少的展示型网站。容易误解的是静态页面都是htm这类页面,实际上静态也不是完全静态,它也可以出现各种动态的效果,如GIF格式的动画、FLASH、滚动字幕等。
大型web项目优化中经常会考虑到使用真静态,这样在访问量大的时候,可以减少cpu的压力,但是会生成大量的文件占用网站的磁盘空间,可以写个php的脚本或用linux的计划任务进行删除。在用真静态的时候有的时候需要用到局部的动态化。
真静态实现方法
1)利用PHP模板生成静态页面;
2)使用PHP文件读写功能生成静态页面;
3)使用PHP输出控制函数缓存机制生成静态页面;
4)使用nosql从内存中读取内容(其实这个已经不算静态化了而是缓存);
memcached是键值一一对应,key默认最大不能超过128个字节,value默认大小是1M,因此1M大小满足大多数网页大小的存储。
真静态和伪静态的区别
1)是不是一个真正静态页面;
2)有没有和数据库或后台程序进行交互;
3)它们的应用场景和解决的问题不同;
4)用javascript:alert(document.lastModified)来判断是真静态还是伪静态;
真静态在apache和nginx上的区别与否
1)真静态在nginx上的运行速度比apache运行速度快;
2)nginx处理静态文件对于apache来说消耗的内存少;
伪静态在apache和nginx上的区别与否
1)本质上没有区别,两者都是根据正则匹配对应的url的重写。但是apache和nginx上的伪静态规则还是有点不同,在配置的时候要注意;
2)apache处理伪静态比nginx更有优势;
四、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;}}
伪静态重写
location / {rewrite c(\d+)_(.*).html /index.php?c=user&m=index&id=$1&title=$2 last;root/usr/share/nginx/html/sta;indexindex.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.txt2)# rsync --delete-before -a -H -v --progress --stats /mnt/ops/html.txt /mnt/ops/web.html3)这样置为空后就可以快速删掉原理:把文件系统的目录与书籍的目录做类比,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个url1.http://www.abc.com/index.php/front/index/parse/name/yangxignyi/age/182.http://www.abc.com/index.php/front/index/parse?name=yangxignyi&age=183.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/indexrewrite 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.htmlrewrite parse-(\w+)-(\d+).html /index.php/front/index/parse/name/$1/age/$2 last;如上配置中的\w是数字字母下划线的意思,\d是数字的意思 +是最少一个{1,} 1到无穷大{1,3} 这样是1-3位数。
nginx配置伪静态的Rewrite重写的正则使用说明
正则表达式匹配 :~ 为区分大小写的匹配。~* 不区分大小写的匹配(匹配firefox的正则同时匹配FireFox)。!~ 不匹配的!~* 不匹配的. ?匹配除换行符以外的任意字符\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]标记,表示完成rewritebreak ???终止匹配, 不再匹配后面的规则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
nginx伪静态配置案例集锦
nginx里使用伪静态是直接在nginx.conf 中写规则的,并不需要像apache要开启写模块(mod_rewrite)才能进行伪静态。nginx只需要打开nginx.conf配置文件,在server里面写需要的规则即可。1)配置案例1server {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;}以上添加了几条重写规则访问wojiuwangla.com/wangla.html跳转到百度访问wojiuwangla.com/纯数字至少一个数字.html跳转到QQ官网访问wojiuwangla.com/匹配字母或数字或下划线组合.html 跳转到问道官网。2)配置案例2server { ???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;}禁止htaccesslocation ~/\.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伪静态配置案例
公司域名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.htmlrewrite 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.htmlrewrite 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/.htaccessrewrite ^/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配置伪静态 去除框架的Index.php
在nginx.conf中Location/{}中加上下面这个if判断就可以了:location /{ ???if (!-e $request_filename) { rewrite ^(.*)$ /index.php?s=$1 last; break; }}
Nginx/Apache之伪静态设置 - 运维小结
原文地址:https://www.cnblogs.com/kevingrace/p/10393234.html