centos+Nginx + PHP+mysql,请问我怎么开启rewrite

2024-11-20 18:24:22
推荐回答(1个)
回答1:

nginx不用开启rewrite,nginx的重写规则需要写在nginx的conf里面,下面是一些例子

rewrite重写需要写在location里面如

location / {
            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;
        }

完整的例子

server
{
listen       80;
index index.html index.htm index.php default.html default.htm default.php;
root  /data/wwwroot/yiqicms;
                location / {
if (-f $request_filename/index.html){
                 rewrite (.*) $1/index.html break;
          }
if (-f $request_filename/index.php){
                 rewrite (.*) $1/index.php;
         }
rewrite ^/article\/(.+)\.html$ /article.php?name=$1 last;
rewrite ^/product\/(.+)\.html /product.php?name=$1 last;
rewrite ^/category\/([^/]+)_([0-9]+)[/]? /category.php?name=$1&p=$2 last;
rewrite ^/category\/([^/_]+)[/]? /category.php?name=$1 last;
rewrite ^/catalog\/([^/]+)[/]? /catalog.php?type=$1 last;
rewrite ^/comment.html$ /comment.php last;
rewrite ^/sitemap.xml$ /sitemap.php last;
rewrite ^/a_(.+)\.html$ /article.php?name=$1 last;
rewrite ^/p_(.+)\.html /product.php?name=$1 last;
rewrite ^/c_([^/]+)_([0-9]+)[/]? /category.php?name=$1&p=$2 last;
rewrite ^/c_([^/_]+)[/]? /category.php?name=$1 last;
error_page 404  /404.htm;
         }

location ~ .*\.(php|php5)?$
{
try_files $uri =404;
fastcgi_pass  unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fcgi.conf;
}

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires      30d;
}

location ~ .*\.(js|css)?$
{
expires      30d;
}
}