大网数据广告活动
服务器运维问答

nginx PHP files downloading instead of executing

· 52CE 我爱测

I'm trying to achieve following thing. I've main website under domain root directory. It's working fine. i.e PHP files are executed. After some time I've added "auth" directory with basic HTTP authentication. Next, I uploaded PHP…

问题原文

原帖链接 · Andrey Andreevsky · CC BY-SA 3.0

I'm trying to achieve following thing. I've main website under domain root directory. It's working fine. i.e PHP files are executed. After some time I've added "auth" directory with basic HTTP authentication. Next, I uploaded PHP program into "auth" directory, but PHP files are downloading instead of executing.

  • domain.name/test.php -> executing
  • domain.name/sub/file.php -> executing
  • domain.name/auth/protected.php -> downloading instead of executing

Here is sites-available conf

server {
    charset utf-8;

    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;

    # Add index.php to the list if you are using PHP
    index index.php index.html index.htm index.nginx-debian.html;
    server_name xxx.com;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        # try_files $uri $uri/ =404;
        try_files $uri $uri/ /index.php?$args;
    }

    location ^~ /auth {
        auth_basic "Restricted Content";
        auth_basic_user_file /var/www/html/auth/.htpasswd;
    }

    location ^~ /protected {
        deny all;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;

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

    location ~ /\.(ht|git|svn) {
        deny all;
    }
}

Currently I've following solution, but I believe it's not a right way.

    location ^~ /auth {
        auth_basic "Restricted Content";
        auth_basic_user_file /var/www/html/auth/.htpasswd;
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;

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

Following solution doesn't work

        location ^~ /auth {
                auth_basic "Restricted Content";
                auth_basic_user_file /var/www/html/auth/.htpasswd;
                try_files $uri @php-fpm;
        }

        location @php-fpm {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
        }

社区高分解答

原帖链接 · Kalman · CC BY-SA 3.0

Had the same issue!

Found the answer here, http://www.ceus-now.com/nginx-password-protect-directory-downloads-source-code/

Because we are adding ^~ we are leaving behind some other settings (not sure why we need to add this but it was the only way to get it to actually pull up the authentication :( )

include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/var/lib/php5-fpm/web11.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors on;

I tried various combinations to see what was/not needed and each line is needed. I wish I understood these things better.

UPDATE: July 27th 2016:

So I did some reading and finally understand why we have this issue.

In short when we install FastCGI and php-fpm it creates a directive in the nginx server files (which file depends on your server install).

So in mine I have the following...

        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
    #
    #       # With php7.0-cgi alone:
    #       fastcgi_pass 127.0.0.1:9000;
    #       # With php7.0-fpm:
            fastcgi_pass unix:/run/php/php7.0-fpm.sock;

As you will notice these directives are in location / and not in the global settings. Since we want to secure a particular folder these directives are not inherited. Therefore, we need to declare it again.

If we wanted to follow the DRY ("Don't Repeat Yourself") principle, then we would declare the PHP-fpm and FastCGI in the global settings. We can do this by moving it to below (example, but it just needs to be outside of location directive). Here is an example.

root /var/www/html;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_index index.php;

Hope this helps. I realized the above by reading https://www.digitalocean.com/community/tutorials/understanding-and-implementing-fastcgi-proxying-in-nginx

Disclaimer: I am no pro so if you see mistakes be nice and just let me know and I will hopefully update.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-05-03)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。