nginx里使用偽靜態(tài)是直接在nginx.conf 中寫規(guī)則的,并不需要像apache要開啟寫模塊(mod_rewrite)才能進(jìn)行偽靜態(tài)。
nginx只需要打開nginx.conf配置文件,在server里面寫需要的規(guī)則即可。
 代碼如下:
server 
{ 
listen       80; 
server_name  bbs.Vevb.com; 
index index.html index.htm index.php; 
root  /home/www/bbs; 
error_page  404                                             /404.htm;       #配置404錯(cuò)誤頁(yè)面 
location ~ .*.(php|php5)?$ 
{ 
#fastcgi_pass  unix:/tmp/php-cgi.sock; 
fastcgi_pass  127.0.0.1:9000; 
fastcgi_index index.php; 
include fcgi.conf; 
} 
#下面就是偽靜態(tài)了
location /{ 
rewrite ^(.*)/equip(d+).html$ $1/index.php?m=content&c=index&a=lists&catid=$2 last; 
} 
access_log  access_log   off; 
} 
然后重啟nginx服務(wù)器偽靜態(tài)就生效了,這種維護(hù)起來(lái)很是不方便我們可以把它寫在外部文件如bbs_nginx.conf中
在/home/www/bbs目錄下創(chuàng)建bbs_nginx.conf文件并寫入以下代碼:
 代碼如下:
ocation /{ 
rewrite ^(.*)/equip(d+).html$ $1/index.php?m=content&c=index&a=lists&catid=$2 last; 
} 
然后在上面的代碼后面加上如下代碼:
 代碼如下:
include /home/www/bbs/bbs_nginx.conf; 
這樣網(wǎng)站根目錄中的bbs_nginx.conf偽靜態(tài)規(guī)則,即可實(shí)現(xiàn)單獨(dú)管理。
下面是一個(gè)實(shí)例:
1. 在使用.htaccess文件的目錄下新建一個(gè).htaccess文件,如下面一個(gè)Discuz論壇目錄:
 代碼如下:
vim /var/www/html/jb51/bbs/.htaccess 
2. 在里面輸入規(guī)則,我這里輸入Discuz的偽靜態(tài)規(guī)則(這里僅增加Discuz的偽靜態(tài)規(guī)則):
 代碼如下:
# 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配置文件:
 代碼如下:
vim  /etc/nginx/nginx.conf 
4. 在需要添加偽靜態(tài)的虛擬主機(jī)的server{}中引入.htaccess文件:
 代碼如下:
include /var/www/html/jb51/bbs/.htaccess; (備注:把路徑改成你.htaccess文件的具體位置) 
wq保存退出。
新聞熱點(diǎn)
疑難解答
圖片精選