This is the third day of my participation in the August More text Challenge. For details, see:August is more challenging
os: windows10
Nginx: 1.19.2
Official download address: nginx.org/en/download…
Deploy in Windows
- Download the zip file from the official website
- Decompress to a specified location, such as
D: \ \ Program Files \ nginx - 1.19.2
- Open CMD and CD to the same directory as the previous step
- Enter the start command for nginx
start nginx
Tip: D:\Program Files\nginx-1.19.2, open the file manager, enter CMD in the address box of the file manager, The open CMD window will automatically locate the path of the current folder
Or, when step 2 is complete, we can double-click nginx.exe to run it. At this point, we can check whether nginx is running in the task manager
Schematic diagram of common file locations
| - nginx - 1.19.2 | - conf | - nginx. Conf nginx configuration items are here | - logs | - access. Log nginx access log the default location | - error. The log nginx error log the default locationCopy the code
General operation process
- In the CMD is used
start nginx
Start the service - With text editing tools such as
hbx, sublimetext
Open the fileconf/nginx.conf
- Modify configurations in the configuration file
- Used in CMD
nginx -s reload
Refreshing the Configuration File - check
logs/error.log
, confirm that no error is reported after service restart (if yes, repeat steps 3 and 4) - Web browsing Check whether configuration items meet expectations
logs/access.log
Repeat steps 3,4, and 5 if needed
Nginx advantages
The reverse proxy
There are two kinds of agents
Forward proxy:
- Use a third-party proxy to request resources from the target server
- The third party is only responsible for requesting the url specified by the user, obtaining the resource and returning it
Reverse proxy:
- Users directly access the proxy server, the server according to the user’s request, to select access to the appropriate real server, and return data
- The core is to expose the proxy server and hide the real server address
Example code:
server { listen 80; # specify url server_name 192.168.0.1 in server.server_name; location / { root html; Proxy_pass specifies the actual url that nginx needs to forward access proxy_pass http://127.0.0.1:8080; index index.html index.htm;Copy the code
Server {# configure the external access path http://192.168.0.1:9001 listen 9001; Server_name 192.168.0.1. {proxy_pass http://127.0.0.1:8080 {proxy_pass http://127.0.0.1:8080;} } location ~ /vod/ {proxy_pass http://127.0.0.1:8081; }Copy the code
Load balancing
Distribute requests to multiple servers to reduce the load on a single server
Strategy:
-
Polling (default)
-
The weight
-
IP hash
- Each request is assigned according to the hash result of the access IP, so that each visitor can access a fixed back-end server, which can solve the session sharing problem
-
Fair (third party)
- Requests are allocated according to the response time of the back-end server, and those with short response time are allocated first
Example code:
"Upstream myserver" {default: server 192.168.0.1:8080; Server 192.168.0.1:8081; } # Add proxy_pass server {listen 80; Server_name 192.168.0.1:8080; location / { proxy_pass http://myserver; root html; index index.html index.htm; }}Copy the code
Upstream myserver {# 192.168.0.1:8080 weight 10; Server 192.168.0.1:8081 weight 5; }Copy the code
Upstream myserver {# fair server 192.168.0.1:8080; Server 192.168.0.1:8081; fair; }Copy the code
Upstream myserver {# ip_hash; Server 192.168.0.1:8080; Server 192.168.0.1:8081; }Copy the code
Dynamic and static separation
Assign dynamic resources and static group resources to different servers for resolution to improve the resolution speed
Static resources:
Static HTML, CSS, JS, IMG and other resources
Dynamic resources:
A resource that is dynamic, requires background computing, or is pulled from a database, commonly, such as an Ajax request
Example code:
server { listen 8080; Server_name 192.168.0.1. location /html/ { root /data/; index index.html } location /images/ { root /data/; Autoindex on; autoindex on; autoindex on; The actual path}} # files | - data | | - HTML - a.h HTML. | | - images - 1 JPG # # # visit the URL http://192.168.0.1:8080/html/a.html http://192.168.0.1:8080/images/1.jpgCopy the code
High availability
When Nginx is used as reverse proxy or load balancing, Nginx is used as the entry point. If Nginx breaks down, all services cannot be provided normally and the impact is very serious. So we need to make sure that nginx is highly available, that is, to configure the backup machine, the first one goes down, and the second one goes down.
To avoid the serious impact of load balancing server downtime, you need to create a backup machine. Both the primary server and the backup machine run High Availability monitors that monitor each other’s health by sending messages such as “I am alive.” When the backup machine cannot receive such a message within a certain period of time, it will take over the service IP address of the primary server and continue to provide load balancing services. When the backup manager receives a message like “I am alive” from the primary manager, it releases the service IP address and the primary server starts to provide load balancing services again.
If necessary, use the keyword Nginx Keepalived high availability load balancing to search for specific build tutorials
Common commands
/nginx -s stop # start nginx # start nginx #./nginx -s stop # start nginx #./nginx -s stop # Used to reload./nginx -s reload after manually adjusting the configuration fileCopy the code
The configuration file
Global block
From the configuration file to the event block, the main Settings are the configuration directives that affect the overall running of Nginx
The larger the value of worker_Process 1, the more concurrent processing can be supportedCopy the code
The event of
Configure the network connection between nginx and the user
Worker_connections 1024 Maximum number of supported connectionsCopy the code
HTTP block
HTTP global block
The server block
The location instructions
location [ = | ~ | ~* | ^~ ] uri {
}
Copy the code
= : Before the URI that does not contain regular expressions, the request string must match the URI strictly. If the match is successful, the search stops and the request is processed immediately
~ : indicates that the URI contains regular expressions and is case sensitive
~* : indicates that the URI contains regular expressions and is case insensitive
^~ : before using a URI that does not contain a regular expression, the Nginx server is required to immediately use the location that matches the request string with the uri, instead of using the re in the location block to match the request string
Note: If a regular expression is included in the URI, it must have a ~ or ~* identifier