Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does nginx implement reverse proxy?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report