针对Bo-blog的Nginx Rewrite实现

今天决定做一下URL优化,直接使用URL Rewrite…后来发现Bo-Blog官网只提供了Apache的Rewrite规则,但有些朋友的空间或VPS使用的是Nginx,就如我的网站 就是使用Nginx而非Apache,那我想使用Bo-Blog的高级高级URL Rewrite,就必须使用Nginx的Rewrite规则,但这规则实在是只了解皮毛,不过好象现在这个社会有网络这东西,在牛人张宴博客上那找到了, 特在此记录以做备份之用。
这个规则我的博客正在使用,目前没发现任何问题,下见代码是写在Nginx.conf里面的:

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
worker_connections  1024;
}

server {
listen       80;
server_name  www.chzker.com chzker.com www.czgblog.com;
root /var/www/html;
index index.html index.php;
#===================下面就是需要添加的代码================================
if (!-e $request_filename)
{
rewrite ^/page/([0-9]+)/([0-9]+)/?$ /index.php?mode=$1&page=$2 last;
rewrite ^/starred/([0-9]+)/?([0-9]+)?/?$ /star.php?mode=$1&page=$2 last;
rewrite ^/user/([0-9]+)/?$ /view.php?go=user_$1 last;
rewrite ^/tags/([^/]+)/?([0-9]+)?/?([0-9]+)?/?$ /tag.php?tag=$1&mode=$2&page=$3 last;
rewrite ^/component/id/([0-9]+)/?$ /page.php?pageid=$1 last;
rewrite ^/component/([^/]+)/?$ /page.php?pagealias=$1 last;

#Force redirection for old rules
rewrite ^/read.php/([0-9]+).htm$ http://$host/post/$1/ permanent;
rewrite ^/post/([0-9]+).htm$ http://$host/post/$1/ permanent;
rewrite ^/post/([0-9]+)_([0-9]+).htm$ http://$host/post/$1/$2/ permanent;
rewrite ^/post/([0-9]+)_([0-9]+)_([0-9]+).htm$ http://$host/post/$1/$2/$3/ permanent;
rewrite ^/index_([0-9]+)_([0-9]+).htm$ http://$host/page/$1/$2/ permanent;
rewrite ^/star_([0-9]+)_([0-9]+).htm$ http://$host/starred/$1/$2/ permanent;
rewrite ^/category_([0-9]+).htm$ http://$host/category/$1/ permanent;
#Filename alias
rewrite ^/([a-zA-Z0-9_-]+)/?([0-9]+)?/?([0-9]+)?/?$ /read.php?blogalias=$1&page=$2&part=$3 last;
}
#===================到此结束========================================
#charset koi8-r;

#access_log  logs/host.access.log  main;

error_page  404              /404.html;

# redirect server error pages to the static page /50x.html
#
error_page   500 502 503 504  /50x.html;
location = /50x.html {
root   html;
}

#use tomcat
location ~ .jsp$ {
index index.jsp;
proxy_pass http://localhost:8080;
}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ .php$ {
root           /var/www/html;
fastcgi_pass   127.0.0.1:9000;
fastcgi_index  index.php;
fastcgi_param  SCRIPT_FILENAME  /var/www/html$fastcgi_script_name;
include        fastcgi_params;
}

# deny access to .htaccess files, if Apache’s document root
# concurs with nginx’s one
#
#location ~ /.ht {
#    deny  all;
#}
}

# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
#    listen       8000;
#    listen       somename:8080;
#    server_name  somename  alias  another.alias;

#    location / {
#        root   html;
#        index  index.html index.htm;
#    }
#}

记得改完以后,重新启动Nginx服务或者直接重启一下服务器就好了!