The installation
Before installing Nginx, you need to do a few things:
- Install GCC compiler:
yum install -y gcc
- G++ compiler:
yum install -y gcc-c++
- Install PCRE library:
yum install -y pcre pcre-devel
- Install zlib library:
yum install -y zlib zlib-devel
- Install OpenSSL development library:
yum install -y openssl openssl-devel
Then download the Nginx zip package path:Nginx websiteI chose the stable version here.Decompress: tar -zxvf nginx-1.20.1.tar.gzThe unzipped directory structure looks like this:
Run the./configure command to check the operating system kernel and whether the required environment is installed, resolve parameters, and generate intermediate directories and makefiles. The configure command generates an objs intermediate directory.
Make this command compiles the Nginx project from the Makefile generated by the configure command, and generates the object file, the final binary file.
/usr/local/ Nginx /usr/local/ Nginx /usr/local/ Nginx /usr/local/ Nginx You can specify the location of the Nginx installation directory by using –prefix=PATH.
How to start Nginx: Go to the deployment directory and executesbin/nginx
To start Nginx:Stop Nginx command:sbin/nginx -s stop
Restart the Nginx command:sbin/nginx -s reload
Basic Principles
During the installation step, you can see that when Nginx is started, two processes are displayed. One is the master, the other is the worker. The default worker is one, and the number of workers can be changed through the configuration file.
Nginx uses a master process to manage multiple worker processes. In general, the number of worker processes is equal to the number of cpus on the server. In fact, it is the worker process that provides the service in Nginx, and the master is only responsible for monitoring and managing the worker process.
Static resource server
Nginx enables independent access to static files and applications. That is, we can access our static resources, such as images, PDF files or our front-end code, without using the application.
I have encountered some old projects that require static files to be requested through the Java interface. Not only does this result in a decrease in security (static resources become inaccessible when the application dies or is redistributed), it also greatly affects our access efficiency.
So we can choose to use Nginx as a static resource server according to the actual business scenario, so how to implement the static resource server, mainly through the HTTP block configuration of nginx.conf file:
worker_processes 1; # worker_connections 1024; HTTP {include mime.types; Default_type application/octet-stream; sendfile on; # specify whether nginx calls sendfile zero copy) to output files #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; Keepalive timeout #gzip on; server { listen 80; Server_name localhost; Localhost :80/images/* root /usr/local/static; # will be eligible to request the request to the real resource directory, / usr/local/static/images / * autoindex on; } # localhost:80/ PDF /* root /usr/local/static; autoindex on; } location / { root html; index index.html index.htm; }}Copy the code
Effect:
Other matching expressions for location:location[=|~|~*|^~|@]/uri/{... }
- Location = / # The configuration under this location will only be used if the user requests /
- ~ indicates that URI matching is case sensitive
- ~* indicates that URI matching is case insensitive
- ^~ indicates that only the first half of the URI parameter matches, location ^~ images {}, requests starting with images will be matched
- @ indicates that it is only used for redirection between requests within the Nginx service. Location with @ does not handle user requests directly
Locations are sequential, and while it is possible for a request to match multiple locations, the request will actually be processed by the first location.
Reverse proxy server
In addition to being a static resource server, Nginx can also be used as a reverse proxy server. Before explaining how to configure a reverse proxy server, let’s briefly explain what a forward proxy is and what a reverse proxy is.
A forward proxy is one where we need to access a web site, but for some reason, our request cannot be directed to the target server. At this point, we can use another server in a foreign country (as a proxy, proxy server), by this server to access the target server, and our client only need to request this proxy server. This process is called forward proxy.Forward proxy hides real client informationThe server does not know which client is actually sending the request because the proxy server directly interacts with the server for requests and data. [Source: Resources]Reverse proxy, on the other hand, hides the real server. For example, our client directly visits Baidu.com. In fact, Baidu.com corresponds to a proxy server. Behind it, there are a large number of servers that really provide services to process and respond to requests. This process is called reverse proxy,The reverse proxy hides the real server.
[Source: Resources]Therefore, Nginx’s role in the reverse proxy is to act as a reverse proxy server, which is used to forward client requests to the actual service server.
When a client sends a request, Nginx does not immediately forward it to the server that provides the service. Instead, Nginx first receives the user’s request in its entirety and then sends a connection to the server to forward the cached client request. The advantage of Nginx is that it reduces the load on the server side and puts the load on the Nginx server as much as possible. The disadvantage is that it lengthens the processing time of the request.
Start configuration: preparation: because I need to test Nginx forwarding to different servers, I packed three jars and started them with ports 8081,8082,8083.Configure nginx. Conf
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; # upstream nginx_demo_colony{ server yourip:8081; server yourip:8082; server yourip:8083; } server { listen 80; server_name yourip; #charset koi8-r; #access_log logs/host.access.log main; location /nginxLoadBalanceDemo/{ proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://nginx_demo_colony; [upstream] {root /usr/local/static; autoindex on; } location /pdf/ { root /usr/local/static; autoindex on; } location / { root html; index index.html index.htm; }}Copy the code
The code to invoke the interface:
@RestController
@RequestMapping(value = "/nginxLoadBalanceDemo")
public class HelloWorld {
@Value("${server.port}")
private String serverPort;
/** * Get the port number of the current application */
@GetMapping (value = "/getServerPort")
public String getServerPort(a){
return this.serverPort;
}
Copy the code
Effect:The default load balancing algorithm is polling. For the time being, who knows for sure in the future, maybe I will come back to improve, see you later.
The resources
- In-depth Understanding of Nginx: Module development and Architecture Parsing (2nd edition)
- Nginx Field (a total of 11 chapters) summary & summary
- Forward proxy and reverse proxy