What are the specific applications of nginx?
1. Deploy static resources
Nginx can be used as a static web server to deploy static resources. Static resources refer to some files that actually exist on the server side and can be displayed directly, such as common html pages, css files, js files, pictures, videos and other resources.
- Why use nginx to deploy static resources?
Because nginx is more efficient in handling static resources than tomcat, static resources are generally deployed in nginx in a production environment. Moreover, it is very simple to deploy static resources to nginx, just copy the files to the directory under the nginx installation directoryhtml
.
Simple test:
write a simple html page hello.html
with the following content:
<html>
<head>
<title>Hello Nginx</title>
<head>
<body>
<h2>hello Nginx ...</h2>
<body>
</html>
Put the hello.html file into /usr/local/nginx/html
the directory, and then visit the page in the browser, the effect is as follows:
its internal implementation is the Server block in nginx.conf:
server {
# Server全局块
listen 80; #监听端口
server_name localhost; #服务器名称
# location块
location / { #匹配客户请求url
root html; #指定静态资源根目录
index index.html index.htm; #指定默认首页
}
}
Note: vim tips input :set nu
display line number.
2. Reverse proxy
- First of all, what is a forward proxy?
The forward proxy is a server located between the client and the original server (origin server). 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 customer service end.
A typical use of a forward proxy is to provide a way for LAN clients inside the firewall to access the Internet.
The forward proxy generally sets up a proxy server on the customer service side , forwards the request through the proxy server, and finally accesses the target server.
- Then talk about what is a reverse proxy?
The reverse proxy is that 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, the reverse proxy The server is responsible for forwarding the request to the target server.
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.
- Configure reverse proxy in nginx.conf:
server { listen 82; #监听端口 server_name localhost; location / { proxy_pass http://192.168.93.101:8080; #反向代理配置,将请求转发到指定服务器 } }
3. Load balancing
The early website traffic and business functions were relatively simple, and a single server could meet the basic needs. However, with the development of the Internet, the business traffic is increasing and the business logic is becoming more and more complex. The performance of a single server and a single point The failure problem is highlighted, so multiple servers are required to form an application cluster to scale performance horizontally and avoid single point of failure.
- Application cluster: Deploy the same application to multiple servers to form an application cluster, receive requests distributed by the load balancer, perform business processing and return response data.
- Load balancer: Distribute user requests to a server in the application cluster for processing according to the corresponding load balancing algorithm.
- Configure load balancing in nginx.conf:
upstream targetserver{ #upstream指令可以定义一组服务器 server 192.168.93.101:8080; server 192.168.93.102:8080; } server { listen 8080; #监听端口 server_name localhost; location / { proxy_pass http://targetserver; } }
- Load balancing strategy:
- Load balancing strategy: