为了隐藏我的网站IP,我用另一个VPS上的nginx代理了主服务器。 我想发送访问者真正的IP到我的网站,这里是我的conf.d文件夹中的configuration:
proxy_cache_path /etc/nginx/cacheddata levels=1:2 keys_zone=staticfilecache:180m max_size=500m; proxy_temp_path /etc/nginx/cacheddata/temp; proxy_connect_timeout 30; proxy_read_timeout 120; proxy_send_timeout 120; #IMPORTANT - this sets the basic cache key that's used in the static file cache. proxy_cache_key "$scheme://$host$request_uri"; upstream wordpressapache { #The upstream apache server. You can have many of these and weight them accordingly, #allowing nginx to function as a caching load balancer server 127.0.0.1:8080 weight=1 fail_timeout=120s; } server { listen 80; server_name XXXXXX.com; access_log off; error_log off; set_real_ip_from 0.0.0.0/0; real_ip_header X-Real-IP; real_ip_recursive on; # gzip compression options gzip on; gzip_http_version 1.0; gzip_comp_level 6; gzip_min_length 0; gzip_buffers 16 8k; gzip_proxied any; gzip_types text/plain text/css text/xml text/javascript application/xml application/xml+rss application/javascript application/json; gzip_disable "MSIE [1-6]\."; gzip_vary on; location / { proxy_pass http://XXXXXX/; proxy_redirect off; 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_request_headers on; proxy_max_temp_file_size 0; client_max_body_size 10m; client_body_buffer_size 128k; proxy_connect_timeout 90; proxy_send_timeout 90; proxy_read_timeout 90; proxy_buffer_size 4k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; } }
并用代理服务器上的这段代码创build了一个简单的php页面:
<? echo $_SERVER["REMOTE_ADDR"]; ?>
当我打开页面时,显示nginx服务器IP而不是我的IP。 我做错了什么吗?
您需要在网站正在运行的实际服务器上configuration这些选项:
set_real_ip_from 0.0.0.0/0; real_ip_header X-Real-IP; real_ip_recursive on;
您需要在set_real_ip_from指令中使用您的代理服务器的IP地址,以便只允许该服务器的X-Real-IP标头。
这些指令告诉nginx它应该使用HTTP头中列出的IP地址,而不是TCP连接源的IP地址作为连接的源IP。
然后,在您的代理服务器中,您需要确保它将客户端IP地址的值设置为X-Real-IP标头,就像您的configuration已经设置了它一样。
总之,在代理服务器中设置标题,并在主服务器中告诉Web服务器使用标题。
将实际的远程IP从nginx转发到apache实际上需要在apache端的httpd.conf安装并启用mod_remoteip模块 。
您当前的nginxconfiguration没问题,它在server块中需要proxy_set_header选项:
server { ... proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; ... }
在apache一面,你应该启用所需的模块:
LoadModule remoteip_module modules/mod_remoteip.so
并设置这个指令 :
RemoteIPHeader X-Real-IP RemoteIPInternalProxy 127.0.0.1
用你的nginx IPreplace127.0.0.1如果需要的话…
您将“真正的”IP存储在X-Real-IP ,而不是存储在REMOTE_ADDR 。
以下应该工作:
<? echo $_SERVER["X-Real-IP"]; ?>