This is the sixth day of my participation in the First Challenge 2022. For details: First Challenge 2022.

I. Baidu Baike

Nginx is a high-performance HTTP and reverse proxy Web server that also provides IMAP/POP3/SMTP services. Nginx was developed by Igor Sessoyev for Russia’s second most visited rambler.ru site, with the first public version 0.1.0 released on October 4, 2004.

It distributes source code under a BSD-like license and is known for its stability, rich feature set, simple configuration files, and low system resource consumption.

Nginx is a lightweight Web server and E-mail (IMAP/POP3) proxy server distributed under the BSD-like protocol. In fact, Nginx’s concurrency is better in the same type of web server. In Mainland China, Nginx website users are: Ali, Baidu, JINGdong, Sina, NetEase, Tencent and so on.

Nginx as a Web server

Nginx can serve as a Web server for static pages and also provides CGI protocol for dynamic languages such as Perl, PHP, etc. But Java is not supported. Java can only be done with Tomcat. Nginx was developed specifically for performance optimization. Performance is the most important consideration, and implementation is very efficient. Nginx can withstand high loads and reports indicate that it can support up to 50,000 concurrent connections.

Nginx handles requests logically

4. Nginx’s advantages

  1. High concurrent connection;
  2. Low memory consumption;
  3. Low cost;
  4. The configuration file is very simple;
  5. Support for Rewrite (grouping HTTP requests into different back-end server groups based on domain names and urls). ;
  6. Built-in health check function;
  7. Save bandwidth (support for GZIP compression, you can add the browser’s local cache Header.) ;
  8. High stability (used for reverse proxy, the probability of downtime is minimal.) ;
  9. Supports hot deployment.

5. Nginx application scenarios

Forward proxy Introduction

If the Internet outside the LAN is considered as a huge resource library, clients in the LAN need to access the Internet through a proxy server. This proxy service is called forward proxy.

1. Reverse proxy

The client’s agent is no perception, because the client does not require any configuration can access, we only need to send the request to the reverse proxy server, chosen by the reverse proxy server to the target server to get data, and then returned to the client, at this time the reverse proxy server and the target server is a server, exposure is a proxy server address, The IP address of the real server is hidden.

2. Load balancing

A single server is not the solution, we increase the number of servers, and then distribute requests to multiple servers, instead of centralized requests to a single server, and that’s load balancing.

3. Separation of activity and movement

In order to speed up the site’s resolution speed, dynamic pages and static pages can be resolved by different servers, speed up the resolution, reduce the original single server pressure.

Nginx common commands

1, start,

Nginx installation directory address -c nginx configuration file address

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

Calm stop

(1) View the process ID

Ps – ef | grep nginx (2) kill the process

kill -QUIT 2072

3. Stop quickly

(1) View the process ID

Ps – ef | grep nginx (2) kill the process

kill -TERM 2132

4, forced stop

pkill -9 nginx

5, restart

(1) Verify that the nginx configuration file is correct

Method 1: Go to the nginx installation directory sbin and run the./nginx -t command

Nginx. conf syntax is OK

nginx.conf test is successful

Method 2: Add -t before the -c command

/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf

6. Restart the Nginx service

Method 1: Go to the nginx executable directory sbin and run the./nginx -s reload command

Method 2: Search for the current Nginx process id and run the kill -hup process ID command to restart the nginx service

Nginx configuration file

1, Find config file location:

cd /usr/local/nginx/conf/nginx.conf

2. Contents in the configuration file

  1. Global block: configuration instructions that configure the server to run as a whole, such as worker_processes 1; Handle the configuration of concurrency;
  2. Events block: affects the network connection between the Nginx server and users such as worker_connections 1024; The maximum number of connections supported is 1024.
  3. The HTTP block also contains two parts: the HTTP global block Server block;

Nginx configuration instance – Reverse proxy instance

1, achieve the effect

Open a browser and enter www.123.com in the address box to go to the Tomcat home page of the Liunx system

2. Preparation

  1. Install Tomcat on liunx, use the default port 8080 * to save the tomcat installation file to liunx, decompress * to go to the bin directory of tomcat, and start the Tomcat server./startup.sh
  2. Firewall – CMD –add-port=8080/ TCP –permanent firewall- CMD – reload Check the open port number firewall- CMD –list-all
  3. Access the Tomcat server from a browser in Windows

3. Analysis of the visit process

4. Configure the mapping between domain names and IP addresses in the Host file of the Windows operating system

(1) Add content to host file

192.168.17.129 www.123.com

Step 2 configure request forwarding on Nginx (reverse proxy configuration)

server{
	listen:80;
	server_name 192.168100.1.;
	location / {
		root html;
		proxy pass http:/ / 127.0.0.1:8080;index index.html index.htm; }}Copy the code

5. Final test

The principle of Nginx

1. Mater and Worker

2. How does worker work

3. One master and multiple Wokers have advantages

  1. You can use Nginx -s reload hot deployment and use Nginx for hot deployment.
  2. Each Woker is an independent process. If one of the wokers has a problem, the other wokers are independent and continue to scramble to implement the request process without causing service interruption

4. Set the appropriate number of wokers

It is best if the number of workers is equal to the number of cpus on the server.

5. Worker_connection

First: how many connections did woker use to send the request? Answer: 2 or 4;

Nginx has one master and four wokers. Each woker supports up to 1024 connections. What is the maximum number of concurrent connections supported?

The normal maximum number of concurrent static accesses is: worker_connections * worker_processes /2;

If HTTP is used as a reverse proxy,

The maximum number of concurrent requests should be worker_connections * worker_processes/4;