Получение IP-адреса клиента в Django из конфига прокси nginx [duplicate]
У меня есть следующая настройка для запуска моего сервера Django, и я хочу получить IP-адрес клиента на сервере Django, но он выдает мне неправильный IP-адрес
Nginx proxy conf
events {
worker_connections 1024;
}
http {
log_format custom_format '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /first_access.log custom_format;
client_max_body_size 50M;
server_tokens off;
upstream backend {
server backend:8000;
}
server {
listen 80 default_server;
server_name _;
return 301 https://$host$request_uri;
}
server {
listen 443;
server_name www.$ENV-api.in $ENV-api.in;
ssl on;
ssl_certificate /certs/server.crt;
ssl_certificate_key /certs/server.key;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;
ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256';
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://backend;
add_header X-Frame-Options "DENY" always;
add_header Content-Security-Policy "frame-ancestors 'self';" always;
add_header X-Content-Type-Options "nosniff" always;
add_header X-XSS-Protection "1; mode=block" always;
add_header Referrer-Policy "no-referrer" always;
}
}
}
Этот вышеуказанный conf направляет запрос на следующий nginx conf
vents {
worker_connections 1024;
}
http {
log_format custom_format '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /second_access.log custom_format;
client_max_body_size 50M;
server {
include "/etc/nginx/mime.types";
listen 8000;
server_name django;
gzip on;
gzip_min_length 1000;
gzip_proxied expired no-cache no-store private auth;
gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $http_x_real_ip;
proxy_set_header X-Forwarded-For $http_x_forwarded_for;
proxy_pass http://core_service:8001;
}
location = /favicon.ico {
log_not_found off;
}
location /static/ {
alias /static/;
}
location /media/ {
alias /media/;
}
}
}
Это 2-й запрос ngnix к django gunicon
В django я использую следующий код для получения IP-адреса
from django.utils.deprecation import MiddlewareMixin
class GetClientIPMiddleware(MiddlewareMixin):
def process_request(self, request):
x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
if x_forwarded_for:
ip = x_forwarded_for.split(',')[0].strip()
else:
ip = request.META.get('REMOTE_ADDR')
request.client_ip = IP
Все эти 3 установки заключены в контейнер docker
version: '3'
services:
nginx-proxy:
build:
context: .
dockerfile: Dockerfile
container_name: nginx-proxy
ports:
- "443:443"
- "80:80"
volumes:
- "./docker_config/certs:/certs"
environment:
TZ: Asia/Kolkata
ENV: ${ENV}
env_file:
- .env
core_service:
container_name: core_service
build:
context: ./docker_config/python_config
working_dir: /home/python/app
volumes:
- "./microservices/core_service:/home/python/app"
- "./docker_config/python_config/deploy_commands.sh:/deploy_commands.sh"
- "./logs:/logs"
env_file:
- .env
depends_on:
- redis
environment:
TZ: Asia/Kolkata
links:
- redis
command:
- /bin/bash
- -c
- |
echo "Starting django server ..."
/deploy_commands.sh
backend:
container_name: backend
image: nginx:1.13-alpine
ports:
- "8000:8000"
environment:
TZ: Asia/Kolkata
volumes:
- ./microservices/core_service/static:/static
- ./microservices/core_service/media:/media
- ./docker_config/python_config/nginx/nginx.conf:/etc/nginx/nginx.conf:ro
- ./docker_config/certs:/certs
Теперь я вызываю один API в Django, поэтому в Django я получаю разные IP, а не реальный IP клиента.