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. , . , , . , . , . . , , .
  3. Quit. . , , .
  4. Reopen. -.

Unix ( kill). . ID. nginx.pid. , №134. :

kill -s QUIT 1628

, . ps. :

ps -ax | grep nginx

, , , . , nginx-.









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

, nginx. HTML- ( ). , nix nginx . ? , . , . , , . , /data/www HTML-. /data/images . nginx , server http. location.

: 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;

}

«/» , , . , /data/www , . , , . , , «». :

location /images/ {

root /data;

}

, . , , :

server {

location / {

root /data/www;

}

location /images/ {

root /data;

}

}

, №80. , : http://localhost/. ?

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 / {

}

}

: . 8080. listen, 80-. , /data/up1 (, ). index.html. root server location (, , ). -. proxy_pass, , , ( http://localhost:8080). :

server {

location / {

proxy_pass http://localhost:8080;

}

location /images/ {

root /data;

}

}

, , location . , . - :

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

root /data/images;

}

- :

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.




All Articles