Article Directory
1. Nginx reverse proxy configuration
nginx
web
An important function as a server is 反向代理
.
Of course, you can also use nginx forward proxy configuration , this article merely describes how to configure nginx
the 反向代理
.
nginx反向代理
The instructions do not need to add additional modules, the default comes with proxy_pass
instructions, only need to modify the configuration file to achieve 反向代理
.
2. What is a reverse proxy server
The reverse proxy function is one nginx
of the three main functions (static web
server, reverse proxy, load balancing). nginx
Generally, it is used as a 静态web服务器
sum 反向代理服务器
, as an web服务器
access 静态文件图片、css、js、html
file, and as a reverse proxy server to send requests to the back-end business processing service. If there are multiple back-end processing nodes, the 负载均衡
function will be configured .
A reverse proxy server is a proxy server used to manage the connection from the external network to the internal network or any specific request. It protects, routes, and manages traffic from external networks to internal networks, web servers, or private networks.
2.1 Extranet client
In www.csdn.cn
the scenario where we usually open the browser and enter the URL to visit , our notebook can be understood as an extranet client.
2.2 nginx reverse proxy service
After the browser enter the URL and press Enter, will initiate a http
request to the nginx
(reverse proxy server), if the request is accessing static files, nginx
as web
server directly return the requested content, if it is a background service logical access, then nginx
the request is forwarded To the back-end service processing.
2.3 Intranet web service
The backend service may be a variety of types, LNMP
at ambient php-fpm
process, Java
the environment tomcat、jetty
and other containers, of program logic processing http
request, generates html
a page or json
string back to the client.
For small applications, the back-end service can be nginx
deployed on the same machine.
3. Benefits of reverse proxy server
nginx
The important role of the reverse proxy is to cooperate to upstream
achieve load balancing.
At the same time, security is increased. Clients cannot directly access back-end services, adding an intermediate barrier.
Improve performance, pass the request to the back end in an asynchronous and non-blocking way, and improve the concurrent processing capability.
Can also use cache, compression response to improve response speed.
4. How to configure reverse proxy in nginx
nginx
The reverse proxy does not need to compile additional modules, it comes with proxy_pass
and fastcgi_pass
instructions by default, and the location
reverse proxy function can be realized by adding instructions in the configuration block.
For example, a wordpress
program for a website wordpress
is php
written in a language, and then it needs to go through the php
operating environment, which can be selected as apache
an php
extension or php-fpm
environment. The mainstream choice is php-fpm
to php-fpm
set it to Unix socket
mode or ip:
port mode.
4.1 Unix socket backend service configuration
server {
listen 80;
server_name localhost;
location /app {
fastcgi_pass unix:/tmp/php-cgi.sock;
}
}
4.2 ip port backend service configuration
server {
listen 80;
server_name localhost;
location /app {
proxy_pass http://127.0.0.1:8080;
}
}
5. The difference between proxy_pass and fastcgi_pass
For both cases described above proxy_pass
and fastcgi_pass
may be substituted for each other use, but there are differences between the two, we can see from the name, fastcgi_pass
it is used to reverse proxy fastcgi
agreement proxy_pass
may include a proxy fastcgi
other protocols, including the protocol.
For example, mirroring a website, in this case you need proxy_pass
=>
location /{
proxy_pass http://www.baidu.com;
}
In short, proxy_pass
it is more versatile.
(To be added later)