NGINX Tutorial – Configuration, Modifiers, Rewrite Directives, and Environment Variables


In this NGINX tutorial, we will cover Configuration, Modifiers, Rewrite directives, and Environment variables. This section assumes you have already followed a previous NGINX tutorial. This article also covers configuring and customizing your server’s security settings. Once you have these settings, you can use your NGINX server to run multiple websites.


A configuration file in Nginx specifies how to run the various modules. In addition, it contains directives for enabling and disabling multiple options. These directives are divided into two main types: simple directives and block directives. Simple directives contain a name and several parameters separated by spaces and ended by a semicolon, while block directives have a set of additional instructions enclosed in braces. The configuration file also contains information about contexts. Directives in a block are regarded as the primary context. For example, the server directive is inside the HTTP context.

Nginx is a system daemon that runs in the background. It can be configured with a configuration file with directives like poll and kqueue. It also has rising options, allowing it to accept serialized connections.


Modifiers are unique tags that change the way Nginx interprets URLs. The location directive, for example, varies the configuration depending on the URI of the request. By default, it matches the string from the beginning. Adding a ‘:’ before the URI prompts Nginx to search for the series in case it fits. The equals sign matches the string exactly, whereas a tilde initiates a case-sensitive Regex match. Adding a carat to the line indicates that no Regular Expressions are checked. These modifiers are similar to flags in JavaScript regex.

Nginx supports regular expressions on three different levels. These levels are the requested URI, the server level, and the protocol level. You can customize the Nginx configuration in any of these areas using modifiers. For instance, if you use a location symbol, Nginx will match requests that begin with a prefix like /images/. This prefix will be matched by Nginx first, and then regular expressions in the rest of the URL will be checked after it.

Rewrite directives

Rewrite directives in nginx allow you to specify the contents of the response body for a URL. This can be a simple or complex URL modification. For example, a user could use this directive to change a website’s URL to match a specific server. The message is also flexible, as it can accommodate elements in the URL’s path. However, it must be remembered that it can only return HTTP 301 and 302 responses. For other status codes, a return directive must be used.

A rewrite directive requires a little knowledge of how URLs work. First, you must specify what type of redirect you’d like to achieve. Different methods for achieving this depending on your URL type and how many URLs you have. For example, a simple redirect is ineffective when there are parameters in the URL, but an exact match with the location directive works best.

Environment variables

Nginx is a popular web server and reverses proxy, and many developers use it to deploy multiple applications, including in the cloud. However, Nginx does not support using environment variables within most configuration blocks. The solution is to use envsubst, which lets you define environment variables before the web server starts.

Environment variables are a collection of dynamic named values that an application can use to customize its behaviour. They are often written in all-uppercase, with the dollar sign $ before them.

Apache vs NGINX

The main difference between Apache and NGINX lies in their security level. While both web servers offer excellent protection, they have different strengths and weaknesses. For instance, Apache performs better on low-traffic websites, while NGINX excels at high-traffic sites. So regardless of which one you use, the right level of security is essential for your website.

Another critical difference between the two web servers is flexibility. Apache is more flexible and supports dynamic modules, whereas NGINX requires an external program to handle dynamic content. Apache also supports dynamic module loading, which means you can change your web server without recompiling the binary.


The Nginx web server uses a controller and multiple worker processes to handle requests. The number of worker processes is configured in the configuration file. It can be fixed as per your requirement or dynamically adjusted according to the number of cores available in the CPU. The configuration file also sets the number of processes to spawn when the server is connected. Increasing the number of worker processes will consume more CPU resources but does not improve the server’s performance.

Moreover, Nginx uses a timeout feature that will limit the number of requests it will process during a period. This will help you trace any problems on your web server. However, a significant downside of this feature is that it will slow down the server. You can disable this feature in Nginx settings and get better performance.


An essential aspect of maintaining privacy on your website is the ability to log requests without recording a visitor’s remote IP address. While this feature makes tracking down a compromised site easier, it can backfire in some cases. In such cases, it can be helpful to delete logs after a certain period, which can keep them from being used by hackers.

The default Nginx configuration reveals sensitive information, which can help hackers prepare for attacks. This can be problematic for PCI compliance environments, where this vulnerability must be remedied immediately. Additionally, the server only accepts GET, HEAD, and POST requests by default. This leaves other demands, such as TRACE and DELETE, vulnerable to an attack. It also allows cross-site tracking and cookie information to be stolen.