Nginx Rewrite Rules solution!

Discussions and support about the different URL Rewriting techniques for phpBB.

Moderator: Moderators

Nginx Rewrite Rules solution!

Postby kkrzysi0 » Tue Apr 27, 2010 10:58 pm

Hi
I moved from Apache2 to nginx. But not without problems with the mod rewrite, is solved as follows.

When we have a working apache2 with mod rewrite and good settings in the panel and the generated file. htaccess.
We make the following:

All commands running on linux debian.

1)Searching the file .htaccess only those lines starting with RewriteRule and put that to the rewrite.txt file.
Code: Select all
cat .htaccess | grep RewriteRule >> rewrite.txt

2)Sample lines in the file.
Code: Select all
RewriteRule ^(forum|[a-z0-9_-]*-f)([0-9]+)/?(page([0-9]+)\.html)?$ /viewforum.php?f=$2&start=$4 [QSA,L,NC]
RewriteRule ^(forum|[a-z0-9_-]*-f)([0-9]+)/(topic|[a-z0-9_-]*-t)([0-9]+)(-([0-9]+))?\.html$ /viewtopic.php?f=$2&t=$4&start=$6 [QSA,L,NC]
RewriteRule ^announces/(topic|[a-z0-9_-]*-t)([0-9]+)(-([0-9]+))?\.html$ /viewtopic.php?t=$2&start=$4 [QSA,L,NC]
[...]

3)Changes between apache and nginx:
a)RewriteRule to rewrite
b)
Code: Select all
add "/" after "^"
only at the beginning of rule
c)(in the end of rule) change
Code: Select all
[QSA,L,NC] to last;

Show differences:
Code: Select all
#APACHE FORMAT
RewriteRule ^(forum|[a-z0-9_-]*-f)([0-9]+)/?(page([0-9]+)\.html)?$ /viewforum.php?f=$2&start=$4 [QSA,L,NC]
#NGINX FORMAT
rewrite ^/(forum|[a-z0-9_-]*-f)([0-9]+)/?(page([0-9]+)\.html)?$ /viewforum.php?f=$2&start=$4 last;

4)Examples of changes in Sample lines in 2)
Code: Select all
rewrite ^/(forum|[a-z0-9_-]*-f)([0-9]+)/?(page([0-9]+)\.html)?$ /viewforum.php?f=$2&start=$4 last;
rewrite ^/(forum|[a-z0-9_-]*-f)([0-9]+)/(topic|[a-z0-9_-]*-t)([0-9]+)(-([0-9]+))?\.html$ /viewtopic.php?f=$2&t=$4&start=$6 last;
rewrite ^/announces/(topic|[a-z0-9_-]*-t)([0-9]+)(-([0-9]+))?\.html$ /viewtopic.php?t=$2&start=$4 last;

