centos installs nginx and configures load balancing



1. Install nginx

Installation tutorial, refer to: http://mp.weixin.qq.com/s/RVaRlRpHqZRjCaXGmOlfKw

2. Configuration of reverse agent

Modify the contents of nginx.conf file in the conf subdirectory under the deployment directory

  1. location / {    
  2.             #Set the host header and the real address of the client so that the server can obtain the real IP address of the client
  3.             proxy_set_header Host $host;    
  4.             #nginx non 80 port processing
  5.             #proxy_set_header Host $host:$server_port;     
  6.             #Get real IP
  7.             proxy_set_header X-Real-IP $remote_addr;    
  8.             #Get the real ip of the agent
  9.             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;    
  10.     
  11.             #Disable caching
  12.             proxy_buffering off;    
  13.             #Set the address of the reverse agent
  14.             proxy_pass http://192.168.1.1; (modified according to the actual situation)
  15.       }    



3. Load balancing configuration

By default, the upstream of nginx implements load balancing by polling. In this way, each request is allocated to different back-end servers in chronological order. If the back-end servers are down, they can be automatically eliminated. Another way is ip_hash: each request is allocated according to the hash result of accessing IP, so that each visitor accesses a back-end server fixedly, which can solve the problem of session.  

  1. #user nobody;  
  2. worker_processes 2;  
  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;  
  9.    
  10.    
  11. events {  
  12.   accept_mutex on; #Set the network connection serialization to prevent the occurrence of swarm alarm. The default is on
  13.   multi_accept on; #Set whether a process accepts multiple network connections at the same time. The default is off
  14.   worker_connections 1024;#Maximum connections
  15. }  
  16.    
  17.    
  18. http {  
  19.   include    mime.types;#File extension and file type mapping table, which is mainly used for static resources deployed on native nginx
  20.   default_type application/octet-stream;  
  21.    
  22.   #Log format
  23.   log_format main '$remote_addr - $remote_user [$time_local] "$request" '  
  24.            '$status $body_bytes_sent "$http_referer" '  
  25.            '"$http_user_agent" "$http_x_forwarded_for"';  
  26.    
  27.   access_log logs/access.log main;  
  28.    
  29.   sendfile    on;  
  30.   #tcp_nopush   on;  
  31.    
  32.   #keepalive_timeout 0;  
  33.   keepalive_timeout 65;#Connection timeout
  34.    
  35.   gzip on;  
  36.    
  37.   #Reverse proxy
  38.    
  39.   #[configuration 1] this configuration is a combination of [configuration 4] and [configuration 5]
  40.   #This configuration forwards the request to two WEB servers, allocates the target host according to the client IP, and allocates the traffic according to the weight
  41.   upstream app1 {  
  42.     ip_hash;  
  43.     server 192.168.14.132:8080 weight=5;  
  44.     server 192.168.14.133:80 weight=3;  
  45.   }  
  46.    
  47.   #[configuration 2]
  48.   #Default load balancing configuration, nginx applies HTTP load balancing to distribute requests.   
  49.   #upstream app1 {  
  50.   #  server 192.168.14.132:8080;  
  51.   #  server 192.168.14.133:80;  
  52.   #}  
  53.    
  54.   #[configuration 3]
  55.   #With minimum connection load balancing configuration, nginx will try not to use busy servers, but distribute new requests to servers that are not too busy.   
  56.   #upstream app1 {  
  57.   #  least_conn;  
  58.   #  server 192.168.14.132:8080;  
  59.   #  server 192.168.14.133:80;  
  60.   #}  
  61.    
  62.   #[configuration 4]
  63.   #The session persistence configuration uses ip-hash, and the IP address of the client is used as the hash key
  64.   #To determine which server in the server group should be selected for client requests.   
  65.   #This method ensures that requests from the same client are always directed to the same server unless the server is not available.   
  66.   #upstream app1 {  
  67.   #  ip_hash;  
  68.   #  server 192.168.14.132:8080;  
  69.   #  server 192.168.14.133:80;  
  70.   #}  
  71.    
  72.   #[configuration 5]
  73.   #Weighted load balancing configuration further affects nginx load balancing algorithm by using server weight.   
  74.   #Servers that are not weighted mean that all specified servers are considered equally qualified for a particular load balancing method.   
  75.   #upstream app1 {  
  76.   #  ip_hash;  
  77.   #  server 192.168.14.132:8080 weight=3;  
  78.   #  server 192.168.14.133:80 weight=2;  
  79.   #  server 192.168.14.134:80;  
  80.   #  server 192.168.14.135:80;  
  81.   #}  
  82.    
  83.    
  84.   server {#Multiple server s can be configured to listen to different IP and different ports
  85.     listen    80;#Listening port
  86.     server_name localhost;#Listening server
  87.    
  88.     #charset koi8-r;  
  89.    
  90.     #access_log logs/host.access.log main;  
  91.    
  92.     #The anti skew bar represents all connections. The purpose of this configuration is to give all connections to the upstream agent named app1 for load balancing
  93.     location / {      
  94.     #Set the host header and the real address of the client so that the server can obtain the real IP address of the client
  95.         proxy_set_header Host $host;  
  96.   
  97.         proxy_set_header X-Real-IP $remote_addr;  
  98.         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  
  99.   
  100.         #Disable caching
  101.         proxy_buffering off;  
  102.   
  103.        #Configuration of reverse agent (the address is configured according to the actual situation)
  104.        proxy_pass http://app1;  
  105.     }  
  106.    
  107.     #Picture file path. Generally speaking, static files will be deployed on the local machine to speed up the response
  108.     #Multiple such location s can be configured to meet various requirements
  109.     location ~\.(gif|jpg|png)$ {  
  110.       root /home/root/images;  
  111.     }  
  112.    
  113.     location ~\.(iso|zip|txt|doc|docx)$ {  
  114.       root /home/root/files;  
  115.     }  
  116.    
  117.    
  118.     #error_page 404       /404.html;  
  119.    
  120.     # redirect server error pages to the static page /50x.html  
  121.     #  
  122.     error_page  500 502 503 504 /50x.html;  
  123.     location = /50x.html {  
  124.       root  html;  
  125.     }  
  126.    
  127.    
  128.     #FastCGI is CGI full name is "Common Gateway interface"
  129.     #For me, use Tomcat instead. Please ignore this configuration.   
  130.     #location ~ \.php$ {  
  131.     #  root      html;  
  132.     #  fastcgi_pass  127.0.0.1:9000;  
  133.     #  fastcgi_index index.php;  
  134.     #  fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;  
  135.     #  include    fastcgi_params;  
  136.     #}  
  137.    
  138.     #Add a blacklist to prevent xxx from accessing specific files
  139.     # concurs with nginx's one  
  140.     #  
  141.     #location ~ /\.ht {  
  142.     #  deny all;  
  143.     #}  
  144.   }  
  145.    
  146.    
  147.   # another virtual host using mix of IP-, name-, and port-based configuration  
  148.   #  
  149.   #server {  
  150.   #  listen    8000;  
  151.   #  listen    somename:8080;  
  152.   #  server_name somename alias another.alias;  
  153.    
  154.   #  location / {  
  155.   #    root  html;  
  156.   #    index index.html index.htm;  
  157.   #  }  
  158.   #}  
  159.    
  160.    
  161.   # HTTPS server  
  162.   #  
  163.   #server {  
  164.   #  listen    443 ssl;  
  165.   #  server_name localhost;  
  166.    
  167.   #  ssl_certificate   cert.pem;  
  168.   #  ssl_certificate_key cert.key;  
  169.    
  170.   #  ssl_session_cache  shared:SSL:1m;  
  171.   #  ssl_session_timeout 5m;  
  172.    
  173.   #  ssl_ciphers HIGH:!aNULL:!MD5;  
  174.   #  ssl_prefer_server_ciphers on;  
  175.    
  176.   #  location / {  
  177.   #    root  html;  
  178.   #    index index.html index.htm;  
  179.   #  }  
  180.   #}  
  181.    
  182. }  

