Skip to content

Nginx | Understanding the structure of Nginx.

Introduction

In this blog, we’ll go through the Nginx setup and structure, how it manages the requests, and how we can efficiently set up the servers using Nginx. There are many properties in nginx that can be used to optimize the server requests and responses.

Structure of Nginx

Configuration files of Nginx are situated in /etc/nginx containing importantly sites-available, sites-enabled folders, and nginx.conf file. nginx.conf file invokes or imports other Nginx configuration files and we can configure this to import from different locations.

For changing or including files from different directories of locations, we need to change include /etc/nginx/sites-enabled/*; or add another include block in the file.

Nginx Directives

Inside the sites-available directory, we can create a file with any name and then enable it in Nginx so that it can start reading from this. In any of these files, there needs to be a server block containing the configuration in {}(curly braces).

Inside the server block, we defile everything like the listening port, root files, server names, what to return and SSL configurations, and many more things according to the requirements.

In the server block, listen is used to let Nginx know when the request is incoming from the port defined in listen, then execute the configuration, defined in the block. listen 80; is for HTTP requests and listen 443 SSL; is for HTTPS requests.

And then there is the index, which lets the Nginx know if the request is sent to the directory and then which file to execute first priority-wise. For example: index index.php index.html index.htm;

server_name is used to define the domain name or names in which case the configuration will be executed. For example: server_name blog.ramansaini.in;. server_name can be dynamic, like _ for all requests, *.ramansaini.in for all subdomain requests and we can also use regex in the server_name for getting the incoming request domain in a variable like: server_name ~^(?<name>(.*?))\.ramansaini\.in;.

root is used to define the directory where the request will be sent. For example: root /var/www/html/

Error and Access logs can be defined to log all the request behavior in files. Access log will write whatever we want to, in a defined file. For example: for error: error_log /var/log/nginx/error.log;, for access: access_log /var/log/nginx/access.log;

For setting up the SSL in Nginx configuration, first of all, we need to change the port in listen to listen 443 ssl;, and then using the created certificates as:

ssl_certificate /etc/letsencrypt/live/php.thcs.in/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/php.thcs.in/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

And then for PHP applications, like Laravel, WordPress, or other frameworks from PHP, we need to define the socket location for PHP-fpm, when the request needs to execute the .PHP file. For example:

location / {
try_files $uri $uri/ /index.php?$args;
        }
location ~ \.php$ {
include snippets/fastcgi-php.conf;
 fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}

Other than that, we can define the maximum size of the file to be uploaded using client_max_body_size, the timeout for a request using client_body_timeout, and how long a request should stay in the buffer with keepalive_timeout, and we can also setup request compression using gzip module.

For beginners do checkout: nginx for beginners.

We use the proxy_pass module to forward the incoming request from one port to another port. Like if an app is running on a port, e.g. a node app running on port 3003, and we need to forward the request from port 80 to 3003 port then we’ll use proxy_pass like this:

location / {
proxy_pass http://localhost:3003;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_pass_header Server;
}

You can also check out: How to set up nginx for optimized load.

Bonus

As a bonus, we can use a subdomain as a variable to configure the Nginx behavior accordingly. For example, we use regex in server_name:
server_name ~^(?<name>(.*?))\.js\.thcs\.in;

Then use the name variable in if conditions in the configuration:
if ($name = "blog"){
proxy_pass http://localhost:3003;
}

2 thoughts on “Nginx | Understanding the structure of Nginx.”

  1. Pingback: How to set up Nginx for optimized traffic load? – Raman

  2. Pingback: How to configure server security with Nginx. - Raman

Leave a Reply

Your email address will not be published. Required fields are marked *