5)Sample of my rewrite rules to apache in .htaccess
Code: Select all
RewriteRule ^rss(/(news)+)?(/(digest)+)?(/(short|long)+)?/?$ /gymrss.php?channels&$2&$4&$6 [QSA,L,NC]
RewriteRule ^(news|maps)/?(page([0-9]+).html)?$ /map.php?$1&start=$3 [QSA,L,NC]
RewriteRule ^(forum|[a-z0-9_-]*-f)([0-9]+)/?(page([0-9]+)\.html)?$ /viewforum.php?f=$2&start=$4 [QSA,L,NC]
RewriteRule ^(forum|[a-z0-9_-]*-f)([0-9]+)/(topic|[a-z0-9_-]*-t)([0-9]+)(-([0-9]+))?\.html$ /viewtopic.php?f=$2&t=$4&start=$6 [QSA,L,NC]
RewriteRule ^announces/(topic|[a-z0-9_-]*-t)([0-9]+)(-([0-9]+))?\.html$ /viewtopic.php?t=$2&start=$4 [QSA,L,NC]
RewriteRule ^([a-z0-9_-]*)/?(topic|[a-z0-9_-]*-t)([0-9]+)(-([0-9]+))?\.html$ /viewtopic.php?forum_uri=$1&t=$3&start=$5 [QSA,L,NC]
RewriteRule ^resources/[a-z0-9_-]+/(thumb/)?([0-9]+)$ /download/file.php?id=$2&t=$1 [QSA,L,NC]
RewriteRule ^member/([^/]+)/?$ /memberlist.php?mode=viewprofile&un=$1 [QSA,L,NC]
RewriteRule ^member/([^/]+)/(topics|posts)/?(page([0-9]+)\.html)?$ /search.php?author=$1&sr=$2&start=$4 [QSA,L,NC]
RewriteRule ^(group|[a-z0-9_-]*-g)([0-9]+)(-([0-9]+))?\.html$ /memberlist.php?mode=group&g=$2&start=$4 [QSA,L,NC]
RewriteRule ^post([0-9]+)\.html$ /viewtopic.php?p=$1 [QSA,L,NC]
RewriteRule ^active-topics(-([0-9]+))?\.html$ /search.php?search_id=active_topics&start=$2&sr=topics [QSA,L,NC]
RewriteRule ^unanswered(-([0-9]+))?\.html$ /search.php?search_id=unanswered&start=$2&sr=topics [QSA,L,NC]
RewriteRule ^newposts(-([0-9]+))?\.html$ /search.php?search_id=newposts&start=$2&sr=topics [QSA,L,NC]
RewriteRule ^the-team\.html$ /memberlist.php?mode=leaders [QSA,L,NC]
RewriteRule ^([a-z0-9_-]+)/?(page([0-9]+)\.html)?$ /viewforum.php?forum_uri=$1&start=$3 [QSA,L,NC]
RewriteRule ^.+/(style\.php|ucp\.php|mcp\.php|faq\.php|download/file.php)$ /$1 [QSA,L,NC,R=301]
RewriteRule ^.+/(styles/.*|images/.*)/$ /$1 [QSA,L,NC,R=301]
RewriteRule ^(news|maps)/([a-z0-9_-]+)(/([a-z0-9_-]+))?/?(page([0-9]+).html)?$ /map.php?$2=$4&$1&start=$6 [QSA,L,NC]
RewriteRule ^rss(/(news)+)?(/(digest)+)?(/(short|long)+)?(/([a-z0-9_-]+))?/([a-z0-9_]+).xml(.gz)?$ /gymrss.php?$9=$8&$2&$4&$6&gzip=$10 [QSA,L,NC]
RewriteRule ^[a-z0-9_-]*-[a-z]+([0-9]+)(/(news)+)?(/(digest)+)?(/(short|long)+)?/([a-z0-9_]+).xml(.gz)?$ /gymrss.php?$8=$1&$3&$5&$7&gzip=$9 [QSA,L,NC]
RewriteRule ^([a-z0-9_-]+)(/(news)+)?(/(digest)+)?(/(short|long)+)?/([a-z0-9_]+).xml(.gz)?$ /gymrss.php?nametoid=$1&$3&$5&$7&modulename=$8&gzip=$9 [QSA,L,NC]
RewriteRule ^sitemapindex.xml(.gz)?$ /sitemap.php?gzip=$1 [QSA,L,NC]
RewriteRule ^[a-z0-9_-]+-([a-z]+)([0-9]+).xml(.gz)?$ /sitemap.php?module_sep=$1&module_sub=$2&gzip=$3 [QSA,L,NC]
RewriteRule ^([a-z0-9_]+)-([a-z0-9_-]+).xml(.gz)?$ /sitemap.php?$1=$2&gzip=$3 [QSA,L,NC]

