What is Nginx (Web Server) and how to install it ?
What is a Web Server?
Whenever you open your browser, type a URL and then click enter. Basically, you are requesting the contents of that URL. Ever wondered where the content are? Yes, you’re right those are contents placed on remote computers which, after accepting your request, send the contents of that URL back as a response.
Web Servers are computers that deliver the requested web pages. Every web server has an IP address and domain name.
Let us understand web servers as an example.
You open your favorite browser and type www.geeksforgeeks.org/c-plus-plus and click enter to see resources on C++. In the above URL, www.geeksforgeeks.org is the domain name, and /c-plus-plus is the page you want to see.
So www.geeksforgeeks.org will route your request to the webserver and that will, in turn, see the file system that you’re requesting. In our case, it is /c-plus-plus.
What is Nginx?
It is open-source software designed for maximum performance and stability. Let’s see basically why we need it basically see how we can benefit from this.
Why do we need a dedicated webserver?
Suppose we develop our application on Django or Node. All such frameworks have built-in development servers to host your project. But whenever someone tries to host that application on the production level with built-in development servers, you’ll get a tough time handling all the requests while experiencing downtimes while just handling 30-40 concurrent requests.
Nginx is a dedicated web server that has solved efficiency issues and provided us with an optimum way to handle 1000s of requests concurrently.
Web server for reverse proxy, caching, and load balancing.
The reverse proxy accepts a request from a client, forwards the request to a server that can fulfill it, and returns the response from the server to the client.
Caching is a technique that stores a copy of a given resource and serves it back when requested. When a web cache has a requested resource in its store, it intercepts the request and returns its copy instead of re-downloading from the originating server.
A load balancer distributes the incoming client requests to a group of servers, in which it can handle concurrent requests without experiencing load on a particular server.
Other features of Nginx are as follows:
- It provides HTTP server capabilities.
- Designed to provide stability and maximum performance.
- Functions as a proxy server for email (IMAP, POP3, and SMTP).
- It uses an event-driven and non-threaded architecture to provide less CPU computation per request served.
- It provides scalability.
- Reduces wait time for the client.
- Upgrades can be done while Nginx is hosting a website without any downtime.
I hope you’re convinced now why we should use a dedicated web server in our production application.
For understanding the further configuration settings of Nginx, we need to get a glimpse of Nginx understands before that.
Nginx uses the Master-Slave architecture, where we have a master who reroutes our request to any of the workers under it by distributing the load on the server, then the Proxy cache is looked for faster response, else after failing to do So the webpage is loaded from the memory itself. An image demonstration will help to understand this structure more clearly.
Some Configuration Settings for Nginx
The main settings required for Nginx are stored in a file named nginx.conf. This file is mainly structured into many contexts.
worker_processes: A worker process is basically the number of workers that a master will control. To be more precise, it is a single-threaded process. So if you have a multi-core processor like 8-cores, you can set 8 worker_processes to fully utilize disk bandwidth and help it in doing CPU-intensive work.
worker_connections: It is the number of simultaneous connections with different clients. The default is 512 or 1024. So, suppose you have 8 worker_processes and 512 worker_connections, the maximum clients you can serve is worker_processes * worker_connections, i.e. 8*512 = 4096 connections.
access_log & error_log: By default, the Nginx server writes its events in 2 types of logs as mentioned. The activities of all visitors, requesting data from the server are logged in access_log, you can find which files were accessed and how Nginx responded to them, the IP of the client, and a lot more. But if Nginx faces any issues and glitches, then it will log all this information in the error log for further debugging.
gzip: If you want to juice out as much performance as available, it compresses all the response data. but it uses extensive CPU resources, which can slow down your Nginx if you use this on all items, so keep this option up and running only for large items.
How to install Nginx?
Steps to follow are:
- Install Nginx
- Adjust Firewall
- Check your server
Linux (Ubuntu like distros)
First of all, open the terminal in your Linux distro and then type in the following commands to install Nginx on your system.
# Update your system sudo apt-get update # After updating your system # Install nginx using CLI, press Y to allow it to install sudo apt-get install nginx # Enabling Firewall sudo ufw enable
These are some steps to installing Nginx and enabling the firewall in Linux. Let’s check the version and proceed ahead to starting the server.
# checking Nginx version nginx -v # if output is -> nginx version: nginx/1.xx.x (ubuntu) # you have successfully installed it
Now we need to add the rules to the firewall so that your server can get requests on Http and Https ports.
# This commands tells you all the configuration # that your firewall know which can be added sudo ufw app list # Here you'll see the output and from available options, # you shall see (Nginx Full, Nginx HTTP, Nginx HTTPS) # Let's add these rules to your firewall sudo ufw allow 'Nginx Full' sudo ufw allow 'Nginx HTTP' sudo ufw allow 'Nginx HTTPS'
Now we need to see the status of the rules that we added to the firewall.
# To check status sudo ufw status
Now we will check if our server is running.
# To check the status of the server sudo systemctl status nginx
Congratulations! If you can see active (running) under the Active heading, means your server is running.
Windows (7/8/10 or maybe 11)
Click here to go to the download page of Nginx, and download a mainline version (in my case it is nginx-1.21.1.zip).
Unzip it anywhere you want, open the folder and open the Nginx application, and allow it to change the settings and rules in the firewall.
Voila! Your server is running and you can check that by going to this link in the browser: http://localhost/
Now you might be confused about where this webpage (Welcome to Nginx) is coming from. For that, let’s proceed to the Conf folder in Nginx, unzip the folder, and use any editor (or notepad) to open the nginx.conf file.
Here you can see worker_processes, worker_connections, and other discussed configuration settings. You can go under HTTP -> server -> location/ to get the location of the webpage or which routes will open a webpage from. Here it says under the root directory (here it is an unzipped folder) the HTML folder, index.html page is being served as a response.
Hence, whenever you have started the Nginx server and while going on localhost URL in a browser, if you see Welcome to Nginx. Our server is up and running!!
Do read next article on setting up nginx on the remote server: link
Please Login to comment...