Details on Nginx HTTP load balancing and reverse agent configuration

  • 2020-05-12 06:48:32
  • OfStack

At present, most websites with high concurrency use Nginx as proxy server and cache to support high concurrency. I also configured a simple agent with nginx before, so I have time to share the integration process with you today, but most of it is also a resource found on the Internet.

The complete reverse agent code for nginx is shown below:


[root@data conf]# vim nginx.conf 
user www www; 
worker_processes 10; 
  
error_log /var/log/nginx/nginx_error.log; 
  
pid    logs/nginx.pid; 
  
worker_rlimit_nofile 65535 
events { 
  use epoll; 
  worker_connections 65535; 
} 
  
http { 
  include    mime.types; 
  default_type application/octet-stream; 
   
  server_names_hash_bucket_size 128; 
  client_header_buffer_size 32k; 
  large_client_header_buffers 4 32k; 
  sendfile    on; 
  tcp_nopush   on; 
  
  keepalive_timeout 65; 
  fastcgi_connect_timeout 300; 
  fastcgi_send_timeout 300; 
  fastcgi_read_timeout 300; 
  fastcgi_buffer_size 64k; 
  fastcgi_buffers 4 64k; 
  fastcgi_busy_buffers_size 128k; 
  fastcgi_temp_file_write_size 128k; 
  
  gzip on; 
  gzip_min_length 1k; 
  gzip_buffers   4 16k; 
  gzip_http_version 1.0; 
  gzip_comp_level 2; 
  gzip_types    text/plain application/x-javascript text/css application/xml; 
  gzip_vary on; 
  
  client_max_body_size 300m;  # Maximum number of bytes per file allowed for client request  
  client_body_buffer_size 128k; # The buffer agent buffers the maximum number of bytes requested by the client  
  proxy_connect_timeout 600;  # Initiate a handshake with the backend server to wait for the response timeout  
  proxy_read_timeout  600;  # After a successful connection, wait for the back-end server response time and wait in the back-end queue  
  proxy_send_timeout 600; # The back-end server data return time, is in the specified time the back-end server must transfer all the Numbers  
  proxy_buffer_size 16k; # The proxy requests the cache, which holds the user's information for use nginx Do rule processing, 1 As long as you can save the header information  
  proxy_buffers 4 32k; # Ibid. Tell nginx Save a few for a single use Buffer What's the maximum space  
  proxy_busy_buffers_size 54k; # If the system is busy you can apply for a few larger ones proxy_buffer 
  proxy_temp_file_write_size 64k; # Cache the temporary file size  
  
  upstream php_server_pool { 
  server 192.168.1.100:80 weight=4 max_fails=2 fail_timeout=30s; 
  server 192.168.1.101:80 weight=4 max_fails=2 fail_timeout=30s; 
  server 192.168.1.102:80 weight=4 max_fails=2 fail_timeout=30s; 
} 
  upstream message_server_pool { 
  server 192.168.1.103:3245; 
  server 192.168.1.104:3245 down; 
} 
  upstream bbs_server_pool { 
  server 192.168.1.105:80 weight=4 max_fails=2 fail_timeout=30s; 
  server 192.168.1.106:80 weight=4 max_fails=2 fail_timeout=30s; 
  server 192.168.1.107:80 weight=4 max_fails=2 fail_timeout=30s; 
  server 192.168.1.108:80 weight=4 max_fails=2 fail_timeout=30s; 
} 
# The first 1 Virtual host, reverse proxy php_server_pool This set of servers  
  server { 
    listen    80; 
    server_name www.chlinux.net; 
    access_log /var/log/nginx/www.chlinux.net_access.log main; 
    location / { 
# If the back-end server returns 502 , 504 , execution timeout and other errors will automatically forward the request to upstream Another in the load balancing pool 1 A server to achieve failover.  
   proxy_next_upstream http_502 http_504 error timeout invalid_header; 
   proxy_pass http://php_server_pool; 
   proxy_set_header Host www.chlinux.net; 
   proxy_set_header X-Forwarded-For $remote_addr; 
    } 
  } 
# The first 2 Three virtual hosts  
  server { 
    listen    80; 
    server_name bbs.chlinux.net; 
    access_log /var/log/nginx/www.chlinux.net_access.log main; 
# access http://bbs.chlinux.net/message/*** Address, reverse proxy message_server_pool This set of servers  
    location /message/ { 
     proxy_pass http://message_server_pool; 
     proxy_set_header Host $host; 
    }   
# Visit in addition to /message/ In addition to the http://bbs.chlinux.net/*** Address, reverse proxy php_server_pool This set of servers  
  location /message/ { 
     proxy_pass http://bbs_server_pool; 
     proxy_set_header Host $host; 
   proxy_set_header X-Forwarded-For $remote_addr; 
    }   
   } 
# The first 3 Three virtual hosts  
  server { 
    listen    80; 
    server_name forum.chlinux.net; 
    access_log /var/log/nginx/www.chlinux.net_access.log main; 
    location / { 
  
     proxy_next_upstream http_502 http_504 error timeout invalid_header; 
     proxy_pass http://php_server_pool; 
     proxy_set_header Host www.chlinux.net; 
     proxy_set_header X-Forwarded-For $remote_addr; 
    } 
  } 
}

As shown above, you have seen how nginx is configured for load balancing multiple domains. The pustream directive is used to set up a set of proxy servers that can be used in the proxy_pass and fastcgi_pass directives. The server directive in the upstream module is used to specify the name and parameters of the back-end server. The name of the server can be 1 domain name, 1 IP address, port number, or UNIX Socket

The nginx reverse proxy can be configured to separate dynamic and static web pages, which enables dynamic PHP and other program web pages to access PHP web server, and cache pages, images, javascript, CSS, Flash to access squid and other caching servers.


Related articles: