来自使用Nginx + Wordpress的帖子的相同IP

时间:2015-02-06 18:20:32

标签: php wordpress apache nginx

我正在使用一个名为WP-Polls的插件,我需要查看所有不同的IP来控制选民。所以,在这个插件中有一个获取ips的功能:

### Function: Get IP Address
if(!function_exists('get_ipaddress')) {
    function get_ipaddress() {
        if (empty($_SERVER["HTTP_X_FORWARDED_FOR"])) {
            $ip_address = $_SERVER["REMOTE_ADDR"];
        } else {
            $ip_address = $_SERVER["HTTP_X_FORWARDED_FOR"];
        }
        if(strpos($ip_address, ',') !== false) {
            $ip_address = explode(',', $ip_address);
            $ip_address = $ip_address[0];
        }
        return esc_attr($ip_address);
    }
}

此函数从REMOTE_ADDR中查找地址,但此标头包含服务器的相同IP。所以,我不能通过IP限制投票。我也尝试过,通过ECHO在同一个函数中查找所有标题以检查所有可能性:

echo "HTTP_CLIENT_IP: " . $_SERVER["HTTP_CLIENT_IP"];
echo "HTTP_X_FORWARDED_FOR: " . $_SERVER["HTTP_X_FORWARDED_FOR"];
echo "HTTP_X_FORWARDED: " . $_SERVER["HTTP_X_FORWARDED"];
echo "HTTP_X_CLUSTER_CLIENT_IP: " . $_SERVER["HTTP_X_CLUSTER_CLIENT_IP"];
echo "HTTP_FORWARDED_FOR: " .$_SERVER["HTTP_FORWARDED_FOR"];
echo "REMOTE_ADDR: " . $_SERVER["REMOTE_ADDR"];
echo "REMOTE_HOST: " . $_SERVER["REMOTE_HOST"];
echo "HTTP_X_REAL_IP: " . $_SERVER["HTTP_X_REAL_IP"];
echo "REMOTE_HOST: " . $_SERVER["REMOTE_HOST"];
echo "REQUEST_URI: " .  $_SERVER["REQUEST_URI"];

但只有在我的REMOTE_ADDR中,我才能看到某些内容(来自我服务器的IP)。

我认为这个问题的解决方案是在Nginx设置中。当我使用APACHE代替Nginx时,一切正常,从REMOTE_ADDR获取正确的信息。

有没有人能解决这个问题?

1 个答案:

答案 0 :(得分:1)

Nginx通过代理运行PHP,因此当您尝试访问REMOTE_ADDR时,PHP会将Nginx视为远程客户端,从而为您提供自己的IP地址。要访问此信息,您需要通过代理重写它。

proxy_set_header  X-Real-IP        $remote_addr;
proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;

有关详细信息,请参阅https://rtcamp.com/tutorials/nginx/forwarding-visitors-real-ip/

在我的设置中,我通过fastcgi使用spawn-fcgi并包含一个名为fastcgi_params的文件,该文件会覆盖代理中丢失的许多$_SERVER个变量。如果有用,该文件的内容如下。

<强> fastcgi_params:

fastcgi_param  QUERY_STRING      $query_string;
fastcgi_param  REQUEST_METHOD    $request_method;
fastcgi_param  CONTENT_TYPE      $content_type;
fastcgi_param  CONTENT_LENGTH    $content_length;

fastcgi_param  SCRIPT_NAME       $fastcgi_script_name;
fastcgi_param  REQUEST_URI       $request_uri;
fastcgi_param  DOCUMENT_URI      $document_uri;
fastcgi_param  DOCUMENT_ROOT     $document_root;
fastcgi_param  SERVER_PROTOCOL   $server_protocol;
fastcgi_param  HTTPS             $https if_not_empty;

fastcgi_param  GATEWAY_INTERFACE CGI/1.1;
fastcgi_param  SERVER_SOFTWARE   nginx/$nginx_version;

fastcgi_param  REMOTE_ADDR       $remote_addr;
fastcgi_param  REMOTE_PORT       $remote_port;
fastcgi_param  SERVER_ADDR       $server_addr;
fastcgi_param  SERVER_PORT       $server_port;
fastcgi_param  SERVER_NAME       $server_name;

# override for wp-polls plugin
fastcgi_param  HTTP_X_FORWARDED_FOR $remote_addr;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS   200;

_.php.conf: - 包含在我的服务器块中,用于在WordPress中进行PHP处理。

location ~ \.php$ {

    # proxy buffers - no 502 errors!
    proxy_buffer_size            128k;
    proxy_buffers                4 256k;
    proxy_busy_buffers_size      256k;
    proxy_ignore_client_abort    on;

    # fastcgi buggers - no 502 errors!
    fastcgi_buffering            on;
    fastcgi_buffer_size          16k;
    fastcgi_buffers              16 16k;

    # max timeouts (should match php.ini)
    fastcgi_connect_timeout      600s;
    fastcgi_send_timeout         600s;
    fastcgi_read_timeout         600s;

    # index page
    fastcgi_index                index.php;

    # proxy request to spawn-fcgi running on port 12321
    fastcgi_pass                 localhost:12321;

    # default fastcgi_params
    include                      fastcgi_params;

    # override fastcgi_params
    fastcgi_param                SERVER_NAME $host;
    fastcgi_param                SCRIPT_FILENAME $document_root$fastcgi_script_name;

    break;
}