You will see the term “Nginx” in your exam objectives if you are preparing for the Linux Certification Training or Cloud computing Certification Training. What is Nginx exactly? We will explore this topic further.
This Nginx tutorial is for beginners. It explains the features of this webserver and shows you how to use them in Ubuntu. This can be used with any popular operating system or server. Let’s get started!
What is Nginx?
Nginx, one of the most popular opensource webservers, is very popular. A Web server is simply a program which uses HTTP to provide users with a response. Nginx is the most popular web-server in the industry. Nginx is not limited to this use-case. Nginx also has many other uses, such as –
Multi-domain routing management capabilities added.
Reverse proxy.
Load balancing.
This post will explain the basics of Nginx and its installation. It will also explore various use cases.
Image credit: www.medium.comInstalling Nginx
This tutorial uses Nginx on Ubuntu. Refer to the Nginx documentation for any other operating system installation.
The following commands will update Ubuntu OS and install Nginx.
sudo apt upgrade -ysudo apat install nginx.
sudo systemctl status:nginxsudo programctl start/stop nginxsudo systemsudosystemctl restart/nginxsudosystemctl reload-nginx
Nginx processes
Nginx works by creating multiple processes of two types, i.e. Master process and Worker process.
The master process is responsible to read and take actions according the configuration file. It also creates and manages worker processes.
The worker process is responsible to handle server requests and respond with responses.
Configurations of Nginx
Nginx configuration file can be viewed at /etc/nginx/nginx.conf in Ubuntu.
Understanding the configuration files in Nginx is the most important aspect of Nginx. Below are the most important.
User directives specify the user through whom the worker processes will be spun up.user www.data
The worker_processes directive defines the number of worker processes that must be created & managed by the master process. The master process must create and manage the worker processes. If set to “auto”, the master process would create/delete worker processes according to load.worker_processes auto;
The pid directive specified where to find a file containing the Nginx process ID.pid /run/nginx.pid
The access_log directive specifies where to find the nginx logs.
http access_log /var/log/nginx/access.log;
The error_log directive specifies the location of the nginx error logs.http error_log /var/log/nginx/error.log;
The multiple include directive specifies additional files that need to be loaded as configurations.include /etc/nginx/modules-enabled/*.conf;http include /etc/nginx/mime.types; include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*;
Nginx:
Let’s now try Nginx!
Nginx listens to port 80 by default and serves a basic HTML webpage. It’s worth a try.
curl http://localhost:80Welcome to nginx!Welcome to nginx!If you see this page, the nginx web server is successfully installed andworking. Additional configuration is necessary. Please refer tonginx.org for online support and documentation.<br/>Atnginx.com offers commercial support. This will allow you to understand the configurations of Nginx servers.
A custom webpage can be created
Let’s first create our webpage at /usr/share/nginx/html/index.html –
Custom webpage using NginxCustom webpage using NginxNow, let’s add the server configurations at /etc/nginx/conf.d/default.conf –
server listen 80; server_name localhost; location / root /usr/share/nginx/html; index index.html index.htm; Here, the “location /” means that whenever a request comes to the “/” path on address ‘localhost’ on port ’80’, Nginx serves index.html from /usr/share/ng