Basics of nginx

1, Basic concepts

  1. nginx: high performance HTTP and reverse proxy server, supporting hot deployment, high concurrency, reverse proxy, load balancing, dynamic and static separation
  2. Forward proxy: the client configures a proxy server to directly access the browser. This method is called forward proxy.
  3. Reverse proxy: the client accesses the proxy server. The proxy server directly accesses the target server to obtain data and then accesses the client. That is, the client accesses the target server indirectly, which is called reverse proxy.
  4. Load balancing: distribute requests for centralized access to a single server to different servers, that is, load balancing.
  5. Dynamic and static separation: the dynamic resources (such as jsp/servlet) and static resources (css, htms, js) are deployed separately, that is, dynamic and static separation.
  6. Simple architecture diagram:

2, nginx installation, common commands and configuration files

reference resources https://blog.csdn.net/weixin_40496191/article/details/121028500

3, Common commands

  1. View version:/ nginx -v
  2. Start:/ nginx
  3. Off:/ nginx -s stop
  4. Reload:/ nginx -s reload

4, nginx configuration file

  1. View the configuration file path: whereis nginx conf -->/usr/local/nginx/conf
  2. Composition of nginx configuration file
    (1) Global block: configuration parameters of nginx operation, such as concurrency worker_processes
    (2) events block: the network connection between nginx and the user, such as the number of supported connections worker_connections
    (3) http block: file import, MINE-TYPE definition, log customization, connection timeout, upper limit of single link request, etc
    (4) server block: an http block can have multiple server blocks, and each server is equivalent to the same virtual host.

5, nginx configuring reverse proxy

  1. Prepare a simple springboot project and deploy it. The default port is 8081. docker is recommended because there is no need to configure environment variables. Refer to: https://blog.csdn.net/weixin_40496191/article/details/122714245
  2. Simply configure the reverse proxy and forward the request to port 80 address (ip: 192.168.248.10)

    visit: http://192.168.248.10:80
    result: http://192.168.248.10:8081
  3. Realize different paths of ports and forward different addresses

    visit: http://192.168.248.10:801/test01/index/index , http://192.168.248.10:801/test02/index/index
    result: http://192.168.248.10:8081/index/index , http://192.168.248.10:8082/index/index
  4. Address matching analysis (port 80)
location  /js/ {
proxy_pass 192.168.248.10:8081/;
}
visit: http://192.168.248.10:80/js/index
 result: http://192.168.248.10:8081/index
location  /js/ {
proxy_pass 192.168.248.10:8081;
}
visit: http://192.168.248.10:80/js/index
 result: http://192.168.248.10:8081/js/index
location  /js/ {
proxy_pass 192.168.248.10:8081/js/;
}
visit: http://192.168.248.10:80/js/index
 result: http://192.168.248.10:8081/js/index
location  /js/ {
proxy_pass 192.168.248.10:8081/js;
}
visit: http://192.168.248.10:80/js/index
 result: http://192.168.248.10:8081/jsindex

6, nginx configuring load balancing

  1. to configure

    visit: http://192.168.248.10:80/index/index
    result: http://192.168.248.10:8081/index/index , http://192.168.248.10:8082/index/index polling
  2. Assign server policy
    (1) Simple polling (default): that is, it is evenly distributed in turns. If a service goes down, it will be automatically eliminated. Above
    (2) balance weight: that is, you can configure the allocation weight of each server by yourself. That is, the higher the allocation weight of the server with good efficiency and performance, the more times it will be accessed
    (3)ip_hash mode: each request is allocated according to IP, that is, each IP will only access one back-end service, which can solve the session problem.

    (4) Upstream fair mode: allocate according to the response time. The shorter the response time, the more will be allocated. (not adjusted, embarrassed)

    (5)url_hash method: allocate the request according to the hash result of the URL, so that each URL is directed to the same back-end server. (not adjusted, embarrassed)

