Nginx For Beginners
Nginx Introduction
Nginx (pronounced "engine X") is a high-performance, open-source web server software. While it's often used as a web server, Nginx is much more versatile and serves as a reverse proxy server, load balancer, and HTTP cache as well. It was created by Igor Sysoev and is known for its efficiency, speed, and scalability.
Topics which we will cover in this blog
Nginx Installation and basic commands.
Nginx important configuration files and other files.
How to serve static content with nginx.
Nginx Installation and basic commands
To install nginx on debian based linux distro (eg: ubuntu) use command
sudo apt-get update
sudo apt-get install install nginx
To install nginx on Arch based Linux (eg: Redhat servers) use command
sudo rpm update
sudo rpm install nginx
Now after it is installed check the status of nginx service by using command
sudo systemctl status nginx
If nginx status is not active then use command below to start and enable nginx.
sudo systemctl start nginx
sudo systemctl enable nginx
To restart or reload nginx use their respective commands
sudo systemctl restart nginx
sudo systemctl reload nginx
Keep in mind that reload will only refresh configuration and restart will first stop and then restart the server according to the configuration.
Nginx Important configuration and other files
In this section we will talk about some important configuration files of nginx and where they are located in the server.
The most important directory where we can find all files related to configuartion is
/etc/nginx
If we navigate to this directory we will get
Now root configuration file for nginx is
nginx.conf
In this file we can find all the default configurations like,
where we can find server access & error logs (eg: see Line4 in below screenshot).
which directory is available to serve static content.
which directory is available to add website configuration (eg: see Line59-60 in below screenshot).
So with default configuration we learned that, we can store our website configuration in below folders
conf.d
sites-enabled
Also we learned that to check the server logs, we can navigate to
/var/log/nginx
Here we can find find files with name
error.log and access.log
Looking upon these files will help in troubleshooting any issue related to nginx.
How to serve static content with nginx
Navigate to
/etc/nginx/conf.d
check if default name file is present. if not present you can create a file with this name or choose any name with .conf extension.
sudo vim default
now file will be opened then add below configuration.
server {
root /var/www/html;
listen 79 default_server;
listen [::]:79 default_server;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 403.
try_files $uri $uri/ =403;
}
}
Now save and exit. (go in command mode enter :wq! and press enter)
Note: we can also use site-enabled folder for this step
In this file we are defining that we want to serve static content over server from location
/var/www/html
So go to this folder and add any content. You can add photos or html file or paste the build folder of react/angular app.
For eg; you can paste below html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Nginx</title>
</head>
<body>
Hellow from Nginx
</body>
</html>
Now open browser of you choice
and go to http://localhost:80
you will find the html content rendered on your browser