6)Sample of my rewrite rules to nginx in site configuration (/etc/nginx/sites-enabled/name
Code: Select all
location / {
root /home/moto/public_html/forum/;
index index.php;
rewrite ^/rss(/(news)+)?(/(digest)+)?(/(short|long)+)?/?$ /gymrss.php?channels&$2&$4&$6 last;
rewrite ^/(news|maps)/?(page([0-9]+).html)?$ /map.php?$1&start=$3 last;
rewrite ^/(forum|[a-z0-9_-]*-f)([0-9]+)/?(page([0-9]+)\.html)?$ /viewforum.php?f=$2&start=$4 last;
rewrite ^/(forum|[a-z0-9_-]*-f)([0-9]+)/(topic|[a-z0-9_-]*-t)([0-9]+)(-([0-9]+))?\.html$ /viewtopic.php?f=$2&t=$4&start=$6 last;
rewrite ^/announces/(topic|[a-z0-9_-]*-t)([0-9]+)(-([0-9]+))?\.html$ /viewtopic.php?t=$2&start=$4 last;
rewrite ^/([a-z0-9_-]*)/?(topic|[a-z0-9_-]*-t)([0-9]+)(-([0-9]+))?\.html$ /viewtopic.php?forum_uri=$1&t=$3&start=$5 last;
rewrite ^/resources/[a-z0-9_-]+/(thumb/)?([0-9]+)$ /download/file.php?id=$2&t=$1 last;
rewrite ^/member/([^/]+)/?$ /memberlist.php?mode=viewprofile&un=$1 last;
rewrite ^/member/([^/]+)/(topics|posts)/?(page([0-9]+)\.html)?$ /search.php?author=$1&sr=$2&start=$4 last;
rewrite ^/(group|[a-z0-9_-]*-g)([0-9]+)(-([0-9]+))?\.html$ /memberlist.php?mode=group&g=$2&start=$4 last;
rewrite ^/post([0-9]+)\.html$ /viewtopic.php?p=$1 last;
rewrite ^/active-topics(-([0-9]+))?\.html$ /search.php?search_id=active_topics&start=$2&sr=topics last;
rewrite ^/unanswered(-([0-9]+))?\.html$ /search.php?search_id=unanswered&start=$2&sr=topics last;
rewrite ^/newposts(-([0-9]+))?\.html$ /search.php?search_id=newposts&start=$2&sr=topics last;
rewrite ^/the-team\.html$ /memberlist.php?mode=leaders last;
rewrite ^/([a-z0-9_-]+)/?(page([0-9]+)\.html)?$ /viewforum.php?forum_uri=$1&start=$3 last;
rewrite ^/.+/(style\.php|ucp\.php|mcp\.php|faq\.php|download/file.php)$ /$1 last;
rewrite ^/.+/(styles/.*|images/.*)/$ /$1 last;
rewrite ^/(news|maps)/([a-z0-9_-]+)(/([a-z0-9_-]+))?/?(page([0-9]+).html)?$ /map.php?$2=$4&$1&start=$6 last;
rewrite ^/rss(/(news)+)?(/(digest)+)?(/(short|long)+)?(/([a-z0-9_-]+))?/([a-z0-9_]+).xml(.gz)?$ /gymrss.php?$9=$8&$2&$4&$6&gzip=$10 last;
rewrite ^/[a-z0-9_-]*-[a-z]+([0-9]+)(/(news)+)?(/(digest)+)?(/(short|long)+)?/([a-z0-9_]+).xml(.gz)?$ /gymrss.php?$8=$1&$3&$5&$7&gzip=$9 last;
rewrite ^/([a-z0-9_-]+)(/(news)+)?(/(digest)+)?(/(short|long)+)?/([a-z0-9_]+).xml(.gz)?$ /gymrss.php?nametoid=$1&$3&$5&$7&modulename=$8&gzip=$9 last;
rewrite ^/sitemapindex.xml(.gz)?$ /sitemap.php?gzip=$1 last;
rewrite ^/[a-z0-9_-]+-([a-z]+)([0-9]+).xml(.gz)?$ /sitemap.php?module_sep=$1&module_sub=$2&gzip=$3 last;
rewrite ^/([a-z0-9_]+)-([a-z0-9_-]+).xml(.gz)?$ /sitemap.php?$1=$2&gzip=$3 last;
}

6)Sample of my config to nginx
Code: Select all
server {
        listen   80;
        server_name  name_of_hosts.com;

        error_log /var/log/nginx/moto_error.log;

        location / {
                root   /home/moto/public_html/forum;
                index  index.php index.html index.htm;
        }

        location /doc {
                root   /usr/share;
                autoindex on;
                allow 127.0.0.1;
                deny all;
        }

        location /images {
                root   /usr/share;
                autoindex on;
        }

        #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   /home/moto/public_html/forum;
        }

        # 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_param  SCRIPT_FILENAME  /home/moto/public_html/forum$fastcgi_script_name;
                include        fastcgi_params;
        }
