proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

1.nginx 配置模板

server {

    listen 80;
    client_max_body_size 512M;

    proxy_set_header Connection "";
    proxy_http_version 1.1;
    proxy_connect_timeout  300;
    proxy_read_timeout 300;

    access_log /var/log/nginx/ralph-access.log;
    error_log /var/log/nginx/ralph-error.log;

    location /static {
        alias /usr/share/ralph/static;
        access_log        off;
        log_not_found     off;
        expires 1M;
    }

    #location /media {
    #    alias /var/local/ralph/media;
    #    add_header Content-disposition "attachment";
    #}

    location / {
        proxy_pass http://127.0.0.1:8000;
        include /etc/nginx/uwsgi_params;
        proxy_set_header Host $http_host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}
如果想要通过X-Forwarded-For获得用户ip,就必须先使用proxy_set_header    X-Forwarded-For     $proxy_add_x_forwarded_for; 这样就可以获得用户真实ip。

参考: https://blog.51cto.com/gyj110/2056933

原文地址:https://www.cnblogs.com/hixiaowei/p/10802719.html