nginx PHP文件下载而不是执行

And*_*sky 4 nginx conf

我正在努力实现以下目标。我在域根目录下有主网站。它工作正常。即执行 PHP 文件。一段时间后,我添加了具有基本 HTTP 身份验证的“auth”目录。接下来,我将 PHP 程序上传到“auth”目录,但 PHP 文件正在下载而不是执行。

  • domain.name/test.php -> 执行
  • domain.name/sub/file.php -> 执行
  • domain.name/auth/protected.php -> 下载而不是执行

这是站点可用的配置文件

服务器 {
    字符集 utf-8;

    听 80 default_server;
    听 [::]:80 default_server;

    根 /var/www/html;

    # 如果您使用的是 PHP,请将 index.php 添加到列表中
    index index.php index.html index.htm index.nginx-debian.html;
    server_name xxx.com;

    地点 / {
        # 首先尝试将请求作为文件提供服务,然后
        # 作为目录,然后回退到显示 404。
        # try_files $uri $uri/ =404;
        try_files $uri $uri//index.php?$args;
    }

    位置 ^~ /auth {
        auth_basic "受限内容";
        auth_basic_user_file /var/www/html/auth/.htpasswd;
    }

    位置 ^~ /protected {
        否认一切;
    }

    位置 ~ \.php$ {
        包括片段/fastcgi-php.conf;

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

    位置 ~ /\.(ht|git|svn) {
        否认一切;
    }
}

目前我正在遵循解决方案,但我认为这不是正确的方法。

    位置 ^~ /auth {
        auth_basic "受限内容";
        auth_basic_user_file /var/www/html/auth/.htpasswd;
        位置 ~ \.php$ {
            包括片段/fastcgi-php.conf;

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

以下解决方案不起作用

        位置 ^~ /auth {
                auth_basic "受限内容";
                auth_basic_user_file /var/www/html/auth/.htpasswd;
                try_files $uri @php-fpm;
        }

        位置@php-fpm {
                包括片段/fastcgi-php.conf;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
        }

Kal*_*man 6

有同样的问题!

在这里找到答案,http://www.ceus-now.com/nginx-password-protect-directory-downloads-source-code/

因为我们正在添加 ^~ 我们留下了一些其他设置(不知道为什么我们需要添加它,但这是让它实际提取身份验证的唯一方法:()

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;
Run Code Online (Sandbox Code Playgroud)

我尝试了各种组合来查看需要/不需要什么以及需要每一行。我希望我能更好地理解这些事情。

更新:2016 年 7 月 27 日:

所以我做了一些阅读,终于明白为什么我们会遇到这个问题。

简而言之,当我们安装 FastCGI 和 php-fpm 时,它会在 nginx 服务器文件中创建一个指令(哪个文件取决于您的服务器安装)。

所以在我的我有以下...

        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;
Run Code Online (Sandbox Code Playgroud)

你会注意到这些指令在 location / 而不是在全局设置中。由于我们要保护特定文件夹,因此不会继承这些指令。因此,我们需要再次声明。

如果我们想遵循 DRY(“不要重复自己”)原则,那么我们将在全局设置中声明 PHP-fpm 和 FastCGI。我们可以通过将它移动到下面来做到这一点(例如,但它只需要在 location 指令之外)。这是一个例子。

root /var/www/html;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_index index.php;
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助。我通过阅读https://www.digitalocean.com/community/tutorials/understanding-and-implementing-fastcgi-proxying-in-nginx意识到了上述问题

免责声明:我不是专业人士,所以如果你看到错误是好的,请告诉我,我会希望更新。