Nginx is what?
Nginx is a high-performance Web and reverse proxy server with very good features:
-
As a Web server: Nginx uses fewer resources, supports more concurrent connections, and is more efficient than Apache, which makes Nginx especially popular with Web hosting providers. Support up to 50,000 concurrent connections thanks to Nginx’s choice of ePoll and KQueue as development modules.
-
As a load balancer: Nginx can support Rails and PHP directly internally or as an HTTP proxy server externally. Written in C, Nginx is much more efficient in both system resource overhead and CPU usage than Perlbal.
-
As a mail proxy server: Nginx is also an excellent mail proxy server.
Nginx is extremely easy to start and can run almost 24/7, even for months without a reboot.
Proxy server
The forward proxy hides the user (the proxy of the client). The server does not know the client that actually initiates the request. The reverse proxy hides the server (server-side proxy), and the client does not know which server actually provides the service.
Forward agent
A forward proxy is a server that sits between a client and a target server. To get content from the target server, the client sends a request to the proxy and specifies the target server. The proxy then forwards the request to the target server and returns the obtained content to the client.
For example, we access Google through the Virtual Private Network(Virtual Private Network tunnel), as shown below:
The forward proxy hides the user, and the user’s request is received by the proxy server instead of the server, and the server does not know who the user is.
The reverse proxy
In Reverse Proxy mode, a Proxy server receives Internet connection requests, forwards the requests to a server on the network, and returns the results obtained from the server to the client requesting Internet connection. In this case, the proxy server acts as a reverse proxy server.
For example, baidu is definitely not a server, but we all query through www.baidu.com, and we do not know the actual request of the server to get the results, as shown below:
There is a limit to how many requests the server can handle. So a reverse proxy server is used to accept the request, and the load is balanced to distribute the request to multiple real servers. Not only can improve efficiency but also certain security.
Load balancing
Nginx provides two load balancing policies, built-in policies and extended policies. Built-in policies: Polling, weighted polling, and Ip hash. Extension policies: fair, URl_hash, etc.
Upstream: Write a set of proxy server addresses and configure the load balancing algorithm.
upstream mysvr {
server 127.0.0.1:8080;
server 127.0.0.1:8081;
}
server{...location+ ${~ * ^.proxy_pass http://mysvr; Request redirected to mysvr defined server list}}Copy the code
Hot standby
If you have two servers, only enable the second server to provide service when one server has an accident. AAAAAA suddenly A hangs up, BBBBBBBBBBBBBBBB…..
upstream mysvr {
server 127.0.0.1:8080;
server 127.0.0.1:8081 backup; # hot standby
}
Copy the code
polling
Nginx default is polling and its weight is 1 by default. The server processes requests in the following order: ABABABABAB….
upstream mysvr {
server 127.0.0.1:8080;
server 127.0.0.1:8081;
}
Copy the code
Weighted polling
Different numbers of requests are distributed to different servers based on the weight of the configuration. If this parameter is not set, the value is 1 by default. The following server requests are in the following order: abbabbabbabb….
upstream mysvr {
server 127.0.0.1:8080 weight=1;
server 127.0.0.1:8081 weight=2;
}
Copy the code
ip_hash
Hash the IP addresses requested by clients and then distribute the requests from the same CLIENT IP address to the same server for processing. In this way, sessions are not shared.
upstream mysvr {
server 127.0.0.1:8080;
server 127.0.0.1:8081;
ip_hash;
}
Copy the code
Combat the
Windows use nginx as a reverse proxy server and configure load balancing!
Download and Install
Click on nginx to download and unzip the file to use. No installation required.
run
Double-click nginx.exe to access 127.0.0.1.
Configuring a Reverse Proxy
1. Modify the nginx configuration file in the nginx.conf directory.
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;
proxy_pass https://www.baidu.com;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
roothtml; }}}Copy the code
2. When you modify the nginx configuration file nginx.conf, run the nginx -s reload command to make the changes take effect.
3. Visit 127.0.0.1 again and Baidu appears. As shown below:
Configuring Load Balancing
1. Modify the nginx configuration file in the nginx.conf directory.
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream mysvr {
server 127.0.0.1:8081 weight=3;
server 127.0.0.1:8080;
}
server {
listen 8080;
location / {
proxy_passhttps://www.baidu.com; }}server {
listen 8081;
location / {
proxy_passhttps://cn.bing.com; }}server {
listen 80;
server_name mysvr;
location / {
proxy_pass http://mysvr;
proxy_set_header Host mysvr;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header REMOTE-HOST $remote_addr;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
roothtml; }}}Copy the code
2. When you modify the nginx configuration file nginx.conf, run the nginx -s reload command to make the changes take effect.
3. Visit 127.0.0.1 again and baidu/Bing appears, which changes as the browser refreshes.