Nginx

About

nginx is a web server

Concept

Process

nginx has one master process and several worker processes. The main purpose of the master process is to read and evaluate configuration, and maintain worker processes. Worker processes do actual processing of requests.

The nginx.pid file that holds the process id is written in the directory:

  • /usr/local/nginx/logs
  • or /var/run.

List the process:

ps -ax | grep nginx
16015 ?        Ss     0:00 nginx: master process /usr/sbin/nginx
16016 ?        S      0:00 nginx: worker process

HTTP request processing

See Nginx - HTTP Request Processing

Default block

The web content is served from a root directory which is by default /var/www/html but you may control your configuration file to find it (below is the location at /usr/share/nginx/html

server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

        error_page 404 /404.html;
        location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
    }

Directory Structure

  • nginx.conf: The main configuration file may be found at:
    • /usr/local/nginx/conf/nginx.conf,
    • /etc/nginx/nginx.conf,
    • /usr/local/etc/nginx/nginx.conf.
  • /etc/nginx/snippets: Reusable definition fragments that can be included in configuration file

Server Logs

  • /var/log/nginx/access.log: A log file of all Http request
  • /var/log/nginx/error.log: A log file of all errors

Configuration file

Default

All conf file placed into include /etc/nginx/conf.d/*.conf are automatically loaded thanks to this line in nginx.conf

http {
    ....
    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;
}

Debian

Only for Debian, the default nginx.conf file has the following conf

http {
    ....
    include /etc/nginx/sites-enabled/*;
}

They use then the following structure:

  • /etc/nginx/sites-available/: The directory where server_block configuration are defined but not enabled
  • /etc/nginx/sites-enabled/: server_block are enabled by creating a linking to the definition file in the sites-available directory.

Reverse Proxy / Load balancer

HTTP - Gateway (Reverse Proxy) - Network - Load Balancer (NLB, ALB, CLB)

Installation

Doc

  • Ubuntu
sudo apt-get install nginx 
  • CentOs/RedHat
sudo yum update -y
sudo yum install nginx -y

Verification that Nginx is up

curl -I 127.0.0.1
HTTP/1.1 200 OK
Server: nginx/1.16.1
Date: Wed, 01 Jan 2020 10:26:06 GMT
Content-Type: text/html
Content-Length: 4833
Last-Modified: Fri, 16 May 2014 15:12:48 GMT
Connection: keep-alive
ETag: "53762af0-12e1"
Accept-Ranges: bytes

Protection

Dokuwiki

https://www.nginx.com/resources/wiki/start/topics/recipes/dokuwiki/

Documentation / Reference

Task Runner