我使用两台服务器(proxy_1_front、proxy_2_back),每台服务器都使用 nginx 代理。我只想允许第一台服务器调用第二台服务器
代理_1
upstream angular-frontend {
server 127.0.0.1:4000;
}
server {
listen 80;
server_name frontend.com;
set_real_ip_from frontend-ip;
real_ip_header X-Real-IP;
real_ip_recursive on;
location / {
proxy_pass http://frontend;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Proxy_2
upstream drf-backend {
ip_hash;
server web:8000;
}
server {
listen 80;
server_name api.backend.com;
location / {
return 301 https://$host$request_uri;
}
location /.well-known/acme-challenge/ {
root /var/www/certbot;
}
}
server {
listen 443 ssl;
server_name api.backend.com;
location / {
if ($host != frontend-ip or $http_X_Forwarded_For != frontend-ip) { #Securing server to only work with my firt server
return 403;
}
proxy_pass http://backend/;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
}
但我注意到日志中有两个 IP(proxy_2)发出了相同的请求。第一个是客户端 IP,第二个是 proxy_1 IP。我的问题是:如何从 proxy_1 发送单个 IP,以便我可以检查以下条件
if ($host != frontend-ip or $http_X_Forwarded_For != frontend-ip) { #Securing server to only work with my firt server
return 403;
}
这将只允许我的前端应用程序访问我的后端 API。
先感谢您。