Detail the Nginx reverse agent and load balancing deployment guide

  • 2020-05-12 06:59:51
  • OfStack

The Nginx reverse agent and load balancing deployment guidelines are as follows:

1. Install

(1) from Nginx website to download page http: / / nginx org/en/download html) Nginx download the latest version (currently 1.5.13 version) installation package;

2) copy to the deployment directory after unzip.

2. Start and stop Nginx

Currently, Nginx only supports command line operation. Before operation, enter the Dos command environment and enter the Nginx deployment directory.

1) start Nginx: start nginx

2) stop Nginx: nginx-s stop

3) restart after configuration modification: nginx-s reload

These three commands can be made into bat files and placed in the deployment directory for easy follow-up.

start nginx.bat file content: start nginx

stop nginx.bat contents: nginx-s stop

reload nginx.bat file content: nginx-s reload

3. Reverse agent configuration

Modify the nginx.conf file (e.g. nginx-1.5.13 \conf\ nginx.conf) of the conf subdirectory in the deployment directory to adjust the configuration.

Example reverse proxy configuration:


location / {

    # Set the host header and the client real address so that the server can get the client real IP

       proxy_set_header Host $host;

       proxy_set_header X-Real-IP $remote_addr;

    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

       # Disable caching 

       proxy_buffering off;

       # Set the address of the reverse proxy 

       proxy_pass http://192.168.1.1;   

   }

The agent address is changed according to the actual situation.

4. Load balancing configuration

By default, upstream of nginx implements load balancing in the form of polling, in which each request is distributed to a different backend server one by one in chronological order. If the back-end server down is dropped, it can be automatically eliminated.

The other way is ip_hash: each request is allocated according to the hash result of accessing ip, so that each visitor has a fixed access to one back-end server, which can solve the problem of session.

Example load balancing configuration:


upstream backend {

       #ip_hash;

       server 192.168.1.251;

       server 192.168.1.252;

       server 192.168.1.247;

     }

server {

    listen    80;

    server_name trffweb; 

    location / {

       # The address of the reverse proxy 

       proxy_pass http://backend;  

    }

}

The Upstream name and server address are changed according to the actual situation.

5. Complete configuration example

nginx. conf:


worker_processes 1;

events {

  worker_connections 1024;

}

http {

  include    mime.types;

  default_type application/octet-stream;

  sendfile    on;

  keepalive_timeout 65;

upstream backend {

       #ip_hash;

       server 192.168.1.251;

       server 192.168.1.252;

       server 192.168.1.247;

     }

  server {

    listen    80;

    server_name 2; 

    location / {

    # Set the host header and the client real address so that the server can get the client real IP

       proxy_set_header Host $host;

       proxy_set_header X-Real-IP $remote_addr;

    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

       # Disable caching 

       proxy_buffering off;

       # The address of the reverse proxy 

       proxy_pass http://backend;  

    }

  }
}


Related articles: