Get the App
SLTechnology News&Howtos  ›  Servers  › 

How does nginx implement reverse proxy?

Shulou Source: shulou.com Published: 2022-06-03 05:32:56 10月06日 Update

1. The proxy's configuration location / {proxy_pass http://192.168.0.28; # generic match forwards all incoming traffic to "http://192.168.0.28" proxy_set_header Host $http_host" # the host of the request header is forwarded to the host of the backend host, because the client accesses the proxy server with a domain name, while the proxy accesses the backend web using ip to access the lost domain name, so it needs to be added to identify the backend virtual host. Proxy_set_header zhengshiIP $remote_addr; # $ip of the remote_addr request, assign the requested ip to "zhengshiIP". The backend server can get the real ip by referencing it in the log configuration. Note that the following reference needs to be added with "http_"} 2. Backend web configuration, use of logs

Configuration of logs

# Log configuration, the last two are added by default, one is the real ip, and the other is the real url accessed

The following picture of effect

Backend virtual host configuration

Server {listen 80; server_name www.333.com; root / 333; index index.html; location / {}}

# create a new conf file in the "/ etc/nginx/conf.d" directory

3. Set View Recursive access ip

Experimental topology

Proxy 192.168.0.13 configuration

Location / {proxy_pass http://192.168.0.25; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}

Proxy 192.168.0.25 configuration

Location / {proxy_pass http://192.168.0.28; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}

View access.log Log

The use of 4.include

Vim / etc/nginx/proxy_params

# add a configuration file "proxy_params" to the configuration directory of nginx

Proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for

Call

Location / {proxy_pass http://192.168.0.25; include proxy_params; # calls the newly added configuration file. Here is the relative path, and the file should be in the configuration folder of nginx, otherwise the decision path will be written.

# include can be understood as a function. Write the configuration segment in the nginx configuration directory, and the master configuration can be called multiple times.

Tags: Configuration agent file log host directory domain name server virtual host path service following figure two function reason header client client topology effect Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple NVidia Xiaomi Shulou Information macOS