Overview: Nginx is a high-performance HTTP and reverse proxy server. Supports a high number of concurrent requests.

Some of the concepts

1. Forward proxy

You need to configure the address and information about the proxy server on the browser, so that the browser can detect the existence of the proxy server.

2. Reverse proxy

You do not need to configure the address and information of the proxy server on the browser. Therefore, the browser cannot detect the existence of the proxy server.

3. Separation of static and static

In order to speed up the resolution of the website, static pages and dynamic pages can be handed over to different servers to resolve, speed up the resolution, reduce the pressure of a single server.

4. Load balancing

To reduce the pressure on the server, the requests sent by the client are evenly distributed to different servers.

Nginx installation

  1. Baidu download a good installation package
  2. Decompress tar -zvf ngin-xxxxx
  3. Go to the decompressed nginx file and run the./confugure command to check.
  4. Run the make && make install command to compile and install.

Basic commands of Ngixn

1. Run nginx to go to nginx/sbin and run the./nginx command.

2. Close nginx./nginx stop

3. Restart nginx./nginx -s stop

Configuration file parsing

The configuration file is divided into three parts altogether, the concrete may have a look this blog: www.cnblogs.com/hunttown/p/…

1. Global configuration

Global configuration for nginx

2. Configure events

3. The HTTP configuration

Reverse proxy and load balancing are configured here.

Configuring Load Balancing

In the HTTP block, add the configuration

Upstream myServer {//3. Based on this service, evenly distribute to server 10.10.10.1; Server 10.10.10.2; //4. IP address of the server for load balancing..... } server{ listen 80; Server_name 10.10.10.1; //1. The two lines indicate that nginx listens on this host port location / {proxy_pass http://myserver; }} When the browser sends a request, the execution process in Nginx is shown in figure 1, 2, 3, and 4, and load balancing is completed.Copy the code

Load balancing calculation policy

1. Polling Policy [Default Policy] Each request is allocated to a different back-end server one by one in chronological order. If the back-end server breaks down, the request is automatically rejected.

2. Weight Each server has its own weight. The default weight is 1.

Upstream myServer {server 10.10.10.10.weight =1; Server 10.10.10.2 weight = 10; . }Copy the code

3. Ip_hash Each request is allocated based on the HASH of the access IP address. In this way, each visitor accesses the same back-end server, which can solve the session problem.

upstream myserver{ ip_hash; Server 10.10.10.1; Server 10.10.10.2; . }Copy the code

4. Fair is allocated according to the response time of the back-end server, and the short response time will be allocated first!

Upstream myserver {server 10.10.10.1; Server 10.10.10.2; . fair; }Copy the code

Dynamic and static separation

To separate dynamic and static requests from clients, perform the following steps:

  1. Create a data directory under the root directory with WWW and image subdirectories inside



Place a static page in the WWW file, and place an image in the image file

  1. Configure the nginx configuration file
server { listen 80; Server_name 10.10.10.220; #charset koi8-r; #access_log logs/host.access.log main; location /www/ { root /data/; // The WWW folder index index. HTML index. HTM is in the data directory. } location /image/ { root /data/; // Autoindex on; // will be displayed as a list of files}Copy the code
  1. You must restart nginx after the configuration

/nginx -s reload./nginx -s reload

  1. Access on the page

page

The picture

On the other hand, if we start nginx and do not configure static file access path matching, it will not be accessible.

Nginx configures highly available clusters

1. Preparation

  1. You need two servers
  2. Nginx is installed on both servers
  3. Keepalived is installed on both servers

Tomcat

Download under Linux environment

Go to the official website to download: tomcat.apache.org/

2. Start

  1. After downloading the Tomcat installation package, decompress it.
  2. Go to the bin directory and run the./startup.sh command. Tomcat runs successfully.
  3. Run the command: ps – ef | grep tomcat, can check to see if the tomcat run.

Deployment projects

Put the project directly in the WebApp directory to access!

How Nginx works

When we perform ps – ef | grep nginx, will find nginx a total of two processes.

Here’s the whole process

Benefits of one Master and multiple workers?

  1. Hot deployment can be done using./nginx -s reload.
  2. Each worker is an independent process. If there is a problem with one worker, the other workers will run independently without causing service interruption.
  3. Each worker can maximize CPU efficiency, so it is most appropriate to set the number of workers to the number of server cpus.