Converting PhpBB-SEO Apache RewriteRule to Nginx rewrite
After a few hours of work migrating the phpbb forum from an Apache / prefork / mod_php / mysql (P4 3.2Ghz / 3G RAM) installation to a much slower test system based on Nginx / php-fpm+mysqlnd / mysql (Dual P3 1.2Ghz / 2G RAM), both with xcache and with some compile time optimizations, I was surprised to see the same performance on both systems (ab): ~16 Requests/s. One notable difference is that consecutive Apache tests do not have constant results, while nginx ones do.
Migrating PhpBB-Seo (seo premod forum) from apache to nginx requires, of course, database migration, php files with attachments and last, but not least, rewrite rules migration.
Here is an example of a few Apache RewriteRule for phpbb-seo:
Code:
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]
# GLOBAL ANNOUNCES WITH VIRTUAL FOLDER ALL MODES
RewriteRule ^announces/(topic|[a-z0-9_-]*-t)([0-9]+)(-([0-9]+))?\.html$ /viewtopic.php?t=$2&start=$4 [QSA,L,NC]
# TOPIC WITHOUT FORUM ID & DELIM ALL MODES
RewriteRule ^([a-z0-9_-]*)/?(topic|[a-z0-9_-]*-t)([0-9]+)(-([0-9]+))?\.html$ /viewtopic.php?forum_uri=$1&t=$3&s tart=$5 [QSA,L,NC]
and below the equivalent rewrite rules for phpbb-seo in nginx:
Code:
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;
# GLOBAL ANNOUNCES WITH VIRTUAL FOLDER ALL MODES
rewrite ^/announces/(topic|[a-z0-9_-]*-t)([0-9]+)(-([0-9]+))?\.html$ /viewtopic.php?t=$2&start=$4 last;
# TOPIC WITHOUT FORUM ID & DELIM ALL MODES
rewrite ^/([a-z0-9_-]*)/?(topic|[a-z0-9_-]*-t)([0-9]+)(-([0-9]+))?\.html$ /viewtopic.php?forum_uri=$1&t=$3&start=$5 last;
The trick is to follow these rules:
- RewriteRule becomes rewrite
- in nginx the rewrite rules require a forward slash (/) after the ^ character (in regex it means that the URL should start with a forward slash), while Apache doesn't require this.
Ex.
Code:
^([a-z0-9_-]*)/?(topic|[a-z0-9_-]*-t)([0-9]+)(-([0-9]+))?\.html$
in apache vs.
Code:
^/([a-z0-9_-]*)/?(topic|[a-z0-9_-]*-t)([0-9]+)(-([0-9]+))?\.html$
in nginx
-Following
Code:
[QSA,L,NC]
in apache rewrite rules becomes:
Code:
last;
in Nginx rewrite rules (these must always be terminated by a ; [semicolon]).
- nginx rewrite rules are NOT read from
.htaccess files (this should be obvious), but from nginx.conf or from nginx vhost configuration file, depending on your configuration.