Nginx is simple and easy to understand, quick to use

Advantages of Nginx

  1. Faster speed and higher concurrency
  2. Simple configuration and strong scalability
  3. high reliability
  4. hot deployment
  5. Low cost, BSD license
    license

The core composition of Nginx

nginx二进制可执行文件
nginx.conf配置文件
error.log错误的日志记录
access.log访问日志记录

Basic usage of Nginx

  1. Start nginx: sudo service nginx start
  2. Restart nginx: sudo service nginx reload
  3. Close nginx: nginx -s stop

Nginx proxy

Nginx forward proxy

	指代理服务器来接收Internet上的客户端请求,然后将请求转发给内部网络上的服务器,
	并将从服务器上得到的结果返回给客户端。
	此时代理服务器对外就表现为一个反向代理服务器

forward proxy

Nginx reverse proxy

	一个位于客户端和原始服务器之间的服务器,为了从原始服务器获得内容。
	客户端向代理发送一个请求并指定目标(原始服务器),
	然后代理向原始服务器转发并获得的内容返回给客户端。

reverse proxy

Main functions of Nginx

request forwarding

insert image description here
Nginx location path mapping

1. = match

location = / {
    
    
	#精准匹配
}

2. Universal match

location /xxx {
    
    
	#匹配所有以/xxx开头的路径
}

3. Match the beginning path

location ^~ /xxx/xx {
    
    
	#匹配所有以/xxx/xx开头的路径
}

4. Regex matching

location ~ /xxx {
    
    
	#匹配所有以/xxx开头的路径
}

5. Match ending path

location ~* \.(gif/jpg/png)$ {
    
    
	#匹配以.gif、.jpg或者.png结尾的路径
}

6. Universal match

location / {
    
    
	#通用匹配,匹配所有请求
}

load balancing

insert image description here

Nginx has three strategies for load balancing

1. Polling:
the client initiates a request and evenly distributes it to each server

upstream daili_server{
    
    
    server localhost:8080; #服务器IP或域名
    server localhost:8081; #服务器IP或域名
}
server {
    
    
    listen       80;
    listen  [::]:80;
    server_name  localhost;

	location / {
    
    
        proxy_pass http://daili_server/;	#负载均衡
    }
}

2. Weight:
The client's request will be allocated different amounts according to the weight value of the server

upstream daili_server{
    
    
    server localhost:8080 weight=10; #服务器IP或域名
    server localhost:8081 weight=2;  #服务器IP或域名
}
server {
    
    
    listen       80;
    listen  [::]:80;
    server_name  localhost;

	location / {
    
    
        proxy_pass http://daili_server/;	#负载均衡
    }
}

3. ip_hash:
Based on the different ip addresses of the clients who initiated the requests, he will always send the requests to the specified server.
That is to say, if the ip address of the client’s request remains unchanged, the server processing the request will always be the same

upstream daili_server{
    
    
	ip_hash;
    server localhost:8080; #服务器IP或域名
    server localhost:8081; #服务器IP或域名
}
server {
    
    
    listen       80;
    listen  [::]:80;
    server_name  localhost;

	location / {
    
    
        proxy_pass http://daili_server/;	#负载均衡
    }
}

static and dynamic separation

insert image description here
Dynamic resources: such as JSP is processed by Tomcat or other WEB servers.

location / {
    
    
  proxy_pass 路径;
}

Static resources: such as pictures, css, js, etc. are completed by the nginx server.

location / {
    
    
    root 静态资源路径;
    index 默认访问路径下的什么资源;
    autoindex on;#可以不写,写了则代表展示静态资源的全部内容,以列表的形式展开 
}

Nginx common commands

cd /usr/local/nginx/sbin/
./nginx	# 启动
./nginx -s stop	# 停止
./nginx -s quit	# 安全退出
./nginx -s reload	# 重新加载配置文件
ps aux|grep nginx	# 查看nginx进程

Nginx core configuration file structure

case:

worker_processes 1;

events {
    
    
 	worker_connections 1024;
}
http {
    
    
 	include    mime.types;
 	default_type application/octet-stream;
 	sendfile    on;
 	keepalive_timeout 65;
 	server {
    
    
   		listen    80;
   		server_name localhost;
   		location / {
    
    
     		root  html;
     		index index.html index.htm;
   		}
   		error_page  500 502 503 504 /50x.html;
   		location = /50x.html {
    
    
     		root  html;
   		}
 	}
 }
...              #全局配置: 指令名 指令值; 主要设置Nginx服务器整体运行的配置指令

