【问题标题】:$_SERVER['REMOTE_ADDR'] doesn't work with php-fpm and nginx$_SERVER['REMOTE_ADDR'] 不适用于 php-fpm 和 nginx
【发布时间】:2011-01-29 10:49:41
【问题描述】:

我不知道为什么使用 nginx 这个变量 $_SERVER['REMOTE_ADDR'] 不回显 IP。在其他所有网络服务器上,它都可以正常工作。

有什么建议吗?

【问题讨论】:

  • 试试phpinfo()看看IP是否被传送到某处。
  • @Antwan van Houdt:这真的很有帮助。
  • 我在 phpinfo 上找不到我的 Ip:(
  • @Canceru 是的,它不存在。奇怪!

标签: php nginx


【解决方案1】:

我怀疑这与 nginx(网络服务器)和 fastcgi 之间的接口有关,这是运行 PHP 的 API。

根据您提供的信息,Server API 为:FPM/FastCGI

我建议你仔细看一下PHP如何与nginx一起安装的细节(你没有提供任何)。

如果你不需要 nginx 的性能,那么你可能会发现一个务实的解决方案是只使用 apache。我在 apache 前使用 nginx 作为反向代理,但这会引入一些额外的问题,即将 REMOTE_ADDR 传递给 PHP(特别是 mod_rpaf)。

祝你好运!

【讨论】:

    【解决方案2】:

    @Michael,这是我维护的一个项目,它为 Nginx 与 FPM 的接口提供了正确的 fastcgi 参数。希望对您有所帮助。

    fastcgi_params on Github

    【讨论】:

    • 没有帮助。如果我在 php-fpm.conf 中添加 env[REMOTE_ADDR]=$remote_addr 没有任何变化,除了 _SERVER[REMOTE_ADDR] 出现在 phpinfo() 中并且“没有价值”;
    【解决方案3】:

    这些来自 nginx 的 conf 文件

    用户 http; worker_processes 1;

    error_log /var/log/nginx/error.log; pid /var/run/nginx.pid;

    事件{ worker_connections 1024; # multi_accept on; }

    http { 包括 mime.types; default_type 应用程序/八位字节流; access_log /var/log/nginx/access.log;

    sendfile        on;
    #tcp_nopush     on;
    
    #keepalive_timeout  0;
    keepalive_timeout  65;
    tcp_nodelay        on;
    
    gzip  on;
    gzip_disable "MSIE [1-6]\.(?!.*SV1)";
    

    服务器{ 听 80; server_name www.fireangel.ro fireangel.ro; access_log /var/log/nginx/localhost.access.log;

    默认位置

    location / {
        root    /var/www/html/fireangel.ro/public_html;
        index  index.php;
    }
    

    图像和静态内容被区别对待

    location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|xml)$ {
      access_log        off;
      expires           30d;
      root  /var/www/html/fireangel.ro/public_html;
    
    }
    

    解析/srv/http目录下的所有.php文件

    location ~ .php$ {
        fastcgi_split_path_info ^(.+\.php)(.*)$;
        fastcgi_pass   backend;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME   /var/www/html/fireangel.ro/public_html$fastcgi_script_name;
        include fastcgi_params;
        fastcgi_intercept_errors        on;
        fastcgi_ignore_client_abort     off;
        fastcgi_connect_timeout 60;
        fastcgi_send_timeout 180;
        fastcgi_read_timeout 180;
        fastcgi_buffer_size 128k;
        fastcgi_buffers 4 256k;
        fastcgi_busy_buffers_size 256k;
        fastcgi_temp_file_write_size 256k;
    }
    

    禁止查看 .htaccess 和 .htpassword

    location ~ /\.ht {
        deny  all;
    }
    

    } 上游后端{ 服务器 127.0.0.1:9000; }

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-09
      • 2023-03-03
      相关资源
      最近更新 更多