Article Directory
Getting started with Nginx
1 Nginx overview
- Nginx (engine x) is a high-performance HTTP and reverse proxy web server , and also provides IMAP/POP3/SMTP services;
- It releases its 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/reverse proxy server and email (IMAP/POP3) proxy server, released under the BSD-like protocol.
- Its characteristic is that it occupies less memory and has strong concurrency capability . In fact, the concurrency capability of nginx is better than other web servers of the same type.
- Users of Nginx websites in mainland China include: Baidu, JD.com, Sina, Netease, Tencent, Taobao, etc.
- Nginx can be used as a web server for static pages, and it also supports dynamic languages of the CGI protocol, such as perl, php, etc. But java is not supported. Java programs can only be completed by cooperating with tomcat.
- Nginx is specially developed for performance optimization. Performance is its most important consideration. The implementation is very efficient and can withstand the test of high load. According to reports, it can support up to 50,000 concurrent connections.
2 Nginx-related technologies
2.1 Forward Proxy
Forward proxy, which means a server between the client and the original server (origin), in order to obtain content from the original server, the client sends a request to the proxy and specifies the target (origin server), and then the proxy forwards the request to the original server And return the obtained content to the client. The client can use the forward proxy.
- Simply put: the client accesses the background server through a proxy server
2.2 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 can directly access the reverse proxy server to obtain the resources of the target server. At the same time, 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. The reverse proxy server can usually be used as a Web acceleration, that is, using the reverse proxy as the front-end of the Web server to reduce the load on the network and server and improve access efficiency .
- PS: When a large number of clients access the server at the same time, a server server cannot withstand a large number of client requests, we will set up a cluster, set up a proxy server, and the proxy server cluster (the server does not perform specific functions) only sends to the cluster The request from the client responds to the service of a server in the cluster.
The reverse proxy hides the real server IP address and exposes the proxy server address
2.3 Load Balancing
Load balancing strategy provided by Nginx
①Polling strategy
②Right-based polling strategy
③IPHash
- ip_hash is based on the ip requested by the user, and then mapped into a hash value, and then assigned to a specific server;
- After using ip_hash for load balancing, it can be guaranteed that each session of the user will only be sent to the same specific Tomcat, and its session will not be crossed to other Tomcats;
- Each request is allocated according to the hash result of the access ip, so that each visitor accesses a backend server regularly, which can solve the session problem.
2.4 Dynamic and Static Separation
In order to speed up the parsing speed of the website, dynamic pages and static pages can be parsed by different servers to speed up the parsing speed. Reduce the pressure on the original single server.
3 Simple use of Nginx
3.1 Install Nginx
① Enter the official website to download
http://nginx.org/
② Decompress the compressed package
3.2 Nginx configuration file
Nginx.conf
# 内核数
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;
}
}
}
The configuration file consists of three parts
① Global block
From the configuration file to the content between the events block, some configuration instructions that affect the overall operation of the nginx server will be set, mainly including configuring the user (group) running the Nginx server, the number of worker processes allowed to be generated, the process PID storage path, and the log Storage path and type, import of configuration files, etc.
② events block
The instructions involved in the events block mainly affect the network connection between the Nginx server and the user. Commonly used settings include whether to enable serialization of network connections under multiple work processes, whether to allow multiple network connections to be received at the same time, and which event-driven model to choose for processing Connection requests, the maximum number of connections that each word process can support at the same time, etc.
③ http global block
The directives of http global block configuration include file import, MIME-TYPE definition, log customization, connection timeout, maximum number of single link requests, etc.
-
server block
This is closely related to the virtual host. From the perspective of the user, the virtual host is exactly the same as an independent hardware host. This technology was created to save the cost of Internet server hardware.
Each http block can include multiple server blocks, and each server block is equivalent to a virtual host .
Each server block is also divided into global server blocks, and can contain multiple location blocks at the same time.-
location block
The main function of this block is based on the request string (such as server_name/uri-string) received by the Nginx server, and the string other than the virtual host name (or IP alias) (such as the previous /uri-string) Match to process a specific request. Functions such as address orientation, data caching, and response control, as well as the configuration of many third-party modules are also performed here.
-
3.3 Be an agent
3.3.1 Bind domain name and IP
in
C:\Windows\System32\drivers\etc
thehosts
file
- Add the following configuration
127.0.0.1 huozhexiao.com #将域名和ip绑定
3.3.2 Start nginx in the nginx directory
E:\Nginx\nginx-1.23.4>nginx
3.3.3 Browser inputlocalhost
Successfully entered the nginx page
3.3.4 Close Nginx
Enter the nginx command
nginx -s stop
(quick stop) ornginx -s quit
(complete and orderly stop nginx)
3.3.5 Configure nginx.conf
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
#指定要代理的服务器
proxy_pass http://www.baidu.com;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
3.3.6 Accesswww.huozhexiao.com
Enter www.baidu.com
3.4 Do a load balancing
3.3.1 Create a SpringBoot project and write Controller
@RestController
@RequestMapping("/nginx")
public class NginxController {
@Value("${server.port}")
private Integer port;
@RequestMapping("/demo")
public String demo(){
return "nginx_demo--------->:"+port;
}
}
-
Application.yml respectively modify the port number
-
Project to run three ports in parallel
3.3.2 Configure nginx.conf
- configuration
upstream
, in http
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
#设置负载均衡服务器
upstream huozhexiao.com{
server 127.0.0.1:8081 weight=1;
server 127.0.0.1:8082 weight=2;
server 127.0.0.1:8083 weight=3;
}
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
#指定要代理的服务器
#proxy_pass http://www.baidu.com;
proxy_pass http://huozhexiao.com;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
Browser input:
http://huozhexiao.com/nginx/demo
Enter the three port numbers according to the corresponding weight polling strategy
3.5 Another module configures load balancing
3.5.1 Create a new Controller
@RestController
@RequestMapping("/admin/nginx")
public class NginxAdminController {
@Value("${server.port}")
private Integer port;
@RequestMapping("/demo")
public String demo(){
return "nginx_admin_demo--------->:"+port;
}
}
3.5.2 New configuration
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
#设置负载均衡服务器
upstream huozhexiao.com{
server 127.0.0.1:8081 weight=1;
server 127.0.0.1:8082 weight=2;
server 127.0.0.1:8083 weight=3;
}
#设置admin负载均衡服务器
upstream admin.com{
server 127.0.0.1:8084 weight=1;
server 127.0.0.1:8085 weight=2;
server 127.0.0.1:8086 weight=3;
}
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
#指定要代理的服务器
#proxy_pass http://www.baidu.com;
proxy_pass http://huozhexiao.com;
}
location /admin {
proxy_pass http://admin.com;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
browser input
http://huozhexiao.com/admin/nginx/demo
Enter the three port numbers according to the corresponding weight polling strategy
PS: related commands
open:nginx
closure:nginx -s stop
reboot: nginx -s reload
- Learning comes from Canada-China training