Page 1 of 1

phpBB URL静态化的Nginx设置

Posted: 2019-01-15 16:20
by IOsetting
能查到的资料大多数是Apache下的, 对于Nginx的rewrite规则配置介绍的少. 在Nginx下如果需要配置伪静态地址, 需要在server模块下添加rewrite规则, 放置在前缀匹配之后, php后缀正则匹配之前
rewrite只需要处理路径匹配部分的重写, 对于url带的参数(问号后面的部分, 以及post表单提交的部分)可以不用管, 会自动追加到重写后的地址上.

下面是一个例子

Code: Select all

    server {
        listen       80;
        server_name  localhost;
        #access_log  logs/host.access.log  main;
        location / {
            root   /home/someone/somewhere/wwwroot;
            index  index.html index.htm index.php;
        }

        rewrite ^/phpbb/phpBB/f(\d+)\.html.*$ /phpbb/phpBB/viewforum.php?f=$1 last;
        rewrite ^/phpbb/phpBB/f(\d+)-(\d+)\.html.*$ /phpbb/phpBB/viewforum.php?f=$1&start=
$2 last;
        rewrite ^/phpbb/phpBB/f(\d+)-t(\d+)\.html.*$ /phpbb/phpBB/viewtopic.php?f=$1&t=$2 
last;
        rewrite ^/phpbb/phpBB/f(\d+)-t(\d+)-(\d+)\.html.*$ /phpbb/phpBB/viewtopic.php?f=$1
&t=$2&start=$3 last;
        rewrite ^/phpbb/phpBB/f(\d+)-p(\d+)\.html.*$ /phpbb/phpBB/viewtopic.php?f=$1&p=$2 
last;
        rewrite ^/phpbb/phpBB/f(\d+)-t(\d+)-p(\d+)\.html.*$ /phpbb/phpBB/viewtopic.php?f=$
1&t=$2&p=$3 last;

        location ~ \.php$ {
            root           /home/someone/somewhere/wwwroot;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /$document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

    }
上面的重写规则, 对应的是这样格式的静态地址
http://yourforum/phpbb/phpBB/f12.html
http://yourforum/phpbb/phpBB/f12-25.html
http://yourforum/phpbb/phpBB/f12-t3391.html
http://yourforum/phpbb/phpBB/f12-t3391-10.html
http://yourforum/phpbb/phpBB/f12-p5990.html
http://yourforum/phpbb/phpBB/f12-t3391-p5990.html