NGINX basics and configuratons

Tags:

NGINX is open source web server which is mostly used with PHP. t was originally developed to tackle the 10,000 concurrent connections. NGINX can be used as a standalone web server, or serve in front of other web servers as a reverse proxy.

NGINX can then handle aspects like SSL, GZip, headers, load balancing and much more. While many users are aware of Nginx’s capabilities, new users are often confused by some of the conventions they find in Nginx configuration files. In this blog, we will focus on discussing the basic structure of an NGINX configuration file.

In this blog we will explains basics like how to install NGINX, how to start, stop and reload NGINX server, what is directive, block, how to listen to port and how to set server name.

Installation

Installation of NGINX varies for each operating system, below guide explains how NGINX can installed on most of the operating systems.

Installation on ubuntu

On ubuntu NIGNX is installed using Ubuntu's APT (Advanced Packaging Tool). APT comes with installation,you don't need to install it separately.

sudo apt install nginx
Installation on MacOS

On Mac use brew which is package manager for MacOS. To install brew visit here.

brew install nginx
Installation on Windows
  1. Download NGINX from here
  2. Execute following command
cd c:\
unzip nginx-1.19.3.zip
cd nginx-1.19.3
start nginx
Start, Stop, Reload & Restart

To start nginx server, execute nginx command only.

If your nginx running as services then use below commands

Linux
sudo service nginx start
MacOS
brew services start nginx

Once nginx is started, it can be controlled by invoking the executable with the -s parameter. Use the following syntax:

nginx -s signal

Where signal may be one of the following:

  • stop — fast shutdown
  • quit — graceful shutdown
  • reload — reloading the configuration file
  • reopen — reopening the log files

For example to restart/reload nginx server, execute nginx -s reload

Whenever you make changes to configuration file, it does not get applied immediately.You must reload server to reflect changes.

On reloading, nginx checks configurations, if configurations are correct then it starts server. If there's an error it displays error.

Configurations

nginx consists of modules which are controlled by directives specified in the configuration file. Directives are divided into simple directives and block directives.

Simple

A simple directive consists of the name and parameters separated by spaces and ends with a semicolon (;).

Block

Configurations which contains within { and } are called block directive If a block directive can have other directives inside braces, it is called a context (server, http and etc).

Configuration file may include several server blocks distinguished by ports on which they listen to and by server names. General form of any site we can create include following block

http {
    server {
    }
}
Listen to port

By default, Nginx HTTP server listens for incoming connection and binds on port 80, which represents the standard web port. To listen to port other than 90 use listen directive insider sever or http block.

http {
    server {
        listen 8080;
    }
}
Server name

Server name in simple term is site name or domain name. Server name are defined using server_name directive. Server name may be defined using exact names, wildcard names, or regular expressions

Fixed server name

server {
    server_name  example.com;
}

Wild card sub domain name. Here * matches anything. A special wildcard name in the form .example.com can be used to match both the exact name example.org and the wildcard name *.example.com.

server {
    server_name  *.example.com;
}

The regular expressions used by nginx are compatible with those used by the Perl programming language (PCRE). To use a regular expression, the server name must start with the tilde characte

server {
    server_name  ~^(?<user>.+)\.example\.com$;
}
Root Directory and Index Files

The root directive specifies the root directory that will be used to search for a file. To obtain the path of a requested file, NGINX appends the request URI to the path specified by the root directive. The directive can be placed on any level within the http {}, server {}, or location {} contexts.

server {
    root /www/data;

    location / {
    }

    location /images/ {
    }

    location ~ \.(mp3|mp4) {
        root /www/media;
    }
}

Here, NGINX searches for a URI that starts with /images/ in the /www/data/images/ directory in the file system. But if the URI ends with the .mp3 or .mp4 extension, NGINX instead searches for the file in the /www/media/ directory because it is defined in the matching location block.

Serve PHP files

Depending on your distribution and PHP version, the PHP configuration files will be stored in different locations. In this guide we have considered PHP version to be 7.4

location ~* \.php$ {
    fastcgi_pass unix:/run/php/php7.4-fpm.sock;
    include         fastcgi_params;
    fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
    fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
  }

Above will make NGINX to serve PHP file. When server receives request with path name ending with .php,NGINX passes to php-fpm as specified in line fastcgi_pass unix:/run/php/php7.4-fpm.sock.

Learn more

There's many other things which NGINX can do but to get started we have explained only basics.

To setup Nishchay PHP framework on NGINX on any machine, read installation section in learning Center.

  1. Install on ubuntu
  2. Install on CentOS
  3. Install on MacOS