nginx支持.htaccess文件实现伪静态(url重写),解决不同网站静态规则重复问题

本文介绍了在nginx服务器上如何处理多个站点的伪静态规则,以避免因规则重复导致的服务器无法重启问题。通过添加代码过滤.htaccess文件,防止其被下载,并提供了一个实例演示了在phpStudy套件中为不同站点设置伪静态的步骤。

在 利用apache伪静态规则重写lighttpd伪静态规则的方法 文中提到lighttpd伪静态规则不可以重复

假如您的规则最后如下:

url.rewrite = (
#zblog规则
"^(.*)/read-(.*).html$"=>"$1/index.php?id=$1",
#phpwind规则
"^(.*)/read-(.*).html$" => "$1/read.php?tid=$2&page=$3&fpage=$4"
)

这样是不可以的

因为重复了if

"^(.*)/read-(.*).html$"

所以会导致服务器无法重启

在nginx服务器上也是会遇到类似问题的。

您购买独立主机如果是nginx服务器,您可能不止开一个站,多个站的伪静态规则未必不会冲突。

所以在欢迎您继续收看友声网卡卡北的原创“软文”同时,也请您继续看下去,说不定能用上。

在 使用phpStudy套件,php-fastcgi模式下,nginx添加虚拟主机(多站点)配置 一文中讲道,在nginx下如何添加多站点。

那还是用此文中的phpstudy套件进行演示:

以链接:http://hi.ys166.com/read-573.html

找到文中nginx配置代码:

    server {

        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
        root   "D:/phpStudy/WWW";
        location / {
            index  index.html index.htm index.php l.php;
           autoindex  off;
        }

        #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;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        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;
        }

    server {
        listen       80;
        server_name  127.0.0.2;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
        root   "D:/phpStudy/WWW2";
        location / {
            index  index.html index.htm index.php l.php;
           autoindex  off;
        }

        #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;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        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;
        }

分别在这两个站点中的

root   "D:/phpStudy/WWW";

root   "D:/phpStudy/WWW2";

下分别添加:

include D:/phpStudy/WWW/.htaccess;

include D:/phpStudy/WWW2/.htaccess;

同时在每个站点下添加:

location ~ /.ht {
deny all;
}

位置没有太大要求,我为了好截图,添加的比较靠上,这段代码就是过滤.ht开头的文件,防止.htaccess文件被下载。

改后如图:

nginx支持.htaccess文件实现伪静态(url重写),解决不同网站静态规则重复问题 - 卡卡北 - 友声网博客

nginx支持.htaccess文件实现伪静态(url重写),解决不同网站静态规则重复问题 - 卡卡北 - 友声网博客

 

效果演示:

127.0.0.1为phpwind9.0.1

127.0.0.2为discuz x3.3

这两个论坛程序默认规则 是有重复 的。分开写后就不会出现问题了。
nginx支持.htaccess文件实现伪静态(url重写),解决不同网站静态规则重复问题 - 卡卡北 - 友声网博客
nginx支持.htaccess文件实现伪静态(url重写),解决不同网站静态规则重复问题 - 卡卡北 - 友声网博客

本文源自:友声网博客,地址:http://hi.ys166.com/read-575.html


 如果您想转载本文,请注明:本文源自友声网卡卡北

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值