4. After configuration, remember to execute the following command to take effect

  1. nginx -s reload  
5. Nginx built in variable meaning

  1. Name    Edition    Description (variable list from file ngx_http_variables )   
  2. $args    1.0.8    Parameters in request;   
  3. $binary_remote_addr    1.0.8    Binary representation of remote address   
  4. $body_bytes_sent    1.0.8    Message body bytes sent   
  5. $content_length    1.0.8    HTTP In the request message"Content-Length";   
  6. $content_type    1.0.8    In the request message"Content-Type";   
  7. $document_root    1.0.8    Set the value for the root path of the current request;   
  8. $document_uri    1.0.8    And $uri identical;   
  9. $host    1.0.8    In the request information"Host",If not in the request Host Line, equal to the server name set;   
  10. $hostname    1.0.8       
  11. $http_cookie    1.0.8    cookie information   
  12. $http_post    1.0.8       
  13. $http_referer    1.0.8    Reference address   
  14. $http_user_agent    1.0.8    Client agent information   
  15. $http_via    1.0.8     Last access server's Ip Address.   
  16. $http_x_forwarded_for    1.0.8     Equivalent to a network access path.   
  17. $is_args    1.0.8       
  18. $limit_rate    1.0.8    Restrictions on connection rate;   
  19. $nginx_version    1.0.8       
  20. $pid    1.0.8       
  21. $query_string    1.0.8    And $args identical;   
  22. $realpath_root    1.0.8       
  23. $remote_addr    1.0.8    Client address;   
  24. $remote_port    1.0.8    Client port number;   
  25. $remote_user    1.0.8    Client user name for authentication;   
  26. $request    1.0.8    User request   
  27. $request_body    1.0.8       
  28. $request_body_file    1.0.8    Local file name sent to back end   
  29. $request_completion    1.0.8       
  30. $request_filename    1.0.8    Current requested file pathname   
  31. $request_method    1.0.8    The requested method, such as"GET","POST"etc.;   
  32. $request_uri    1.0.8    Requested URI,Band parameters;   
  33. ...  

Tags: Nginx network Session PHP

Posted on Fri, 03 Apr 2020 14:27:54 -0700 by Okami