DEV Community

Cover image for Nginx configuration for beginners
Hussain Md. Safwan
Hussain Md. Safwan

Posted on

Nginx configuration for beginners

Nginx (pronounced as Engine X) is a reverse proxy server. What is a reverse proxy you ask? Well, say you wanna make a GET request to an address called www.example.com but the twist is that there are several servers under that domain, serving identical or different versions of the website, say for different languages, and your PC (the client) doesn't know which server to make the request to. Here drops the concept of a reverse proxy. We can configure a proxy server in between the client (your PC) and the server (servers serving for www.example.com in this illustration) that can route the request to the appropriate server based on load balancing or other deterministic criteria.

Okay, what's up with Nginx?

Nginx reverse proxy is what you need when you intend to cram multitudes of different websites under on single server differentiated by nothing but a port number. One way of accessing the sites would be to manually mention the port number along with the domain, like www.domain.tld:3000 - which, obviously, isn't the most practical way of doing it. What we want here is to have a mechanism within the server that somehow maps the domain name to the specific port it should point to. That's exactly what you're here for right?

Install Nginx

Installing nginx is quite easy.

sudo apt-get install nginx

Check wether its installed successfully by checking the version,

nginx -v

Before configuring the actual server, we need to unlink the default file stored in /etc/nginx/sites-enabled from the file of same name stored in /etc/nginx/sites-available. We do that with,

cd /etc/nginx/sites-enabled && unlink default

Configure Nginx

Now we're all set to configure the actual server. To do that, we cd to /etc/nginx/conf.d and create a file with .conf extension. The file can basically assume any valid filenames.

cd
cd /etc/nginx/conf.d
nano site-config.conf

The nano interface should open now which lets you edit the .conf file you just created. Within the .conf, we can add as many server blocks as we want, where each block represents a process running on a specific port. Here I'll show the minimal configuration for two sites running on ports 3000 and 5000 respectively.


server {
        listen 80; // default port
        server_name domain1.tld, www.domain1.tld; //domain name

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

server {
        listen 80; // default port
        server_name domain2.tld, www.domain2.tld; // add your domain name here

        location / {
                proxy_pass http://127.0.0.1:5000; // add your <IP address:port> here
        }
}

Enter fullscreen mode Exit fullscreen mode

Test and Deploy

What actually is happening over here is that the server by default listens to the port 80, unless one is mentioned explicitly (like www.domain1.tld:3000), which we are enforcing a change on by assigning the proxy_pass parameter to the server IP address with a port.

Every server usually comes with an Apache engine pre-installed which needs to be disabled to allow nginx to take on the role. We specifically need to make room for nginx on ports 80 and 443.

sudo fuser -k 80/tcp
sudo fuser -k 443/tcp

Next, we need to screen for any syntax error in the .conf file,

nginx -t

Should any syntax errors occur, the test returns a fail and an OK if everything is fine.

Finally, we need to restart nginx to apply the changes made.

sudo systemctl reload nginx

and see if its running by,

systemctl status nginx

if that shows a green active flag, then you're ready to check that out. To see that in action, simply pull out a browser window and type in the domain name.

Latest comments (0)