php – 如何在nginx中的sublocations中全局使用FastCGI和Basic Auth?

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 如何在nginx中的sublocations中全局使用FastCGI和Basic Auth?脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我最近部署了我的第一个Nginx设置,一切都很好,除了位置解析让我疯了.我有一个简单PHP fastcgi设置如下:
location ~ \.PHP {
    if (!-e $request_filename) {
            return 404;
    }

    include /etc/Nginx/fastcgi.conf;
    keepalive_timeout 0;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_SCRIPT_NAME;
    fastcgi_pass 127.0.0.1:9000;
}

现在我想用基本的auth保护一些位置,如下所示:

location /madmin {
         auth_basic "Restricted";
         auth_basic_user_file /VAR/www/localhost/admincp/.htpasswd;
 }

有了他的设置,Nginx在转到/ madmin时要求输入密码,但不会在/madmin/foo.PHP询问.如果我将auth位置更改为“location~ ^ / madmin”,那么Nginx会提供PHP文件供下载…

是不是可以在Nginx中配置多个位置?如果没有,这里的解决方法是什么

谢谢你的帮助.

有关Nginx如何处理请求(包括位置)的说明,请参阅 http://nginx.org/en/docs/http/request_processing.html.维基文档也有一些很好的例子.不幸的是,最有可能的是你想要的当前未记录的功能.

如前所述,只有一个位置在Nginx中获胜;但是,您可能不知道Nginx支持位置内的位置.所以你的位置策略实际上可能就像这个示例服务器(0.8.31中的fastcgi.conf):

upstream my-backend {
  localhost:9000;
}
server {
  listen 80;
  server_name my-awesome-PHP.sITe;
  root /path/to/root;
  # The PRotected location
  location /protected {
    auth_basic "Give me codes.";
    auth_basic_user_file /path/to/.htpasswd;
    location ~ \.PHP${
      include fastcgi.conf;
      fastcgi_pass my-backend;
    }
  }      

  # Normal files (blank location is OK,just means serve From root)
  location / {
  }
  # PHP for normal stuff
  location ~ \.PHP${
    include fastcgi.conf;
    fastcgi_pass my-backend;
  } 

}

脚本宝典总结

以上是脚本宝典为你收集整理的php – 如何在nginx中的sublocations中全局使用FastCGI和Basic Auth?全部内容,希望文章能够帮你解决php – 如何在nginx中的sublocations中全局使用FastCGI和Basic Auth?所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。