开发者

$_SERVER['REMOTE_ADDR'] doesn't work with php-fpm and nginx

开发者 https://www.devze.com 2023-02-07 22:03 出处:网络
I don\'t know why with开发者_开发知识库 nginx this variable $_SERVER[\'REMOTE_ADDR\'] doesn\'t echo an IP. On every other web server it works as it should.

I don't know why with开发者_开发知识库 nginx this variable $_SERVER['REMOTE_ADDR'] doesn't echo an IP. On every other web server it works as it should.

Any suggestions?


I suspect it has something to do with the interface between nginx (the webserver) and fastcgi, which is the API in which PHP is running.

According to your info provided, the Server API is: FPM/FastCGI

I suggest you take a hard look at the details of how PHP is installed with nginx (you have not provided any).

If you do not require the performance of nginx, then you may find a pragmatic solution is to just use apache. I use nginx as a reverse proxy in front of apache, but that introduces some additional issues with getting the REMOTE_ADDR passed to PHP (notably, mod_rpaf).

Good luck!


@Michael, here is a project I maintain which provides the proper fastcgi parameters for interfacing Nginx with FPM. Hope it helps.

fastcgi_params on Github


These are from the conf file from nginx

user http; worker_processes 1;

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

events { worker_connections 1024; # multi_accept on; }

http { include mime.types; default_type application/octet-stream; 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)";

server { listen 80; server_name www.fireangel.ro fireangel.ro; access_log /var/log/nginx/localhost.access.log;

Default location

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

Images and static content is treated different

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

}

Parse all .php file in the /srv/http directory

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;
}

Disable viewing .htaccess & .htpassword

location ~ /\.ht {
    deny  all;
}

} upstream backend { server 127.0.0.1:9000; }

}

0

精彩评论

暂无评论...
验证码 换一张
取 消