Familiarize yourself with the installation and configuration of Nginx (Part 3)

Tram Ho

Continuing the previous section , we have looked at some contexts in Nginx configuration such as main , events , http , server , location . And in order to continue everything I need to know about Nginx, in this article, I will continue to mention some other contexts of Nginx.  Let’s start.

Upstream context

The upstream context is used to define and configure the upstream server. This context defines a group of named servers that Nginx can then request a proxy for. This context can be used when you reconfigure proxy types.

The upstream context can be referenced by name in servers or location blocks to pass requests of a certain type to the specified server group. The upstream will then use an algorithm to determine which specific server will send the request.

The upstream context should be placed within the http context, outside of the server context. The structure is as follows:

Mail context

Nginx is often used as a reverse proxy server or web server, but it can also act as a mail proxy server. Mail context is defined in the main context, outside of the http context.

The main function of the mail context is to provide an area to define the proxy resolving configuration on the server. Nginx has the ability to redirect requests to the external authentication server. It can then provide access to POP3 and IMAP servers to serve the data as it really is. Mail context can also be configured to connect to the SMTP Relayhost if desired.

The structure of the mail context will be as follows:

If context

If the context can be set to provide processing with a given condition to execute directives. It is like if statement in normal programming. The if statement in Nginx will execute directives if the condition is true . If the context in Nginx is used in case you want to override it based on conditions. However, it should not be abused.

Its structure is as follows:

Limit_except context

limit_except context is used to restrict the use of HTTP methods in the location context. For example, only certain users have access to content using the POST method, and everyone can read the content (GET method). We can use the limit_except block to make this request.

It will apply the directives inside the context when encountering any HTTP methods except the methods listed in the context header. The result of the above example is that any client can use GET and HEAD, only those from network are allowed to use other methods.

Some other contexts

In addition to the context described above, there are several other contexts available in Nginx and are briefly described below. These contexts depend on optional modules and are rarely used.

  • split_clients : Split customer requests into two or more categories. This context is defined in the http context and is primarily used for A / B testing.
  • map : To create variables whose values ​​depend on the values ​​of other variables and are defined in the http context.
  • geo : Classify the customer’s IP address. It is used to map the value of a variable depending on the connected IP addresses.
  • charset_map : To add specific character sets to the “Content-Type” response header. Also, by using it we can convert data from one character set to another with a number of restrictions that occur.
  • perl/perl_set : Deploy location and variable handlers in Perl and insert Perl calls into SSL. With the use of perl_set we can install the Perl handler for a specific variable.
  • types : MIME re-maps correctly with file extensions. It may appear in the http context, server context or location context.


Along with the previous article, we have gone through the basics of the context in Nginx configuration. As for the directive, there are many other things. So the best way is to read the documentation on Nginx’s homepage . The article certainly has many shortcomings, because the writer is just beginning to learn about Nginx. Hope you read sympathy.

Share the news now

Source : Viblo