location / {
root /home/moto/public_html/forum/;
index index.php;
rewrite ^/rss(/(news)+)?(/(digest)+)?(/(short|long)+)?/?$ /gymrss.php?channels&$2&$4&$6 last;
rewrite ^/(news|maps)/?(page([0-9]+).html)?$ /map.php?$1&start=$3 last;
rewrite ^/(forum|[a-z0-9_-]*-f)([0-9]+)/?(page([0-9]+)\.html)?$ /viewforum.php?f=$2&start=$4 last;
rewrite ^/(forum|[a-z0-9_-]*-f)([0-9]+)/(topic|[a-z0-9_-]*-t)([0-9]+)(-([0-9]+))?\.html$ /viewtopic.php?f=$2&t=$4&start=$6 last;
rewrite ^/announces/(topic|[a-z0-9_-]*-t)([0-9]+)(-([0-9]+))?\.html$ /viewtopic.php?t=$2&start=$4 last;
rewrite ^/([a-z0-9_-]*)/?(topic|[a-z0-9_-]*-t)([0-9]+)(-([0-9]+))?\.html$ /viewtopic.php?forum_uri=$1&t=$3&start=$5 last;
rewrite ^/resources/[a-z0-9_-]+/(thumb/)?([0-9]+)$ /download/file.php?id=$2&t=$1 last;
rewrite ^/member/([^/]+)/?$ /memberlist.php?mode=viewprofile&un=$1 last;
rewrite ^/member/([^/]+)/(topics|posts)/?(page([0-9]+)\.html)?$ /search.php?author=$1&sr=$2&start=$4 last;
rewrite ^/(group|[a-z0-9_-]*-g)([0-9]+)(-([0-9]+))?\.html$ /memberlist.php?mode=group&g=$2&start=$4 last;
rewrite ^/post([0-9]+)\.html$ /viewtopic.php?p=$1 last;
rewrite ^/active-topics(-([0-9]+))?\.html$ /search.php?search_id=active_topics&start=$2&sr=topics last;
rewrite ^/unanswered(-([0-9]+))?\.html$ /search.php?search_id=unanswered&start=$2&sr=topics last;
rewrite ^/newposts(-([0-9]+))?\.html$ /search.php?search_id=newposts&start=$2&sr=topics last;
rewrite ^/the-team\.html$ /memberlist.php?mode=leaders last;
rewrite ^/([a-z0-9_-]+)/?(page([0-9]+)\.html)?$ /viewforum.php?forum_uri=$1&start=$3 last;
rewrite ^/.+/(style\.php|ucp\.php|mcp\.php|faq\.php|download/file.php)$ /$1 last;
rewrite ^/.+/(styles/.*|images/.*)/$ /$1 last;
rewrite ^/(news|maps)/([a-z0-9_-]+)(/([a-z0-9_-]+))?/?(page([0-9]+).html)?$ /map.php?$2=$4&$1&start=$6 last;
rewrite ^/rss(/(news)+)?(/(digest)+)?(/(short|long)+)?(/([a-z0-9_-]+))?/([a-z0-9_]+).xml(.gz)?$ /gymrss.php?$9=$8&$2&$4&$6&gzip=$10 last;
rewrite ^/[a-z0-9_-]*-[a-z]+([0-9]+)(/(news)+)?(/(digest)+)?(/(short|long)+)?/([a-z0-9_]+).xml(.gz)?$ /gymrss.php?$8=$1&$3&$5&$7&gzip=$9 last;
rewrite ^/([a-z0-9_-]+)(/(news)+)?(/(digest)+)?(/(short|long)+)?/([a-z0-9_]+).xml(.gz)?$ /gymrss.php?nametoid=$1&$3&$5&$7&modulename=$8&gzip=$9 last;
rewrite ^/sitemapindex.xml(.gz)?$ /sitemap.php?gzip=$1 last;
rewrite ^/[a-z0-9_-]+-([a-z]+)([0-9]+).xml(.gz)?$ /sitemap.php?module_sep=$1&module_sub=$2&gzip=$3 last;
rewrite ^/([a-z0-9_]+)-([a-z0-9_-]+).xml(.gz)?$ /sitemap.php?$1=$2&gzip=$3 last;
}

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


I hope that help someone :D
maybe someone will write a program to change lines?
Greetings from Poland ;] Sorry for my english :D
kkrzysi0
 
Posts: 3
Joined: Sat Jul 26, 2008 12:44 am

Advertisement

NO ITS NOT A SOLUTION - Re: Nginx Rewrite Rules solution!

Postby dlugasx » Tue Dec 13, 2011 1:49 pm

Your configuration is buggy.

For example Search button doesnt work properly.
If You would like to test it, simply open post(not edit mode) and then try to click Search in menu. BUG...

Another thing is, when I`m clicking on the User name...

myforum.com/member2.html

doesnt work...

Its not a solution.

Could You please try to check this once again ?
dlugasx
 
Posts: 3
Joined: Sun Nov 16, 2008 2:40 pm

Re: Nginx Rewrite Rules solution!

Postby yaashul » Wed Apr 03, 2013 11:16 am

I am using nginx as front webserver only to serve static content....dynamic content is served by Apache...Shud I change these things or not.
yaashul
PR1
PR1
 
Posts: 157
Joined: Wed Aug 29, 2007 1:21 am


Return to phpBB mod Rewrite

 


  • Related topics
    Replies
    Views
    Last post

Who is online

Users browsing this forum: No registered users and 8 guests