Nginx installation, configuration, and related presentations

I. Introduction

  Nginx is a high-performance HTTP server and reverse proxy, the Russians developed the first version was released in 2004 October 4. It is a lightweight Web server, which is characterized by possession of less memory, high concurrency, load balancing provides a very convenient configuration. Several major domestic Internet companies are also in use, is fire, very powerful, now let us enjoy its presence.

 

Two, Nginx configuration and installation environment

  Configuring nginx on 2.1 windows

  Download: http: //nginx.org/en/download.html

  

  You can download the latest stable version, currently 1.16.1. Mainline version belongs to the developer version. After downloading decompression can be used, pure green, the structure as shown below:

  

  Double-click nginx.exe feel about flashing, Task Manager to view the process, there will be two nginx process, the official explanation is: a process in which the main process, another process for the work process.

  If it does not, you can see why in the error log (logs \ error.log). If no log information, you need to see reason in the windows event log. Usually occupied port, or configuration file errors.

  You can also console windows, for nginx to manage with the following command (the path to switch to the directory where nginx). 

1 Start nginx        // start the service (not a double process) 
2 nginx -s STOP      // Quick Stop Service (forced off) 
3 nginx -s quit      // Exit Service 
4 nginx -s reload    // reload (with the new configuration heavy-duty service, similar restarted) 
5 nginx -s Reopen    // log splitting (open a new log file) 
6 nginx -t           // verify the configuration file is correct

  Because it is green, the basic body is not written for the windows, Nginx does not provide support services for Windows. To run as a service, it needs a gadget WSW (Windows Service Wrapper) to the non-focus, we will not elaborate here.

  2.2 linux installation Nginx (In Case CentOS 7)

  (1) As used herein, EPEL installation (quick and easy), you need to install EPEL warehouse, enter the following command to open a terminal

sudo  yum  install warm-release

  (2) install nginx, enter the installation command, when prompted, answer yes.

sudo yum install nginx

  (3) Start nginx

sudo systemctl start nginx

  Enter the browser (requires centos mounting interface) on localhost, see the following (with epel install, is this picture), installation is successful.

  If you want to access outside the network, and the system has run a firewall, you need to add http and https only be allowed to communicate. You can run the following command:

1 sudo firewall-cmd --permanent --zone=public --add-service=http 
2 sudo firewall-cmd --permanent --zone=public --add-service=https
3 sudo firewall-cmd --reload

  Nginx service set to start when you start the system, you can run the command

sudo systemctl enable nginx

  Even to get here, Nginx default installation path is / etc / nginx

 

Three, Nginx reverse proxy service configuration

  The following instructions, for convenience, are based on an example of the windows. Nginx configuration file is in the root directory, nginx.conf file in the conf directory.

  First is that the right thing website, nginx after successful installation, open the page, you can open the equivalent of a stand, nginx is configured by default. The default is only one station, as some virtual hosts (non-virtual machine) that a server has multiple sites. To achieve this function, in nginx configuration it is very simple.

  Because with the same host name (all local, use localhost), need to use a different port number, just in the http module, then add keys to a server, configuration is as follows:

1  server {# this is part of the default server, showing only the main content.
2      the listen 80 ;
 . 3      server_name localhost ;
 . 4      LOCATION / {
 . 5          the root HTML ;
 . 6          index index.html index.htm ;
 . 7      }
 . 8      # N items omitted
 9  }
 10  # define ourselves a server, which copies a root here nginx html directory to the directory of the current directory, rename html1.
11 # h1 modify the contents of the index.html in html1 is to nginx 1 is available for purchase !
12 is  Server {
 13 is      the listen 8090 ;
 14      LOCATION / {
 15         root html1;
16         index  index.html;
17     }
18 }

  Under restart nginx service, respectively access localhost, localhost: 8090 to see the results.

  ========================= 50 equals gorgeous dividing line ================= ========

  Here entered, take a look at how to configure a reverse proxy is, quite simply, a line of configuration to get, look at the configuration.

{Server 
    the listen        80 ;
     server_name localhost ;
     LOCATION / { 
        #root HTML ;
         #index index.html index.htm ;
         proxy_pass HTTP: // localhost: 8090 ;   # Add to this configuration, two comment out above. 
    } 
}

  Directly configure a reverse proxy port here in 8090 on a step of the station, try to look at the results. Remember to restart nginx service (convenient presentation, other sites are also possible)

  You're not wrong, it is so easy to get. HAHA ..

 

Four, Nginx load balancing configuration

  Load balancing, one of Nginx common core functionality. Configure load balancing, you need to use to configure upstream item, I will not speak nonsense, direct look at the configuration, as follows:  

. 1  upstream MyApp {
 2      Server localhost: 8090 ;
 . 3      Server localhost: 8080 ;
 . 4      Server localhost: 8070 ;
 . 5  }
 . 6  
. 7  Server {
 . 8      the listen 80 ;
 . 9      server_name localhost ;
 10      LOCATION / {
 . 11          proxy_pass HTTP: // MyApp ;   # same with this configuration, the entry point upstream of the symbol name, can define their own. 
12      }
 13  }
 14  
15  # define several multi-server for a load. For demonstration purposes, all on a single machine to achieve, and the actual work is not possible case.
16  Server {
 . 17     8090 the listen ;
 18 is      LOCATION / {
 . 19          the root HTML1 ;
 20 is          #index index.html ; # can not be specified, the default is index.html or index.htm 
21 is      }
 22 is  }
 23 is  
