Nginx tutorial

1. Introduction to Nginx

Nginx is a lightweight web server/reverse proxy server and email (IMAP/POP3) proxy server. It is characterized by less memory and strong concurrency capability. In fact, the concurrency capability of nginx is better than other web servers of the same type. The websites that use nginx in mainland China include: Baidu, Jingdong, Sina, Netease, Tencent, Taobao, etc.

2. Download and install

Installation process:
1. Install dependent packages

yum -y install gcc pcre-devel zlib-devel openssl openssl-devel

2. Download the Nginx installation package

wget https://nginx.org/download/nginx-1.16.1.tar.gz

3. Unzip

tar -zxvf nginx-1.16.1.tar.gz

4. Enter the nginx directory

cd nginx-1.16.1

5. Specify the installation directory where nginx will be installed later

./configure --prefix=/usr/local/nginx

6. Compile and install

make && make install


Directory Structure

  • conf/nginx.conf------------------------>nginx configuration file
  • html--------------------------------------->Store static directory
  • logs---------------------------------------> store log files
  • sbin/nginx-------------------------------> binary file, used to start and stop nginx service
    insert image description here

3. Nginx command

1. View version

./nginx -v

2. Check the correctness of the configuration file

./nginx -t

3. Start and stop

start up:

./nginx

stop:

./nginx -s stop

View Nginx process

ps -ef | grep nginx

4. Reload the configuration file

./nginx -s reload

You can configure the environment variables of nginx, so that we can execute commands.
Configure under PATH in /etc/profile, and separate multiple environment variables with colons
insert image description here

4. Configuration file structure

The overall Nginx configuration file (nginx.conf) is divided into three parts:

  • Global block -----------------> Global configuration related to Nginx operation
  • events block ---------------> and network connection related configuration
  • http block -------------------> proxy, cache, log, virtual host configuration
    • http global block
    • Server block
      • Server global block
      • location block

Note: Multiple Server blocks can be configured in the http block, and multiple location blocks can be configured in each Server block

insert image description here

5. Specific application of Nginx

1. Deploy static resources

Nginx can be used as a static web server to deploy static resources. Static resources refer to some files that actually exist on the server side and can be displayed directly, such as common html pages, css files, js files, pictures, videos and other resources. Compared with Tomcat, Nginx is more efficient in handling static resources, so in a production environment, static resources are generally deployed to Nginx. Deploying static resources to Nginx is very simple, just copy the files to the html directory under the Nginx installation directory.

server {
	listen 80; #监听端口
	server_name localhost; #服务器名称
	location / {  #匹配客户端请求url
		root html; #指定静态资源根目录
		index index.html; #指默认首页
	}
}

2. Reverse proxy

Forward proxy: The user knows the address of the proxy server
insert image description here
Reverse proxy:
The reverse proxy server is located between the user and the target server, but for the user, the reverse proxy server is equivalent to the target server, that is, the user directly accesses the reverse proxy server The resources of the target server can be obtained, and the reverse proxy server is responsible for forwarding the request to the target server. The user does not need to know the address of the target server, and does not need to make any settings on the client side.
insert image description here


Configure reverse proxy

server {
	listen 82;
	server_name localhost; 
	location / {
		proxy_.pass http://192.168.138.101:8080; #反向代理配置,将请求转发到指定服务
	}
}

3. Load balancing

The early website traffic and business functions were relatively simple, and a single server could meet the basic needs. However, with the development of the Internet, the business traffic is increasing and the business logic is becoming more and more complex. The performance of a single server and a single point The problem of failure is highlighted, so multiple servers are required to form an application cluster to scale performance horizontally and avoid single point of failure.

  • Application cluster: Deploy the same application to multiple machines to form an application cluster, receive requests distributed by the load balancer, perform business processing and return response data
  • Load balancer: Distribute user requests to a server in the application cluster for processing according to the corresponding load balancing algorithm.
    insert image description here
    The configuration is as follows:
upstream targetserver{ #upstream指令可以定义一-组服务 器
	server 192.168.138.101:8080 weight=10; #可以设置权重,默认都是1,数值越大,分配的越多
	server 192.168.138.101:8081 weight=5;
}
server {
	listen 8080;
	server_name localhost;
	location / {
		proxy_.pass http://targetserver;
	}
}

Load balancing strategy:
insert image description here

Guess you like

Origin blog.csdn.net/lx00000025/article/details/130711921