nginx configure load balancing

Two days ago, I was studying load balancing, and then I saw nginx. Currently, it is a reverse proxy. The load balancing nginx used is indeed relatively large. For example, the Tengine used by Ali was also changed based on nginx. Of course, nginx is also used in our project.

Nginx is a reverse proxy server, the purpose is to forward http requests. In this way, the request can be forwarded without knowing the server address. Nginx can be understood as a middleman, the user operates the client, forwards it to the backend through nginx, and the backend request processing is returned to the user.

The download address (windows) is posted here: http://nginx.org/download/nginx-1.14.0.zip  


Install the docker container I use under linux:

   The first step is to pull down the nginx instance from docker docker pull nginx

   The second step is to run the instance by setting the port, etc. (if the server occupies port 80, please set xx to other unoccupied ports) docker run -p xx : 80 --name nginx -v $PWD/www:/www -v $ PWD/logs:/wwwlogs -d nginx

  If you install under linux and don't know docker, please install docker under linux by yourself.

After the installation is complete, you can enter the instance and configure the reverse proxy and load balancing of nginx;

(Load balancing: In order to reduce the pressure on the server, the server is randomly accessed by setting the weight. When a user accesses the interface, the pressure is very small, but the pressure of 1 million people accessing the interface is very high. The load balancing plays the role of Jiangzi. , you can configure multiple servers to make http requests).

Next, start configuring the nginx reverse proxy:

Open the nginx.conf file under conf,

I won't look at the others for the time being, here I will focus directly on the key points.

#This is the target server address and port number that needs to be forwarded 
upstream mynginx{ server localhost:
8080; #Apache } ## Start www.quancha.cn ## server { listen 80; server_name localhost; index index.html index.htm index.php; ## send request back to apache ## location / {
     #The server load balancing that needs to forward requests is also configured like this proxy_pass http:
//mynginx; #Proxy Settings proxy_redirect off; #Whether to jump proxy_set_header Host $host; #Request the host to be forwarded proxy_set_header X -Real- IP $remote_addr;#The requested remote address can be seen in the browser header, not explained one by one proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504; proxy_max_temp_file_size 0; proxy_connect_timeout 90 ; #Connect the previous server timeout time proxy_send_timeout 90 ;#Timeout time for requesting forwarding data packets proxy_read_timeout 90 ;#read timeout proxy_buffer_size 4k; # buffer size proxy_buffers 4 32k; # proxy_busy_buffers_size 64k; # #proxy_buffers buffer, the average web page is below 32k proxy_temp_file_write_size 64k; ##Buffer size under high load (proxy_buffers*2) } }

This configuration can play the role of a reverse proxy

 

Similar to load balancing, configure multiple tomcat addresses, here is the pseudo-cluster method

#Configure multiple servers (only different ports on one server here) 
upstream mysvr { #The
    weight ratio is set to 1:3, which means that the user requests the server four times, and accesses port 8082 three times on average and port 8081 once, so as to achieve The role of server balance (high concurrency) server
127.0.0.1:8081 weight=1; server 127.0.0.1:8082 weight=3 ; #Hot backup }
server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;
        #access_log  logs/host.access.log  main;

     #Static files, nginx handles it by itself
        location ~ ^/(images|javascript|js|css|flash|media|static)/ {
            
            #Expire 30 days, static files are not updated very much, the expiration can be set larger,
            #If it is updated frequently, it can be set smaller.
            expires 30d;
        }
    location   ~*^.+$ { #Request url filtering, regular matching, ~ is case-sensitive, ~* is case-insensitive.
           #root path; #root directory
           #index vv.txt; #Set the default page
           proxy_pass http: // mysvr; #Request to the list of servers defined by mysvr
           #deny 127.0.0.1 ; #denied ip
           #allow 172.18.5.54 ; #allowed ip           
        }

        location / {
            root   html;
            index  index.html index.htm;
        }
}

Go down and configure server{ }, which is similar to reverse proxy configuration. It should be noted here that reverse proxy is more suitable for accessing static resources (reverse to static resource server), and load balancing is more suitable for back-end server clusters.

 

At this point, the simple configuration of Nginx is finished, and record your learning results! The above are only my own opinions. If you have any objections, you can leave a message in the comment area. Please indicate the source for reprinting.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325649189&siteId=291194637