【问题标题】:Nginx - Password Protect PHP ScriptNginx - 密码保护 PHP 脚本
【发布时间】:2011-05-18 02:13:00
【问题描述】:

如何在 Nginx 中使用密码保护单个 ph php 脚本。我使用 nginx 作为网络服务器并代理到 php-fastcgi。我无法让位置块按预期运行。

这是我正在尝试的内容。

location /admin\.php$ {
    auth_basic "Valid User Required";
    auth_basic_user_file /etc/nginx/http-auth;
}
location ~\.php$ {
    root /var/www/nginx/vhosts/site;
    include /etc/nginx/fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_pass phpfcgi;
}

【问题讨论】:

    标签: authentication passwords nginx


    【解决方案1】:
    位置 /admin\.php$ {
        auth_basic "需要有效用户";
        auth_basic_user_file /etc/nginx/http-auth;
    +根/var/www/nginx/vhosts/站点;
    + 包括 /etc/nginx/fastcgi_params;
    + fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    + fastcgi_pass phpfcgi;
    }

    这可能对你有帮助。

    【讨论】:

      【解决方案2】:

      第一期:

      您正在匹配前缀字符串而不是正则表达式:

      1. 前缀字符串按字面意思匹配。
      2. 即使匹配前缀字符串,搜索也会继续使用下面的正则表达式 (~\.php$)。如果匹配,前缀字符串匹配将被忽略。

      问题 #1 的解决方案:

      添加~ 以执行正则表达式匹配:~/admin\.php$。

      第二期:

      现在你的块匹配了,你想将其中的 php 脚本传递给 fastcgi,否则它们将作为文本文件提供,而不被解析。

      问题 #2 的解决方案:

      在~/admin\.php$ 位置块内嵌套一个~\.php$ 位置块,最终结果如下所示:

      location ~/admin\.php$ {
          auth_basic "Valid User Required";
          auth_basic_user_file /etc/nginx/http-auth;
          location ~\.php$ {
              root /var/www/nginx/vhosts/site;
              include /etc/nginx/fastcgi_params;
              fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
              fastcgi_pass phpfcgi;
          }
      }
      location ~\.php$ {
          root /var/www/nginx/vhosts/site;
          include /etc/nginx/fastcgi_params;
          fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
          fastcgi_pass phpfcgi;
      }
      

      参考:

      请参阅此相关的post,此answer 上的位置块优先级,以及有关该主题的 Nginx docs。

      【讨论】:

        猜你喜欢
        • 2012-02-21
        • 1970-01-01
        • 1970-01-01
        • 2019-09-18
        • 1970-01-01
        • 1970-01-01
        • 2015-10-19
        • 1970-01-01
        • 2015-10-20
        相关资源
        最近更新 更多