events {
    
             # events块,主要设置,Nginx服务器与用户的网络连接,这一部分对Nginx服务器的性能影响较大
   ...   #指令名 指令值;
}

http {
    
         # 是Nginx服务器配置中的重要部分,代理、缓存、日志记录、第三方模块配置...  
	
    upstream edustudy {
    
    	# 负载均衡配置,默认是轮询
        server 127.0.0.1:8080 weight=1;	# weight是权重
        server 127.0.0.1:8081 weight=1;
    }
    server {
    
            #server代理  是Nginx配置和虚拟主机相关的内容
        listen       9001;
        server_name  localhost;	# 监听到之后转发到localhost
        
        location / {
    
      # 9001端口下的所有请求都会被匹配到这里  基于Nginx服务器接收请求字符串与location后面的值进行匹配,对特定请求进行处理
			proxy_pass http://edustudy;	# 转发至负载均衡里
        }
        
        location /admin {
    
    
            
        }
       
        # 当地址中包含"eduService"时,请求会转发至 http://localhost:8085
        location ~ /eduService/ {
    
     
            proxy_pass http://localhost:8085;
        }

    }
    
    server {
    
    
      	listen       9002;
        server_name  localhost;
    }
}

Summary:
There are three default blocks in the nginx.conf configuration file: the global block, the events block, and the http block. Multiple server blocks can be configured in the http block, and multiple location blocks can be configured in each server block.

Rewrite function configuration

Rewrite is an important basic function provided by the Nginx server, and it is almost a must-have function in web server products. The main function is to implement URL rewriting. www.jd.com Note: The implementation of the Rewrite function of the Nginx server depends on the support of PCRE, so the PCRE library needs to be installed before compiling and installing the Nginx server. Nginx uses the ngx_http_rewrite_module module to parse and process the related configuration of the Rewrite function
Rewrite related commands

  1. set command
  2. if instruction
  3. break command
  4. return command
  5. rewrite command
  6. rewrite_log command

Application Scenarios of Rewrite

  1. domain redirection
  2. Domain Mirroring
  3. independent domain name
  4. The directory automatically adds "/"
  5. merge directory
  6. Realization of anti-leech

Rewrite related instructions

set command

This command is used to set a new variable
insert image description here

variable: The name of the variable. The variable name should use "$" as the first character of the variable, and it should not have
the same name as the global variable preset by the Nginx server.
value: The value of the variable, which can be a string, other variables, or a combination of variables.

if instruction

This command is used to support condition judgment, and select different Nginx configurations according to the condition judgment result.
insert image description here

break command

This directive is used to interrupt other Nginx configurations currently in the same scope. In the Nginx configuration in the same scope as this directive, the directive configuration before it takes effect, and the directive configuration behind it is invalid. And break has another function, which is to terminate the current matching and redirect the current URI to this location for access processing.

insert image description here

return command

This instruction is used to complete the processing of the request and return it directly to the client. All Nginx configuration after return is invalid
insert image description here

rewrite command

This directive alters the URI through the use of regular expressions. One or more instructions can exist at the same time, and URLs are matched and processed sequentially.

insert image description here

rewrite_log command

This directive configures whether to enable the output function of the URL rewriting log.
insert image description here

The case of Rewrite

domain redirection

Problem Analysis
Let’s look at an effect first. If we want to visit the Jingdong website, we all know that we can enter www.jd.com, but we can also enter www.360buy.com to access the Jingdong website. This is actually because the domain name of JD.com was www.360buy.com when it first started. Later, due to various reasons, it changed its domain name to www.jd.com. Although the domain name is variable, it only remembers www.360buy before. For .com users, how do we migrate these users to our new domain name? For this problem, we can use the domain name redirection of Rewrite in Nginx to solve it.
Environmental preparation

  • Prepare three domain names
vim /etc/hosts
127.0.0.1  www.itcast.cn
127.0.0.1  www.itheima.cn
127.0.0.1  www.itheima.com
  • Access www.itcast.cn through Nginx
server {
    
    
	listen 80;
	server_name www.itcast.cn;
	location /{
    
    
		default_type text/html;
		return 200 '<h1>welcome to itcast</h1>';
	}
}

Rewrite requests to redirect www.ithema.com and www.itheima.cn to www.itcast.com

server {
    
    
	listen 80;
	server_name www.itheima.com www.itheima.cn;
	rewrite ^/ http://www.itcast.cn;
}

Guess you like

Origin blog.csdn.net/weixin_42506139/article/details/127282408