Skip to content

Install Nginx Proxy

Video Lecture

Install Nginx Proxy Install Nginx Proxy

Description

I also want to point a domain name and an SSL certificate to the game server. We can add settings inside our server.js which is being run by NodeJS to manage this, but it is very common to use a proxy instead for this purpose. We can use Nginx. Nginx will be able to manage proxying for all your domain names and SSL certificates for this server, in case you ever want to have several websites running on it.

Check if Nginx is installed

nginx -v

No, then install it with

sudo apt install nginx

Now check its version

nginx -v

Now check its status

sudo service nginx status

Visit http://[your IP address]

CD into the folder Nginx sites-enabled folder.

cd /etc/nginx/sites-enabled
ls

Delete the file default

rm default

Create a new file called socketio-example.conf

nano socketio-example.conf

Add this script

1
2
3
4
5
6
7
8
server {
    listen 80;
    listen [::]:80;

    location / {
        proxy_pass           http://127.0.0.1:3000/;
    }
}

Check that the nginx configuration is OK,

nginx -t

restart

sudo service nginx restart

and verify its status is OK,

sudo service nginx status

Test it by visiting again http://[your IP address]

Look at errors, how it is polling, and not creating a proper socket.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
server {
    listen 80;
    listen [::]:80;

    location /socket.io/ {
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_pass "http://localhost:3000/socket.io/";
    }

    location / {
        proxy_pass           http://127.0.0.1:3000/;
    }
}

Test nginx configuration is OK,

nginx -t

restart

sudo service nginx restart

And visit again http://[your IP address]

It should now be perfect without any errors.

As an extra measure, to ensure that the game can only be played via the Nginx proxy, it is advisable to add a firewall to your server. In the video, I show how to set this up using the options in Digital Ocean.