Nginx: setup and installation

What is apache, nginx? Purpose, features, settings options are things that every web developer should be familiar with in order to test their achievements.

About nginx let us put in a word

nginx setup
This tool has one main and several workflows. The first is reading and checking the configuration. Also under his control is the management of work processes. The task of the latter is to process incoming requests. Nginx uses an event-based model. Operating system-specific mechanisms are also used to efficiently distribute requests directly between workflows. Their number is always indicated in the configuration file. The value can be either fixed or set automatically, being guided by the number of processor cores with which you can work. In nginx, the system and modules are configured using the configuration file. Therefore, if you need to change something, then you need to look for it. Usually it is in the / etc / nginx directive (but the path may change when using other systems) and has the extension .conf.

Startup, reboot and logs

install and configure nginx
To do this, you need to make the executable file work. Configuring a nginx server is possible only when it is running. Management is performed by calling the executable file with the –s parameter. To do this, use the following entry:

nginx -s signal

In this case, you can substitute such commands (they must come from the user that launched the tool):

  1. Stop Used for quick shutdown.
  2. Reload. The command is needed to reload the configuration file. The fact is that any changes will not be applied while the file is running. And for them to take effect, a reboot is required. As soon as this signal is received, the main process will begin to check the correctness of the syntactic component of the configuration file and try to apply the instructions there. In case of failure, it will roll back the changes and will work with the old parameters. If everything happened successfully, then new work processes will be launched, and the old ones will be sent a request to complete.
  3. Quit. It is used for a smooth shutdown. It is used if it is necessary to wait until the current requests are completed.
  4. Reopen Close and open log files.

Using utilities

Processes can also be configured using Unix tools (the kill utility will be considered as an example). Usually they use a mechanism to send a signal directly to the process with data. They are linked using ID. This data is stored in the nginx.pid file. Suppose we are interested in process No. 134. Then for smooth completion we need to send the following information:

kill -s QUIT 1628

Suppose we want to see a list of all running files. We use the ps utility for this. The command will look like this:

ps -ax | grep nginx

That is, as you see, when using additional tools, it is indicated that it is being used. Now let's focus on how the nginx configuration is done.

Configuration file structure

nginx server setup
Installing and configuring nginx involves working with modules. They are configured using the directives that are specified in the configuration file. They are simple and blocky. The first type of directives consists of a name and parameters, which are separated by spaces, and their end is indicated by a semicolon - (;). Block has a similar structure. But in this directive, instead of ending, a set of additional instructions is placed, which are placed in curly braces ({instructions}). If you can place the names and parameters of other processes in them, then such constructions are called context. Examples include http, location, and server.

Static Content Distribution

This is one of the most important tasks facing the nginx configuration. By distributing statistical content, we mean images and HTML pages (not dynamic). Let's say that we need a one-time job to configure the nix nginx cluster. Is it hard to do this? No, and let's look at an example. Before embarking on it, it is necessary to detail the conditions of the problem. So, depending on the requests, the files will come from different local directories. So, in / data / www we have HTML documents. And the directory / data / images contains images. The optimal configuration of nginx in this case requires editing a configuration file in which it is necessary to configure the server block inside http. Two locations will also be used for support.

Implementation: server

one-time work on setting up a nix nginx cluster
So, for starters, we need to create the directories themselves and place the files with the necessary extensions in them (in html you need to add the contents). Then open the configuration file. In it, by default, there are already several server blocks, which for the most part are commented out. To achieve the optimal result, this process must be done in relation to all components by default. Then we add a new server block using this code:

http {

server {

}

}

A configuration file can work with several such blocks. But they must be distinguished by their names and the ports through which data is received.

Implementation: location

apache nginx destination features customization options
Defined internally by server:

location / {

root / data / www;

}

The presence of the β€œ/” sign is necessary to compare the received data and see if there is such an address from the processed request here. If there are no problems, then specify the path / data / www to the necessary file, which is located in this local system. If there is a match with several blocks, then the one with the longest prefix is ​​selected. In the given example, its length is equal to one, that is, the use will be exclusively if there are no "competitors". Now let's update it:

location / images / {

root / data;

}

As you can determine, we are looking for images. And now let's combine all the achievements that were earlier, and the configuration at the moment is as follows:

server {

location / {

root / data / www;

}

location / images / {

root / data;

}

}

This is a working version, which happens to be standard port number 80. This server can be easily accessed on the local computer if you go to the address: http: // localhost /. How will all this work?

The principle of operation of the example

configure nginx on the local computer
So, when requests come in that start with / images, the server will send files from the corresponding directory to the user. If it is absent, information indicating error 404 will be transmitted. If nginx is configured on the local computer, then when we request http: //localhost/images/example.png, we will receive a file whose location is /data/images/example.png. If one character β€œ/” is specified, the search will be performed in the directory / data / www. But we just changed the configuration. For it to start working, it needs to be rebooted. To do this, use the nginx -s reload command. In the case when normal operation is not possible, then in the error.log and access.log files located in the / usr / local / nginx / logs directive, you can search for the cause of the malfunctions.

Creating a simple proxy server

optimal nginx configuration
It can be said about nginx - setting up this object is one of the frequent applications (and quite easy, by the way). It uses the principle of a server that accepts a request, and then redirects them to the necessary sites. After this, an answer is expected from them, which directs them to the one who set the task. Therefore, let's look at an example of creating a base point. She will be engaged in servicing user requests and provide them with images from the local directory. So, add another server with the following content to the http block:

server {

listen 8080;

root / data / up1;

location / {

}

}

Now let's decrypt for you: a simple server is being created. It will listen on port 8080. Do not specify listen, then the server will work on the 80th. All requests within the local file system that are directed to the / data / up1 directory will be displayed (of course, you will need to create it before that). To be able to check it, you need to place the index.html file there. By placing the root directive in the server context, we can use location under any conditions (since, thus, access restrictions are removed). Now we are working on creating a proxy server. For its operation, we will need the proxy_pass directive, for which the protocol, name, and also the object port will be specified as parameters (if connected locally, it will look like http: // localhost: 8080). You get the following result:

server {

location / {

proxy_pass http: // localhost: 8080;

}

location / images / {

root / data;

}

}

If you review the code and analyze it, you may notice that the second location block has been changed. So, in this case, it can work with typical image extensions. In a slightly different way, it could be displayed in this way:

location ~ \. (gif | jpg | png) $ {

root / data / images;

}

The final proxy configuration is as follows:

server {

location / {

proxy_pass http: // localhost: 8080 /;

}

location ~ \. (gif | jpg | png) $ {

root / data / images;

}

}

It will filter out requests at the end of which there are specified extensions, and send them to the one who asked for the files. Do not forget that if you want to check the configuration file, you will need to reload it. And believe me, this is the simplest nginx setup. If you open the configuration file of the Vkontakte server or another large company, they will have more code than the words in this article.

Source: https://habr.com/ru/post/K8425/


All Articles