24  Server {
 25      the listen 8080 ;
 26 is      LOCATION / {
 27          the root HTML2 ; #nginx small hand flick, a multi-copy parts of the root directory, change the contents of index.html, differ to
 28          index index.html ;
 29      }
 30  }
 31 is  
32  Server {
 33 is      the listen 8070 ;
 34 is     location / {
35         root html3;
36         index  index.html;
37     }
38 }

  Nginx restart the service, access localhost, refresh a few times to see if the effect is not changed at each visit. This is the most basic load algorithms: Round Robin .

  Polling algorithm: each request individually assigned chronologically to different application server, if the application server is down, automatically remove the rest of the polling continues.

  2. Weight Algorithm: configure weights for polling probability, the larger the weight, the greater the probability of access. Directly upstream configuration, after the related services can be added, as follows:

1 upstream myapp {
2     server localhost:8090 weight=6;
3     server localhost:8080;
4     server localhost:8070;
5 }

  3.ip_ hash algorithm: request access by ip hash result of the distribution, so that each visitor to access a fixed application server, session sharing is mainly used to solve the problem, its configuration is as follows:

1 upstream myapp {
2     ip_hash;
3     server localhost:8090;
4     server localhost:8080;
5     server localhost:8070;
6 }

  4. Minimum Link: forwarding the request to the application server fewer connections, to achieve a better load balance, the rear end of the cluster is traversed Comparative conns / weight of each application service, the selected minimum value. Its configuration is as follows: 

1 upstream myapp {
2     least_conn;
3     server localhost:8090;
4     server localhost:8080;
5     server localhost:8070;
6 }

   By configuring the different options in the upstream parameters, you can achieve different functions and effects, such as not to participate in a service desk application load, can be configured as follows:

1 upstream myapp {
2     server localhost:8090;
3     server localhost:8080;
4     server localhost:8070 down;
5 }

  Application server plus back down means that the server does not participate in the current load. I would not have access to such a server. The configuration items and functions are as follows:

  a. weight

    - weight, that is, the greater the weight, the greater the weight load of weight.

  b. down

    - indicates the current server time being does not participate in load.

  c. max_fails

    - charge allowable number of failed requests, the default is 1. When the maximum number of attempts to return proxy_next_upstream module defined error.

  d. fail_timeout

    After -max_fails failed, pause time.

  e. backup

    - All other non-backup server is down or busy, request backup server. Basically this server lightest pressure.

 

5. Each module configuration instructions

  The main explanation global configuration, events and http module module.

  Global Configuration

1 #user  nobody;
2 worker_processes  1;
3 
4 #error_log  logs/error.log;
5 #error_log  logs/error.log  notice;
6 #error_log  logs/error.log  info;
7 
8 #pid        logs/nginx.pid;
  • user specified nginx worker processes running users and user groups, nobody default account run.
  • Worker_processes specified number of child processes nginx open. In linux, the default is auto. It can be adjusted according to the situation.
  • Error_log defined position error log file and the output level (debug / info / notice / warn / error / crit). The default is crit.
  • pid specifies the process id of the location of the file.

 

  events module

1 events {
2     worker_connections  1024;
3 }
  • worker_connections maximum number of connections that can be received simultaneously. It is noteworthy that the maximum number of connections is worker_processes and joint decision.

 

  http modules, sub-modules described here only in the common configuration server. The above description has been said upstream load balancing.

 1 server {
 2     listen       80;
 3     server_name  localhost;
 4 
 5     #charset koi8-r;
 6 
 7     #access_log  logs/host.access.log  main;
 8 
 9     location / {
10         root   html;
11         index  index.html index.htm;
12         #proxy_pass http://localhost:8080;
13         #proxy_pass http://myapp;
14     }
15 }
  • a virtual host server configuration, a multiple can be configured http server.
  • listen listening port number
  • server_name hostname ip address or domain name may be separated by spaces between a plurality of configurations.
  • charset for setting the location / root sites default encoding format.
  • access_log specify an access log that virtual host server storage path.
  • location is a very important module for configuring routing access information. It will be linked to a reverse proxy, load balancing and other functions.
    • root specifies the root directory of web access
    • When accessing the specific resource index is not specified, the default display a list of files
    • proxy_pass reverse proxy configuration, including load balancing point upstream module.

  Finally, he said in a separate module at this location, and it itself is support for regular, such as access to some of the static allocation of resources is achieved through here.

  location / match which means access to the root directory.

  . Location location ~ \ (gif | jpg | png) $ match the picture static resources, you can specify the directory where the picture: 

1 location ~ \.(gif|jpg|png)$ {
2     root data/images;
3 }

  location ~ \. (css | js) $ match css, js files. Generally do reverse proxy, there will be css, js files can not be loaded, you can use this:

1 location ~\.(css|js)$ {
2     proxy_pass http://localhost:809;
3 }   

  Specify the error page, also used this

1 error_page 404 /404.html;
2 location = /40x.html {
3 }
4 error_page 500 502 503 504 /50x.html;
5 location = /50x.html {
6     root   html;
7 }

 

VI Conclusion

  Stop here, only describes some common features and configuration, under normal circumstances is good enough, Nginx lean, mean, function is far more than that, interested students can study in depth.

Guess you like

Origin www.cnblogs.com/cheny21/p/11506917.html