IT备忘录

您现在的位置是:首页 > 教程 > 建站教程

建站教程

nginx伪静态规则写法

2022-01-16 17:26:55建站教程
nginx作为一个轻量级的网站服务端程序,在很多时候我们需要做一些伪静态设置,下面IT备忘录小编就给大家分享一下nginx伪静态规则写法,不懂的网站长赶紧来学习吧!我们以三种场景给

nginx作为一个轻量级的网站服务端程序,在很多时候我们需要做一些伪静态设置,下面IT备忘录小编就给大家分享一下nginx伪静态规则写法,不懂的网站长赶紧来学习吧!

nginx伪静态规则写法

我们以三种场景给大家举例:

场景一

将 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://z.xx.com/own/xys/index.php/admin/login  以及 http://z.xx.com/2018/gdhp/index.php/login

重写成 http://z.xx.com/own/xys/index/admin/login 以及 http://z.xx.com/2018/gdhp/index/login

建议在nginx/conf目录下新建rewrite.conf配置文件中编写伪静态规则,写完后在域名.conf文件中插入rewrite.conf文件即可(用include rewrite.conf插入)

nginx配置文件如下:

server
    {
        listen 80;
        server_name z.xx.com;
        index index.html index.php index.htm index.php default.html default.htm default.php;
        root  /var/www/apps/z.xx.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/z.xx.com.log;
        error_log  /var/www/wwwlogs/error.z.xx.com.log;
    }

rewrite.conf文件内容如下:

location /
{
        #场景一
        #http://www.abc.com/index.php/front/index/index 变成 http://www.abc.com/a.html
        rewrite 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.html
        rewrite parse-(\w+)-(\d+).html /index.php/front/index/parse/name/$1/age/$2 last;
        #场景三
        #http://z.xx.com/own/xys/index.php/admin/login  以及 http://z.xx.com/2018/gdhp/index.php/login 变成 http://z.xx.com/own/xys/index/admin/login 以及 http://z.xx.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下的子项目伪静态规则
}

 

相关内容标签

文章评论