6, nginx configuration dynamic and static separation

  1. Static file configuration: create a new folder mkdir /data/js, and then put it into jQuery Min.js, configuration file

    visit: http://192.168.248.10:803/js/jquery.min.js
    result: http://192.168.248.10:803/data/js/jquery.min.js
  2. Example of dynamic and static separation: normally, we will uniformly forward the files with static resource suffixes, as shown below

    visit: http://192.168.248.10:804/jquery.min.js
    result: http://192.168.248.10:804/data/js/jquery.min.js

7, nginx configuring high availability clusters

ps: it is not recommended to spend too much time learning in this cluster mode. Because the cluster has mature technology stacks, such as Nacos, Eureka, Zookeep, etc.

  1. Prepare two virtual machines: directly clone the first virtual machine (192.168.248.10192.168.248.11)
  2. Both virtual machines are installed with keepalived: yum install keepalived -y.
  3. Verify: rpm -q -a keepalived monitor is installed successfully
  4. Modify the configuration: VI / etc / kept / kept conf
    Primary server:
! Configuration File for keepalived

#Global definition
global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL10
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_script check_nginx
{
    script "/etc/keepalived/keepalived_check.sh"
    interval 3 #Monitor script interval
    weight -20

}

#Virtual ip configuration
vrrp_instance VI_1 {
    state MASTER #//The active and standby configuration is master --- backup
    interface ens33 #Network card eth0
    virtual_router_id 51 #Active and standby virtual_router_id must be the same
    priority 100 #The primary and standby have different priorities, and the primary is greater than the standby
    advert_int 1  #Multicast packet sending interval of master server
    authentication { #Authentication representation information between active and standby hosts
        auth_type PASS
        auth_pass 1111
    }
   track_script {
        check_nginx        #Monitoring script
   }

    virtual_ipaddress { #Set virtual ip address information
        192.168.248.100
    }
}
}

From server:

! Configuration File for keepalived

#Global definition
global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL11
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_script check_nginx
{
    script "/etc/keepalived/keepalived_check.sh"
    interval 3 #Monitor script interval
    weight -20

}

#Virtual ip configuration
vrrp_instance VI_1 {
    state BACKUP #//The active and standby configuration is master --- backup
    interface ens33 #Network card eth0
    virtual_router_id 51 #Active and standby virtual_router_id must be the same
    priority 90 #The primary and standby have different priorities, and the primary is greater than the standby
    advert_int 1  #Multicast packet sending interval of master server
    authentication { #Authentication representation information between active and standby hosts
        auth_type PASS
        auth_pass 1111
    }
   track_script {
        check_nginx        #Monitoring script
   }

    virtual_ipaddress { #Set virtual ip address information
        192.168.248.100
    }
}

}

keepalived_check.sh script

#! /bin/bash
# Count whether the nginx process exists
A=`ps -C nginx --no-header|wc -l`
# A value of 0 means nginx stops
if [ $A -eq 0 ];then
    # Try restarting nginx
    /usr/local/nginx/sbin/nginx
	sleep 1
    # If nginx restart fails, keepalived commits suicide and transfers vip
	
	A=`ps -C nginx --no-header|wc -l`
	# A value of 0 means nginx stops
	if [ $A -eq 0 ];then
        # Kill keepalived and transfer vip to another machine
        killall keepalived
    fi
fi
  1. Keep alive_ check. Put the SH script in the / etc / kept / directory and add permissions: CHMOD U + X kept_ check. sh
    ps: if it is a script edited by window, it needs to be processed, otherwise an error will be reported – > bad interpreter: there is no file or directory
    Processing: sed - I's / \ R $/ / 'keepalived_ check. Sh (both)
  2. Start nginx and kepplived: CD / usr / local / nginx / SBIN -- >/ nginx --> systemctl start keepalived. Service (both)
  3. View the primary server: ip a. (192.168.248.10)
  4. Access virtual ip: http://192.168.248.100:802/index/index
  5. Stop nginx on the primary server: CD / usr / local / nginx / SBIN -- >/ nginx stop
  6. View slave server: ip a (192.168.248.11)
  7. Access virtual ip: http://192.168.248.100:802/index/index success
  8. Restore the nginx of the primary server, and the virtual ip will jump back to the primary server.

Keywords: Operation & Maintenance Nginx server

Added by Roger Ramjet on Sun, 30 Jan 2022 08:12:14 +0200