How to: Nginx Webserver Tutorial

How to: Nginx Webserver Tutorial

Hello Everyone,
I am starting a new Nginx series, planning to publish complete end to end tutorial.

Setup, Installation, Starting Service and Accessing Initial webpage.

Setup

  • Docker based Ubuntu container with custom network and port forwarding from 80 of the container to 8080 of the host machine, command is as below
docker run --net bridge -it -p 8080:80 --name ubuntu-nginx ubuntu

if you would like to give static IP, you can also give that with --ip 172.18.0.4 argument after --net bridge argument in the above command.

Installation

apt update
apt install nginx -y

NOTE : If DNS Resolution not working inside container, execute below command

echo "nameserver 8.8.8.8" >> /etc/resolv.conf

and then try again above installation steps.

Service

Once installed you can start the service with

service nginx start
# verify same
service nginx status

and access the service on locahost as we have added port forward while creating the container

__$ curl -I localhost:8080
HTTP/1.1 200 OK
Server: nginx/1.18.0 (Ubuntu)
Date: Wed, 03 Nov 2021 18:19:26 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Wed, 03 Nov 2021 17:49:52 GMT
Connection: keep-alive
ETag: "6182cbc0-264"
Accept-Ranges: bytes

or in your favorite browser you can access URL localhost:8080

This concludes a very simple setup of nginx server without any content init.

Basic Nginx Configuration

Quick look at nginx.conf file

Nginx configuration file located at /etc/nginx/nginx.conf you can verify with below command

root@6787c6e55a5d:/# ls /etc/nginx/nginx.conf 
/etc/nginx/nginx.conf
root@6787c6e55a5d:/# 

if you are unable to finx nginx.conf file after installation, you can use below find command

root@6787c6e55a5d:/# find / -name nginx.conf -type f -print 2>/dev/null
/etc/nginx/nginx.conf
root@6787c6e55a5d:/# 

Lets look at first couple of line of nginx.conf

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
  • user www-data -> The line saying that the nginx worker process will be started by user www-data, so just to verify that look at the output of ps -ef | grep nginx
root@6787c6e55a5d:/# ps -ef | grep nginx
root          28       1  0 07:39 ?        00:00:00 nginx: master process /usr/sbin/nginx
www-data      29      28  0 07:39 ?        00:00:00 nginx: worker process
www-data      30      28  0 07:39 ?        00:00:00 nginx: worker process
www-data      31      28  0 07:39 ?        00:00:00 nginx: worker process

  • worker_processes auto; : worker_processes indicates number of workers available in nginx to serve requests, and it can be either integer or auto, when you mention auto as your value, if you have 4 CPU cores, then nginx will span 4 workers automatically, if you have 10 CPUs, 10 workers. For example I have 16 CPUs in my laptop, so I am supposed to have 16 workers, as my configuration is auto.
root@6787c6e55a5d:/# nproc
16
root@6787c6e55a5d:/# 

root@6787c6e55a5d:/# ps -ef | grep "[n]ginx"
root          28       1  0 07:39 ?        00:00:00 nginx: master process /usr/sbin/nginx
www-data      29      28  0 07:39 ?        00:00:00 nginx: worker process
www-data      30      28  0 07:39 ?        00:00:00 nginx: worker process
www-data      31      28  0 07:39 ?        00:00:00 nginx: worker process
www-data      32      28  0 07:39 ?        00:00:00 nginx: worker process
www-data      34      28  0 07:39 ?        00:00:00 nginx: worker process
www-data      35      28  0 07:39 ?        00:00:00 nginx: worker process
www-data      36      28  0 07:39 ?        00:00:00 nginx: worker process
www-data      37      28  0 07:39 ?        00:00:00 nginx: worker process
www-data      38      28  0 07:39 ?        00:00:00 nginx: worker process
www-data      39      28  0 07:39 ?        00:00:00 nginx: worker process
www-data      40      28  0 07:39 ?        00:00:00 nginx: worker process
www-data      41      28  0 07:39 ?        00:00:00 nginx: worker process
www-data      42      28  0 07:39 ?        00:00:00 nginx: worker process
www-data      43      28  0 07:39 ?        00:00:00 nginx: worker process
www-data      44      28  0 07:39 ?        00:00:00 nginx: worker process
www-data      45      28  0 07:39 ?        00:00:00 nginx: worker process
root@6787c6e55a5d:/# ps -ef | grep "[n]ginx" | wc -l
17
root@6787c6e55a5d:/# 

you are seeing 17 lines which includes line 1 which is main process.

  • If I change the worker_process to any integer like 1, then I should be having inly 1 worker thread.
