How To Run phpBB on Nginx With Virtual Hosting

By Angsuman Chakraborty, Gaea News Network
Thursday, July 10, 2008

We now have our phpBB forum running on Nginx web server, a high quality and significantly better performing web server than Apache HTTPD. Soem of the challenges we faced were:

  1. We use nice permalinks in the forum so there are lots of apache httpd rewrite rules in .htaccess files which had to be converted to nginx format.
  2. Additionally we also had to configure it for a virtual hosting setup where the same IP address (server) is shared by multiple websites.

The key change we had to make was adding a / immediately after ^ in the rewrite condition expression. In Apache HTTPD the request URI doesn’t contain an initial / while in nginx it does.As I mentioned before we use nice permalinks. The final rewrite rules for niginx for phpBB are:

rewrite ^/[a-z0-9_-]*-f([0-9]+)/?(p([0-9]+)\.html)?$ /viewforum.php?f=$1&start=$3 last;
# TOPIC WITH VIRTUAL FOLDER
rewrite ^/[a-z0-9_-]*-f([0-9]+)/[a-z0-9_-]*-t([0-9]+)(-([0-9]+))?\.html$ /viewtopic.php?f=$1&t=$2&start=$4 last;
# GLOBAL ANNOUNCES WITH VIRTUAL FOLDER
rewrite ^/announces/[a-z0-9_-]*-t([0-9]+)(-([0-9]+))?\.html$ /viewtopic.php?t=$1&start=$3 last;
# TOPIC WITHOUT FORUM ID & DELIM
rewrite ^/[a-z0-9_-]*/?[a-z0-9_-]*-t([0-9]+)(-([0-9]+))?\.html$ /viewtopic.php?t=$1&start=$3 last;
# PROFILES SIMPLE
rewrite ^/m([0-9]+)\.html$ /memberlist.php?mode=viewprofile&u=$1 last;
# USER MESSAGES SIMPLE
rewrite ^/messages([0-9]+)(-([0-9]+))?\.html$ /search.php?author_id=$1&sr=posts&start=$3 last;
# GROUPS SIMPLE
rewrite ^/g([0-9]+)(-([0-9]+))?\.html$ /memberlist.php?mode=group&g=$1&start=$3 last;
# POST
rewrite ^/p([0-9]+)\.html$ /viewtopic.php?p=$1 last;
# THE TEAM
rewrite ^/the-team\.html$ /memberlist.php?mode=leaders last;
rewrite ^/[a-z0-9_-]+/?(p([0-9]+)\.html)?$ /viewforum.php?start=$2 last;

In Apache you can specify RewriteCond (rewrite conditions) that go with a rewrite rule. You cannot do the same in nginx. However what I did instead was:

if (-e $request_filename) {
        break;
    }

This ensures that existing files, directories or symbolic links are used instead of matching (and re-directing) them with rewrite rules.

We use the same IP to host several websites (virtual hosting) so we had to enclose the rewrite rules above within an if block where we apply it only when the host is forum.taragana.com. It looks like this:

if ($host ~* ^forum\.taragana\.com$) {
        rewrite ^/[a-z0-9_-]*-f([0-9]+)/?(p([0-9]+)\.html)?$ /viewforum.php?f=$1&start=$3 last;
        ...
}

Hope that helps in setting up your nginx server for phpBB. If you have any questions on setting up phpBB on nginx please ask it in our forum.

Discussion

cosmos
October 1, 2009: 11:06 am

please i want somebody to please mail me a web portal software so that i will download it


FrankFN
June 15, 2009: 5:29 pm

I heard from several sources that people didnt recieve the product they bought from amazon
is this true?


incoriRette
March 6, 2009: 5:58 pm

Great post dude,
Thanks alot……for sharing.
You are so helpful……

January 12, 2009: 7:41 am

Hello People,

I Just joined up and am trying edit my profile but i can’t see the button!! probably staring me in the face lol

thanks guys

G


HSCharles
November 13, 2008: 5:15 pm

I have a flash website
i’m looking for the script who of google ads on flash.
do you know this script?

July 12, 2008: 8:16 am

[...] How to run phpBB on Nginx with / without Virtual Hosting [...]

YOUR VIEW POINT
NAME : (REQUIRED)
MAIL : (REQUIRED)
will not be displayed
WEBSITE : (OPTIONAL)
YOUR
COMMENT :