How to use Nginx HTTP achieve load balancing

The following simple example is used to configure nginx load balancing. The main functions are:

user  www www;

worker_processes 10;
 
#error_log  logs/error.log;

#error_log  logs/error.log  notice;

#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

worker_rlimit_nofile 51200;

events

{

      use epoll;

      worker_connections 51200;

}

http

{

      include      conf/mime.types;

      default_type  application/octet-stream;

      keepalive_timeout 120;

      tcp_nodelay on;

              server  192.168.5.2:80;

              server  192.168.5.3:80;

              server  192.168.5.4:80;

              server  192.168.5.5:80;

      }

              server  192.168.5.7:8080;

              server  192.168.5.7:8081;

              server  192.168.5.7:8082;

      }

      server

      {

              listen  80;

              location / {

                      proxy_set_header  Host            $host;

                      proxy_set_header  X-Real-IP        $remote_addr;

                      proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;

              }

                                ‘”$status” $body_bytes_sent “$http_referer” ‘

                                ‘”$http_user_agent” “$http_x_forwarded_for”‘;

      }

      server

      {

              listen  80;

              location / {

                      proxy_set_header  Host            $host;

                      proxy_set_header  X-Real-IP        $remote_addr;

                      proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;

              }

                                ‘”$status” $body_bytes_sent “$http_referer” ‘

                                ‘”$http_user_agent” “$http_x_forwarded_for”‘;

}

}

Here the use of two main modules:

1. HTTP load balancing module (HTTP upstream), explains some fields:

  • server: Specifies the name of the back-end server and a number of parameters. You can use the domain name, IP, port or Unix socket. If you specify a domain name, the first resolved to IP.
  • upstream: This field is provided with a group of servers. This field can be placed in and proxy_pass fastcgi_pass instruction as separate entities. They may be different listening port of the server, may be listening TCP sockets and Unix servers.

2. HTTP proxy module (HTTP Proxy)

The module may forward the request to another server.

  • proxy_pass: This command sets the proxy server address and mapping of URI. The host name or IP address of the form plus the port number can be used.
  • proxy_set_header: This directive allows you to redefine the field or sent to the proxy server is added to the request header. This value may be text, variables, or combinations thereof.

Guess you like

Origin www.linuxidc.com/Linux/2020-03/162502.htm