当前位置: 首页 > 图灵资讯 > 技术篇> proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

来源:图灵教育
时间:2023-06-20 09:29:04

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

用一个例子演示会更清晰