phpBB 3.2 nginx config

Hi I’m fairly new (again) to phpBB forum, why? because I never use it since 14years ago.

For those who are looking for the nginx configuration, i’m sharing mine below

I’ve omitted the part of the installer location block.

To those who will be installing from scratch, you will need to add the installer location block, please google it.

Please take note as well on the “# rewrite for SEO PLugin” part , .

rewrite ^/(.*)-f([0-9]*)/(.*)-t([0-9]*)-s([0-9]*).html /viewtopic.php?f=$2&t=$4&start=$5&$query_string last;
rewrite ^/(.*)-f([0-9]*)/(.*)-t([0-9]*).html /viewtopic.php?f=$2&t=$4&$query_string last;
rewrite ^/(.*)-f([0-9]*)/index-s([0-9]*).html /viewforum.php?f=$2&start=$3&$query_string last;
rewrite ^/(.*)-f([0-9]*)/ /viewforum.php?f=$2&$query_string last;
rewrite ^/(.*)-f([0-9]*) /viewforum.php?f=$2&$query_string last;

This is for the requirement for SEO url extension from https://github.com/tas2580/seourls, therefore remove it if you don’t plan to use the extension.

   # The actual board domain.
    server {
        #listen 80;
        server_name yourdomain.com;
        client_max_body_size 20m;

        root /path/to/your/phpbb;

        location / {
           # phpBB uses index.htm
           index index.php index.htm index.html;
           # rewrite for SEO PLugin
           rewrite ^/(.*)-f([0-9]*)/(.*)-t([0-9]*)-s([0-9]*).html /viewtopic.php?f=$2&t=$4&start=$5&$query_string last;
           rewrite ^/(.*)-f([0-9]*)/(.*)-t([0-9]*).html /viewtopic.php?f=$2&t=$4&$query_string last;
           rewrite ^/(.*)-f([0-9]*)/index-s([0-9]*).html /viewforum.php?f=$2&start=$3&$query_string last;
           rewrite ^/(.*)-f([0-9]*)/ /viewforum.php?f=$2&$query_string last;
           rewrite ^/(.*)-f([0-9]*) /viewforum.php?f=$2&$query_string last;
           try_files $uri $uri/ @rewriteapp;
        }

        location @rewriteapp {
            rewrite ^(.*)$ /app.php/$1 last;
        }

        # Deny access to internal phpbb files.
        location ~ /(config\.php|common\.php|includes|cache|files|store|images/avatars/upload) {
            deny all;
            # deny was ignored before 0.8.40 for connections over IPv6.
            # Use internal directive to prohibit access on older versions.
            internal;
        }

        # Pass the php scripts to fastcgi server specified in upstream declaration.
        location ~ \.php(/|$) {
            # Unmodified fastcgi_params from nginx distribution.
            include fastcgi_params;
            # Necessary for php.
            fastcgi_split_path_info ^(.+\.php)(/.*)$;
            fastcgi_param PATH_INFO $fastcgi_path_info;
            fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
            fastcgi_param DOCUMENT_ROOT $realpath_root;
            try_files $uri $uri/ /app.php$is_args$args;
            fastcgi_pass 127.0.0.1:9000;
        }

        # Deny access to version control system directories.
        location ~ /\.svn|/\.git {
            deny all;
            internal;
        }
    }

How to set up nginx load balancer

Load balancer is a very useful feature in distributing incoming traffic to several servers. why would you want to do that?

By splitting load or by redirecting visitors to several active machines in a round robin effect, redundancy is achieved. (this ensures users/visitors doesn’t experience downtime on your server) . The Round Robin algorithm for load balancing sends visitors to one of a set of IPs.

A basic Round Robin feature is fairly easy to implement, it distributes server load without the worry of user experiencing server response time and server downtime. A sample below could help you setup a basic load balancing feature.

First this is you need to have nginx installed.

Setup
1. open you website’s nginx conf file

2. include the upstream module which looks like this:

upstream backend  {
  server vps1.example.com;
  server vps2.example.com;
  server vps3.example.com;
}

3. reference the module in the configuration:

server {
  location / {
    proxy_pass  http://backend;
  }
}

4. Restart nginx

Load Weight
If you have several server of different specs that will be load balanced, and wanting to distribute the user in proportion. Nginx can assign a number specifying the proportion of traffic that should be directed to each server.

A load balanced setup that included server weight can look like this:

upstream backend  {
  server vps1.example.com weight=1;
  server vps2.example.com weight=2;
  server vps3.example.com weight=4;
}

IP Hash
IP hash allows servers to respond to visitors according to their IP address, then sending visitors back to the same server each time they visit (unless the server is down). If a server is not active it will be marked as down. All users that were supposed to connect to the down server will be then directed to an alternate active/alive servers.

upstream backend {
  ip_hash;
  server   vps1.example.com;
  server   vps2.example.com;
  server   vps3.example.com  down;
 }

Max Fails

Max Fail helps instructs nginx to redirect user to a new server with certain conditions met. In the example below I will be sharing 2 basic directives max_fails and fall_timeout.

max_fails – refers to the maximum number of failed attempts to connect to a server should occur before it is considered inactive.
fail_timeout – sets the time during which the specified number of unsuccessful attempts to communicate with the server should happen to consider the server inactive, By default, the parameter is set to 10 seconds. definitions from http://nginx.org/en/docs/http/ngx_http_upstream_module.html

A sample configuration might look like this:

upstream backend  {
  server vps1.example.com max_fails=3  fail_timeout=15s;
  server vps2.example.com weight=2;
  server vps3.example.com weight=4;
}

Vanilla Forum 2.1 Nginx Configuration

Are you already loosing you hair trying to configure your vanilla forum url-rewrite on NGINX?

If your forum is installed on a main domain not a subdomain I might have the solution for you. here’s what got, please let me know if it works for you…

server {
    listen       80;
    server_name  yourdomain.com;
    root   /usr/share/nginx/youvanilladirectory/;
    #access_log /usr/share/nginx/youvanilladirectory/access.log;
    access_log off;
    #error_log /usr/share/nginx/youvanilladirectory/error.log;
    error_log off;
    index  index.php index.html index.htm;
    #Root location
    location ^~ /discussion/download/ { rewrite ^/(.+)$ /index.php?p=$1 last;}
    location ^~ /utility/thumbnail/ { rewrite ^/(.+)$ /index.php?p=$1 last;}
    location / {
        try_files $uri $uri/ @forum;
    }
    # Rewrite to prettify the URL and hide the ugly PHP stuff
    # Start with this commented out until you configure it in Vanilla!
    location @forum {
        rewrite ^/(.+)$ /index.php?p=$1 last;
    }
    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
    location ~* \.(js|css|png|jpg|jpeg|gif|ico)(\?ver=[0-9.]+)?$ {
        expires 1y;
    }
}