(detailed) distributed architecture: preliminary study on Nignx installation, use and load balancing

Forward proxy

  • Proxy is the client. When the client needs to access the remote server, sometimes the intermediate proxy server is used to replace the client access. The client needs to make certain settings (similar to connecting to the Internet with VPN)

Reverse proxy

  • The proxy is that the server processes the response of the target server through the proxy server, and the client does not need any settings

Nginx installation and use

The core of lightweight and high-performance web server is load balancing

The following is the detailed process of installing and using Nginx

  • 1.1 preparation

    • Installation dependent environment

      Install gcc-c++

      yum install gcc-c++ 
      
    • Install module dependent Libraries

      Here I provide the yum installation and download address

  • 1.2 installation of Nginx

    Download address Select the second linux version

  • 1.3 decompression and compilation

    Unzip other tar files first GZ finally decompress Nginx

    • After decompression, enter each decompressed module into its decompression directory and perform the following operations

      1. 	./config
      2. 	make
      3. 	make install
      

      Note: if/ If config doesn't work, use it/ configure

  • 1.4 start Nginx

    • Directly enter the command usr/local/nginx/sbin/nginx without entering the directory
    notes:If an error occurs:error while loading shared libararies: ```libpcre.so.1```cannot open shared object file:No such file or directory
    

    You only need to do the following

    Command:

    whereis libpcre.so.1

    Libpcre will find so. Copy the path of 1

    ln -s the path just copied here / lib64 / libpcre so. one

    Finally, the figure below shows that Nginx is started successfully

    • Nginx start stop command

      1. Start / usr/local/nginx/sbin/nginx

      2. Stop / usr/local/nginx/sbin/nginx -s stop

      3. Restart: / usr/local/nginx/sbin/nginx -s reload

      4. Check nginx Conf is legal: / usr/local/nginx/sbin/nginx-t

‚Äč

  • 1.5 Nginx configuration file ------ nginx conf

    It is a key profile for reverse proxy and load balancing

    • file structure
      • Global block
        • User settings
        • log setting
      • events block
        • Specify the number of connections
      • http block
        • Configure multiple server s
        • Configure the reverse proxy function to provide load balancing
      • server block
        • Configure relevant parameters of virtual host
      • location block
        • Request URL filtering, using regular matching

Build reverse proxy

It should be noted that Nginx only processes static content, and dynamic content should be handed over to application servers such as background Tomcat for processing

  • Implement reverse proxy

    For nginx The configuration of each conf is described in detail below

    	upstream itripbiz_server{
    		//Set the access port and ip address of the background server, which is placed on the local machine  
            server 127.0.0.1:8080;
        }
    
        server {
            listen       80;
    		//After setting the ip address and domain name mapping of nginx server, access nginx through fuchen to access tomcat
            server_name fuchen;
    		//Set the storage path of nginx static pages
            root /data/itrip/itripfront;
    		//Set the page of nginx default request
            index index.html;
    
    
            #charset koi8-r;
    
            #access_log  logs/host.access.log  main;
    
            location / {
    			//The first two lines set the ip address of the access user, otherwise the background will see that it is the nginx proxy address
              proxy_set_header Host $host;
              proxy_set_header X-Real-IP $remote_addr;
    		  //The url of setting request background server (tomcat) must be consistent with the upstream above
              proxy_pass http://itripbiz_server;
             }
    
    
    	//Cache static files
    	location ~ .*\.(gif|jpg|png|html|mp3|ttf|eof|woff|woff2|svg|eot|swf|jpeg|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp5|webm)${
                    expires 6h;
            }
    	//Cache js css
    	location ~ .*\.(js|css)?${
                    expires 2h;
            }
    	//Set access log
    	access_log /data/logs/nginx/itrip.log;
    

    Then enter the nginx check file command above to check whether there are errors. If most of them do not create the corresponding directory, there is no problem trying after creation

Load balancing

  • polling

    It must occur on at least two servers, so add a tomcat and then deploy the same as the first war package to modify the port for access

	upstream itripbiz_server{
		server 127.0.0.1:8082;
        server 127.0.0.1:8080;
    }
  • Hot backup

As shown in the following figure: take the server on port 8080 as the backup server. Note: at this time, the request will only enter 8082. Only when the 8082 server goes down will it be automatically backed up to 8080, which is the basis for realizing high availability

	upstream itripbiz_server{
		server 127.0.0.1:8082;
        server 127.0.0.1:8080 backup;
    }
  • Weight

    Note: this case is only applicable to high concurrency scenarios, that is, when the number of requests is large, we can see the effect significantly. Generally, we set large weights for those servers with high configuration in order to make full use of their high performance

    	upstream itripbiz_server{
    		server 127.0.0.1:8082;
            server 127.0.0.1:8080 weight=2;
        }
    
  • IP address hash (solve the session sharing of Tomcat)

    According to the ip address requested by the client, it is allocated to the specified tomcat server, which solves the problem of session sharing

    	upstream itripbiz_server{
    		ip_hash;
    		server 127.0.0.1:8082;
            server 127.0.0.1:8080 weight=2;
        }
    

summary

Just started, Nginx is not very familiar with some places. Welcome to give some advice

Note: from now on, the author will embark on the road of distributed architecture. Welcome to join us!

Tags: Linux Nginx Distribution

Posted by cool75 on Mon, 16 May 2022 16:50:33 +0300