Nginx PHP页面下载而不是显示

时间:2014-12-03 20:26:53

标签: php nginx download

这是过去两天的一场斗争。我做了很多搜索,无法找到解决问题的方法。

这是我的nginx.conf:

 user www-data;
    worker_processes 4;
    pid /run/nginx.pid;

    events {
            worker_connections 768;
            # multi_accept on;
    }

    http {

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

            ##
            # Logging Settings
            ##

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

            ##
            # Gzip Settings
            ##

            gzip on;
            gzip_disable "msie6";

            # 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/*;
    }

这是我在/ etc / nginx / sites-enabled中的默认文件:

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /usr/share/nginx/html/;
    index index.php index.html index.htm;

    server_name [redacted server IP];

    location / {
        try_files $uri $uri/ =404;
    }

    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }

    location ~ /admin/ {
        auth_basic "Restricted";
        auth_basic_user_file /etc/nginx/.htpasswd;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }


}

该页面存储在/usr/share/nginx/html/admin/index.php中 - 它提示输入用户名/密码(工作正常),但是一旦我登录,它就会下载而不是显示php网页。

如果我把'位置〜/ admin /'阻止位置〜.php $'阻止,它可以工作,但不会进行身份验证。

我在另一个文件夹中编写的脚本在文件夹/usr/share/nginx/html/api/XXX.php'中被调用时效果很好; (它将东西插入MySQL数据库),但它只返回JSON的东西。

你帮助我很棒,我真的很感激。

2 个答案:

答案 0 :(得分:4)

您正在评论php5-fpm袜子,请从以下行删除评论:

fastcgi_pass unix:/var/run/php5-fpm.sock;

更改您的管理路径配置以使用php5-fpm sock:

location ~ /admin/.+\.php$ {
  auth_basic "Restricted";
  auth_basic_user_file /etc/nginx/.htpasswd;
  fastcgi_split_path_info ^(.+\.php)(/.+)$;
  fastcgi_pass unix:/var/run/php5-fpm.sock;
  include fastcgi_params;
  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  fastcgi_index index.php;
 }

或者在~\.php$ {}规则之后移动管理员配置路径 (都为我工作)

确保php5-fpm服务正在运行且您已重新启动nginx

sudo service php5-fpm restart && sudo service nginx restart

答案 1 :(得分:0)

尝试将默认的PHP块添加到您的conf。

location ~ \.php$ {
    fastcgi_split_path_info ^(.+?\.php)(/.*)$;
        if (!-f $document_root$fastcgi_script_name) {
            return 404;
    }

   fastcgi_pass 127.0.0.1:9000;
   fastcgi_index index.php;
   include fastcgi_params;
}

如果没有此块,预期的行为是显示文件。您需要运行并配置PHP FPM才能使其正常工作。有关说明,请访问http://wiki.nginx.org/PHPFcgiExample