ComputersProgramming

Nginx: setup and installation

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

About nginx

This tool has one main and several work processes. The first one is reading and testing the configuration. Control of work processes is also under his control. The task of the latter is to process incoming requests. Nginx uses a model that is event-based. Also, mechanisms that are dependent on the operating system are used to achieve efficient allocation of requests directly between workflows. Their number is always indicated in the configuration file. The value can be either fixed or set automatically, based on the number of processor cores that you can work with. In nginx, the system and modules are configured using the configuration file. Therefore, if it is necessary to change something, then it is necessary to seek it. Usually it is in the / etc / nginx directive (but the path can change when using other systems) and has a .conf extension.

Startup, restart and logs

To do this, you must get the executable to work. The nginx server can be configured only when it is started. The control is performed by calling the executable with the -s option. To do this, use the following entry:

Nginx -s signal

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

  1. Stop. Used for quick shutdown.
  2. Reload. The command is required to reload the configuration file. The matter is that any changes will not be applied while the file is working. And for them to take effect, a reboot is necessary. 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 available there. If it fails, it will roll back the changes and will work with the old settings. If everything went well, new workflows will be started, and the old one will be sent a request to complete.
  3. Quit. Applicable for smooth shutdown. Applicable if you need to wait until the current requests are finished.
  4. Reopen. Close and open the log files.

Using Utilities

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

Kill -s QUIT 1628

Let's say that we want to see a list of all running files. We use the ps utility to do this. The command will look like this:

Ps -ax | Grep nginx

That is, as you can see, when using additional tools, it is indicated that it is its application. And now let's concentrate on how nginx-tuning is done.

Structure of the configuration file

Installation and configuration nginx provides work with modules. They are configured using directives that are specified in the configuration file. They are simple and blocky. The first type of directives consists of a name and parameters that are separated by spaces, and their end is indicated by a semicolon - (;). Block has a similar structure. But in this directive, instead of the end, a set of additional instructions is placed, which are placed in braces ({directions}). If the names and parameters of other processes can be placed in them, then such constructions are already called context. An example is http, location and server.

Static content distribution

This is one of the most important tasks that faces the nginx configuration. The distribution of statistical content implies images and HTML pages (not dynamic). Let's say that we need a one-time job to configure a nix nginx cluster. Is it difficult to do this? No, and let's look at an example. Before proceeding to 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 / data / images directory contains images. The optimal nginx configuration in this case requires editing the configuration file, in which you need to configure the server block inside http. For support, two locations will also be used.

Implementation: server

So, first, we need to create the directories and place the files with the required extensions (in html you need to add content). Then open the configuration file. In it, by default, there are already several server blocks, which in mass are commented out. To achieve the optimal result, this process must be done with respect to all the components by default. Then add a new server block with this code:

Http {

Server {

}

}

The configuration file can work with several such blocks. But they should differ in their names and ports, through which the data is received.

Implementation: location

It is defined inside the server:

Location / {

Root / data / www;

}

The presence of the "/" sign is necessary to compare the received data and to see if there is such an address from the processed query here. If there is no problem, then specify the path / data / www to the required file, which is in this local system. If there is a match with several blocks, then the one with the longest prefix is selected. In the example above, its length is one, that is, the use will be solely if there are no "competitors". Now let's improve it:

Location / images / {

Root / data;

}

As you can determine, we are looking for images. And now let's combine all the work that was earlier, and the configuration at the moment looks like this:

Server {

Location / {

Root / data / www;

}

Location / images / {

Root / data;

}

}

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

The functioning of the example

So, when requests come that start with / images, then the server files from the corresponding directory will be sent to the user. If it is absent, information indicating error 404 will be transmitted. If you are configuring nginx on the local computer, then by requesting http: //localhost/images/example.png we will get a file whose location is /data/images/example.png. If you specify one "/" character, the search will be performed in the / data / www directory. But we just changed the configuration. To start it, it must be rebooted. To do this, use the nginx -s reload command. In the case when normal work is not possible, then in the files error.log and access.log located in the directive / usr / local / nginx / logs, you can search for the cause of the malfunction.

Creating a Simple Proxy Server

It can be said about nginx - setting this object is one of the frequent applications (and quite easy, among other things). Here the principle of the server is used, which accepts the request, and then redirects them to the necessary sites. After that, a response from them is expected, which directs them to the one who set the task. So let's look at an example of creating a base point. It will handle the requests of users and provide them with images from the local directory. So, to the http block, add one more server with the following content:

Server {

Listen 8080;

Root / data / up1;

Location / {

}

}

And now let's decipher for you: a simple server is created. It will listen on port 8080. Do not specify listen, then the server will run on the 80th. All requests within the local file system that are directed to the / data / up1 directory (of course, it will need to be created before) will be displayed. To be able to check there, you need to put the index.html file. By placing the root directive in the context of the server, 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 need a proxy_pass directive, for which the protocol, name, and port of the object will be specified as parameters (for local connection it will look like http: // localhost: 8080). The result is as follows:

Server {

Location / {

Proxy_pass http: // localhost: 8080;

}

Location / images / {

Root / data;

}

}

If you are reviewing the code and analyzing it, you may notice that the second location block was 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 configuration of the proxy server 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 it will need to be rebooted. And believe me, this is the simplest nginx-tuning. If you open a configuration file for a Vkontakte server or another large company, they will have more code than words in this article.

Similar articles

 

 

 

 

Trending Now

 

 

 

 

Newest

Copyright © 2018 en.unansea.com. Theme powered by WordPress.