Add NGINX as NodeBB Proxy Server

Since you already have a domain name and server (droplet) ip point domain or subdomain to the IP address with A record. It will take a few minutes to a few hour to work. In the meantime we will configure Nginx.

In the previous page of the tutorial we have already NGINX and running. It should be running on port 80. Simply visit:

http://your-droplet-ip/

to see NGINX welcome page.

We also have running our forum on IP Address + Port 4567. Here we will only configure Nginx to serve as proxy server.

NGINX Configuration for NodeBB

To configure Nginx, first we will copy the default configuration file to a file name forums.example.com (or as per your domain). Simply replace all forums.example.com with your real sub/domain where you want to your NodeBB forum.

sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/forums.example.com

now edit the forums.example.com file. Run

sudo nano /etc/nginx/sites-available/forums.example.com

the default server block in the configuration file will be like this:

server {
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;

        root /usr/share/nginx/html;
        index index.html index.htm;

        # Make site accessible from http://localhost/
        server_name localhost;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
                # Uncomment to enable naxsi on this location
                # include /etc/nginx/naxsi.rules
        }

Modify it to something like this:

server {
        listen 80;
        #listen [::]:80 ipv6only=on;

        root /var/www/nodebb/forums;
        index index.html index.htm;

        # Make site accessible from http://localhost/
        server_name forums.example.com;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                # try_files $uri $uri/ =404;
                # Uncomment to enable naxsi on this location
                # include /etc/nginx/naxsi.rules

		proxy_set_header X-Real-IP $remote_addr;
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		proxy_set_header Host $http_host;
		proxy_set_header X-NginX-Proxy true;

		proxy_pass http://127.0.0.1:4567/;
		proxy_redirect off;

		# Socket.IO Support
		proxy_http_version 1.1;
		proxy_set_header Upgrade $http_upgrade;
		proxy_set_header Connection "upgrade";

        }

Change you might notice:

  • removed listen default_server
  • commented out next line, it was for IPv6
  • root changed to /var/www/nodebb/forums
  • server_name changed to forums.example.com
  • commented out try_files declaratives
  • And added many lines in the location

Now we will create a symlink using following commands:

sudo ln -s /etc/nginx/sites-available/forums.example.com /etc/nginx/sites-enabled/forums.example.com

Now reload Nginx Config file with the help of following commands:

sudo service nginx reload

Configure NodeBB for Nginx

Once the Nginx is configured properly we have to make a few changes in NodeBB configuration file. To do so run

nano /var/www/nodebb/forums/config.json

and make a few changes:

{
    "url": "http://162.243.194.62:4567",
    "secret": "285530bc-ddc0-45d6-ae31-ea7cec4b9eb7",
    "database": "redis",
    "redis": {
        "host": "127.0.0.1",
        "port": "6379",
        "password": "",
        "database": "0"
    }
}

To

{
    "url": "http://forums.example.com",
    "secret": "2cdaec-x0xx-45d6-ae31-eaasccxxeb7",
    "bindaddress": "localhost",
    "port": "4567",
    "use_port":false,
    "database": "redis",
    "redis": {
        "host": "127.0.0.1",
        "port": "6379",
        "password": "",
        "database": "0"
    }
}

Notice the following lines:

  • "url": "http://forums.example.com",
  • "bindaddress": "localhost",
  • "port": "4567",
  • <"use_port":false,

Now restart NodeBB with following commands:

cd /var/www/nodebb/forums
./nodebb restart

By now your domain should have propagated. Try to visit your forum by typing forum address.

http://forums.example.com

in my case. What is yours? Join our forums and share yours.

Published by

Anand Kumar

I am the person behind this site.