root@6787c6e55a5d:/# ps -ef | grep nginx
root        2718       1  0 08:00 ?        00:00:00 nginx: master process /usr/sbin/nginx
www-data    2719    2718  0 08:00 ?        00:00:00 nginx: worker process
www-data    2721    2718  0 08:00 ?        00:00:00 nginx: worker process
root        2723       1  0 08:00 pts/0    00:00:00 grep --color=auto nginx
root@6787c6e55a5d:/# grep worker_process /etc/nginx/nginx.conf 
worker_processes 2;
root@6787c6e55a5d:/# 
  • pid /run/nginx.pid; : this line includes the PID of nginx master process
root@6787c6e55a5d:/# cat /run/nginx.pid 
2718
root@6787c6e55a5d:/# 
  • include /etc/nginx/modules-enabled/*.conf; : This is the include directive, which will be used to include configuration files from a specific directory and as you can see regex also supported here.

  • The remaining lines of the nginx.conf are self explanatory, but I am still covering some of them below. As per initial installation, you would see only basic configuration only.

  • We can have more configuration files and couple of them are here as follows, sort of important ones.
    |Standard Name|Description |
    |-------------|---------------------------------------------------------|
    |nginx.conf |as you know this is the main config file |
    |mime.types |A list of file extensions and their associated MIME types|
    |fastcgi.conf |Fast CGI-Related configuration |
    |proxy.conf |Proxy related configuration |
    |sites.conf |virtual host related configuration |

  • Its not mandatory to use this predefined config filenames, you can copy all those sections and paste into nginx.conf and it will work without any issues.

  • Its just matter of maintenance when your nginx inventory growing larger.

  • if you have done changes to your configuration file, it is always recommended to verify the syntax of config file with command nginx -t

root@6787c6e55a5d:/# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
root@6787c6e55a5d:/# 
  • When you install a module, you would define its respective configuration in it directive blocks. From the nginx.conf file
events {
        worker_connections 768;
        # multi_accept on;
}

from the above line, the understanding we need that, events module enabled and with direcive blocks, we are specifying configuration for events modules.

  • But you might think what is the meaning of worker_connections and is it somehow related to worker_procesess means, Yes.
  • if you defined your worker_processess as 1 and worker_connections as 768, that means, our nginx server can serve 768 clients at a time, if worker_processes as 2 then as per above worker_connection configuration 768*2 = 1536 connections it can server at a time. so this is the relation between these two.
  • Variables will start with $ in nginx, they are similar to bash variables. But not all directives supports variables. for example log_format directive supports variables but not error_log directive.
  • for blank space, semicolor, (), {}, better enclosing them as strings either single quote or double quote.

Base Module Directives

Base modules are like preinstalled and already enabled modules in nginx. These modules are available by default and help nginx with basic functionality.

- core Module: Essential features, directives such as process management and security.
- Events Module: lets you configure the inner mechanisms of the networking capabilities. 
- Configuration Module: enables the inclusion mechanism. 

Nginx process architecture

  • when nginx service started, with the user who launched this process, a master process will be started, and this master process will spawn worker process number in nginx.conf file.

Core Module Directives

  • name: daemon
    • if you set it off then nginx will not start in backgroud, it always stays in foreground, this is the best option for debugging. By default its always on unless you explicitly set it to daemon off
  root@88c06ed24ab3:/# grep daemon /etc/nginx/nginx.conf                                     
daemon off                                                                                 
root@88c06ed24ab3:/# nginx                                                                 
nginx: [emerg] directive "daemon" is not terminated by ";" in /etc/nginx/nginx.conf:6      
root@88c06ed24ab3:/# nginx -t                                                              
nginx: [emerg] directive "daemon" is not terminated by ";" in /etc/nginx/nginx.conf:6      
nginx: configuration file /etc/nginx/nginx.conf test failed                                
root@88c06ed24ab3:/# vim /etc/nginx/nginx.conf                                             
root@88c06ed24ab3:/# nginx -t                                                              
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok                           
nginx: configuration file /etc/nginx/nginx.conf test is successful                         
root@88c06ed24ab3:/# nginx    # here as the daemon off, process running in the foreground.                                                             
^Croot@88c06ed24ab3:/# vim /etc/nginx/nginx.conf                                           
root@88c06ed24ab3:/# grep daemon /etc/nginx/nginx.conf                                     
daemon on;                                                                                 
root@88c06ed24ab3:/# nginx    # here as the daemon on, process running in the background.                                                              
root@88c06ed24ab3:/#                                                                       
root@88c06ed24ab3:/#                                                                       

Comments

Popular posts from this blog

grep: unknown device method

Uploading files to FTP/SFTP using CURL

How to find outgoing IP in Linux ?