php`$ _SERVER ['PHP_SELF']`为空

时间:2019-02-09 11:47:13

标签: php ubuntu nginx

我的nginx和php配置有问题。由于某些原因,$_SERVER['PHP_SELF']为空。

我正在跑步:

  • Ubuntu 18.04
  • nginx(nginx版本:nginx / 1.14.0(Ubuntu))
  • php(PHP 7.2.10-0ubuntu0.18.04.1)

我不知道可能导致此问题的原因。我在网上读了很多书,但是找不到任何解决方案。

只是看看是否还有更多信息遗漏,我检查了从print_r($_SERVER);得到的信息,这就是我正在得到的信息(经过审查的私人信息...):

  

Array([USER] => www-data [HOME] => / var / www [HTTP_COOKIE] => CENSORED [HTTP_ACCEPT_LANGUAGE] => zh-CN,en; q = 0.9, he-IL; q = 0.8,he; q = 0.7,zh-CN; q = 0.6,ru; q = 0.5 [HTTP_ACCEPT_ENCODING] => gzip,deflate,br [HTTP_ACCEPT] => text / html,application / xhtml + xml,application / xml; q = 0.9,image / webp,image / apng, / ; q = 0.8 [HTTP_USER_AGENT] => CENSORED [HTTP_UPGRADE_INSECURE_REQUESTS] => 1 [ HTTP_CACHE_CONTROL] => max-age = 0 [HTTP_CONNECTION] =>保持活动[HTTP_HOST] => cumta.morhaviv.com [SCRIPT_FILENAME] => /var/www/morhaviv.com/public_html/cumta/css/test.php [REDIRECT_STATUS] => 200 [SERVER_NAME] => www.cumta.morhaviv.com [SERVER_PORT] => 443 [SERVER_ADDR] => 153.92.209.235 [REMOTE_PORT] => 22964 [REMOTE_ADDR] => 176.231.2.86 [SERVER_SOFTWARE] = > nginx / 1.14.0 [GATEWAY_INTERFACE] => CGI / 1.1 [HTTPS] =>在[REQUEST_SCHEME] => https [SERVER_PROTOCOL] => HTTP / 1.1 [DOCUMENT_ROOT] => /var/www/morhaviv.com/public_html/累积[DOCUMENT_URI] => /css/test.php [REQUEST_URI] => /css/test.php [SCRIPT_NAME] => /css/test.php [CONTENT_LENGTH] => [CONTENT_TYPE] => [REQUEST_METHOD] => GET [QUERY_STRING] => [PATH_INFO] => [FCGI_ROLE] =>响应者[PHP_SELF] => [REQUEST_TIME_FLOAT ] => 1549710420.5126 [REQUEST_TIME] => 1549710420)

我的nginx.conf:

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
    worker_connections 768;
    # multi_accept on;
}

http {
    include        fastcgi_params; 
    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # SSL Settings
    ##

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

我知道这个问题可能缺少更多需要的信息,但是我真的不知道还有什么重要的,所以请发表评论,我会补充缺少的信息。

谢谢您的帮助!

3 个答案:

答案 0 :(得分:0)

确保在Nginx配置文件中包含以下行:

include fastcgi_params

除此之外,我认为没有理由不起作用。

希望有帮助

答案 1 :(得分:0)

一些使用php安装nginx服务器的教程,坚持将php.ini文件的参数cgi.fix_pathinfo更改为0。

所以我的解决方案在php.ini中有所变化:

cgi.fix_pathinfo = 0 

恢复默认值:

cgi.fix_pathinfo = 1

就是这样。

找到解决方案,感谢kenzotenma对他的回答的评论,其链接为:https://www.nginx.com/resources/wiki/start/topics/examples/phpfcgi/

答案 2 :(得分:0)

fix_pathinfo 设置为1是潜在的漏洞。而是将此行添加到nginx php_fpm 部分:

fastcgi_param SCRIPT_FILENAME $ document_root $ fastcgi_script_name;

并手动分配全局变量:

$ _ SERVER ['PHP_SELF'] =!empty($ _ SERVER ['PHP_SELF']))吗? $ _SERVER ['PHP_SELF']:'/'。basename($ _ SERVER ['SCRIPT_FILENAME']);

More info