I. Basic Concepts:

  • The reverse proxy
  • Load balancing
  • Dynamic and static separation
  • High availability

Download and install

  1. For Windows, download and install the software from the official website: nginx.org/en/download…
  2. Install Nginx on centOS
  • To prepare the environment before nginx installation, install the following dependencies: PCRE, OpenSSL, and Zlib

Installation steps:

1. Install GCC GCC -c++

 $ yum install -y gcc gcc-c++

2. Installing pcre

`$ cd /usr/local/`

` $wget ` at http://jaist.dl.sourceforge.net/project/pcre/pcre/8.33/pcre-8.33.tar.gz

` $tar - ZXVF pcre - 8.36. Tar. Gz `

` ` $CD pcre - 8.36

'$./configure # check work'

'$make && make install

'$pcre -- config --version
Copy the code

3. Install the openSSL library

`$ cd /usr/local/`

` $wget ` at http://www.openssl.org/source/openssl-1.0.1j.tar.gz

` $tar - ZXVF openssl - j.t. ar 1.0.1. Gz `

` $CD openssl - j ` 1.0.1

`$ ./config`

`$ make && make install`
Copy the code

4. Install the Zlib inventory

`$ cd /usr/local/`

` $wget ` at http://zlib.net/zlib-1.2.11.tar.gz

` $tar - ZXVF zlib - 1.2.11. Tar. Gz `

`$ ./configure`

`$ make && make install`
Copy the code

Openssl and Zlib can be downloaded in one step:

yum-y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
Copy the code
  • Install Nginx:
1.Downloading the Installation package2.unzip3.Go to the decompression directory and run./configure4.make && make install

$ cd /usr/local/
$ wget http:/ / nginx.org/download/nginx-1.8.0.tar.gz
$ tar -zxvf nginx-1.8. 0.tar.gz
$ cd nginx-1.8. 0
$ ./configure
$ make && make install
Copy the code

Nginx Nginx

Go to the /nginx/sbin directory to check the version command:./nginx -v Start command:./nginx close command:./nginx -s stop Reload configuration file:./nginx -s reloadCopy the code

Four, open firewall port:

'View open ports:'
firewall-cmd --list-all

'Set open port number'
firewall-cmd --add-service=http -permanent
firewall-cmd --add-port=80/tcp -permanent

'Restart firewall'
firewall-cmd -reload
Copy the code

V. Configuration file:

Configuration files are mainly composed of global blocks, Events blocks, and HTTP blocks. The HTTP block is composed of HTTP global blocks and Server blocks. An HTTP block can have multiple Server blocks.

5.1 Reverse Proxy Configuration:

Case 1:

Case 2:

Visit http://192.168.3.70:9001/edu/ jump straight to the 127.0.0.1:8001

Visit http://192.168.3.70:9001/gov/ jump straight to the 127.0.0.1:8002

5.2 Load Balancing

In simple terms, a request is processed by different Tomcat servers, and the default is polling.

  • Several common polling strategies:
1. Default: Polling (weight=1) Each request is allocated to a different backend server in chronological order. If the backend server goes down, it will be deleted automatically. 2. Weight Specifies the polling probability. Weight is proportional to the access ratio and is used when the back-end server performance is uneven. Upstream myServer {server 192.168.3.70:8080 weight=1; Server 192.168.3.70:8081 weight = 2; } 3, ip_hash each request is allocated according to the hash result of the access IP, so that each visitor has a fixed access to the back-end server, which can solve the problem that session cannot cross servers. If the back-end server goes Down, you need to manually go down. upstream myserver{ ip_hash; Server 192.168.3.70:8080; Server 192.168.3.70:8081; } 4. Fair (third-party plug-in) allocates requests based on the response time of the back-end server, with priority given to those with short response times. upstream myserver{ fair; Server 192.168.3.70:8080; Server 192.168.3.70:8081; } 5. Url_hash (third-party) : Allocates requests based on the hash results of urls so that each URL is directed to the same backend server, which is more effective when the backend server is used for caching. (110: adds hash to upstream. Server cannot write weight or other parameters. Hash_method uses hash algorithm. upstream myserver{ hash $request_uri; hash_method crc32; Server 192.168.3.70:8080; Server 192.168.3.70:8081; }Copy the code
5.3. Static and dynamic separation (dynamic and static resources are placed on different servers separately)

Test visit: http://192.168.3.70/img/01.jpg

You can add Expires as a cache, and 3D is three days

5.4, High availability (Have standby Nginx servers ready in case of Nginx outages)

You need to install Keepalived, which is equivalent to a route, which checks the current nginx status through scripts and automatically switches to the standby nginx if the primary nginx goes down.

Note that external access is to a virtual IP that is bound to the active and standby Nginx servers

  • Preparations for configuring ha:
(1) Requires two servers:192.1683.70.and192.1683.71.2) Install nginx (3Install keepalived yum install keepalived -y # install keepalived yum install keepalived -y # install keepalived -q -a keepalived # Systemctl start keepalive. service # start keepalived Systemctl stop keepalive. service # disable keepalived commandCopy the code
  • MASTER and BACKUP configuration:
A little...Copy the code
  • Keepalived Configuration parameters

Nginx principle:

1. The master and the worker
Advantages of a master with multiple workers: It facilitates hot deployment because each worker is an independent process. If Nginx is installed on Windows, the IO multiplexing mechanism cannot be used to maximize the efficiency of Nginx. The number of workers is equal to the number of CPU cores.Copy the code

2. Connection number: work_connection:
Problem 1: The client sends a request, which occupies several connections of work.2A or4Nginx has one master and four workers. The maximum number of connections supported by each worker is1024, what is the maximum number of concurrent requests supported? Answer: (1Maximum number of concurrent accesses to static resources:4*1024/2(2) Maximum number of concurrent requests supported for HTTP as a reverse proxy:4*1024/4
Copy the code