douxun2023 2014-12-03 20:26
浏览 81
已采纳

Nginx PHP页面下载而不是显示

This has been a struggle for the past two days. I've done a lot of searching, couldn't find anything to fix it.

Here's my 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/*;
    }

Here's my default file in /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;
    }


}

The page is stored in /usr/share/nginx/html/admin/index.php -- it prompts for the username/password (which works fine), but as soon as I login, it downloads instead of showing the php webpage.

If I put the 'location ~ /admin/' block after the 'location ~ .php$' block, it works, but doesn't authenticate.

There are scripts I wrote in another folder that work fine when called 'in the folder /usr/share/nginx/html/api/XXX.php' (which inserts stuff into the MySQL database), but it just returns JSON stuff.

You are awesome for helping me out, I really do appreciate it.

  • 写回答

2条回答 默认 最新

  • dongtuji0992 2014-12-03 20:46
    关注

    You are commenting the php5-fpm sock, delete the comment from the following line:

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

    Change your admin path configuration to use the 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;
     }
    

    OR move your admin configuration path AFTER the ~\.php$ {} rule (both worked for me)

    make sure the php5-fpm service is running and you've restarted nginx

    sudo service php5-fpm restart && sudo service nginx restart
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波