Computers, Programming
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
Startup, restart and logs
Nginx -s signal
In this case, you can substitute such commands (should come from the user that launched the tool):
- Stop. Used for quick shutdown.
- 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.
- Quit. Applicable for smooth shutdown. Applicable if you need to wait until the current requests are finished.
- 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
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
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
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
Creating a Simple Proxy Server
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