Nginx reverse proxy and cross-domain

1. Nginx reverse proxy configuration

nginxwebAn important function as a server is 反向代理.

Of course, you can also use nginx forward proxy configuration , this article merely describes how to configure nginxthe 反向代理.

nginx反向代理The instructions do not need to add additional modules, the default comes with proxy_passinstructions, only need to modify the configuration file to achieve 反向代理.

2. What is a reverse proxy server

The reverse proxy function is one nginxof the three main functions (static webserver, reverse proxy, load balancing). nginxGenerally, it is used as a 静态web服务器sum 反向代理服务器, as an web服务器access 静态文件图片、css、js、htmlfile, 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.
Insert picture description here

2.1 Extranet client

In www.csdn.cnthe 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 httprequest to the nginx(reverse proxy server), if the request is accessing static files, nginxas webserver directly return the requested content, if it is a background service logical access, then nginxthe request is forwarded To the back-end service processing.

2.3 Intranet web service

The backend service may be a variety of types, LNMPat ambient php-fpmprocess, Javathe environment tomcat、jettyand other containers, of program logic processing httprequest, generates htmla page or jsonstring back to the client.

For small applications, the back-end service can be nginxdeployed on the same machine.

3. Benefits of reverse proxy server

nginxThe important role of the reverse proxy is to cooperate to upstreamachieve 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

nginxThe reverse proxy does not need to compile additional modules, it comes with proxy_passand fastcgi_passinstructions by default, and the locationreverse proxy function can be realized by adding instructions in the configuration block.

For example, a wordpressprogram for a website wordpressis phpwritten in a language, and then it needs to go through the phpoperating environment, which can be selected as apachean phpextension or php-fpmenvironment. The mainstream choice is php-fpmto php-fpmset it to Unix socketmode 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_passand fastcgi_passmay be substituted for each other use, but there are differences between the two, we can see from the name, fastcgi_passit is used to reverse proxy fastcgiagreement proxy_passmay include a proxy fastcgiother 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_passit is more versatile.



(To be added later)

Guess you like

Origin blog.csdn.net/u013946061/article/details/107739583