Nginx禁用.htaccess和隐藏文件,但允许。well-known目录

我有一个Nginx服务器,并禁用nginx_vhost.conf隐藏文件

 ## Disable .htaccess and other hidden files location ~ /\. { deny all; access_log off; log_not_found off; } 

但是,LetsEncrypt需要访问.well .well-known目录。

如何允许.well-known目录并拒绝其他隐藏的文件?

我已经提供了一个关于如何在我的网站上使用Let's Encrypt与NGINX的完整的一步一步的教程。

关键部分是:

  • 官方客户端只是好的,在亚马逊Linux上真的很差。 我推荐一个不同的客户, ACME 。
  • 用我的推荐客户端的webroot方法使用此位置。 请注意,请求是通过http服务的,而不是https。

你根本不需要你的https模块中的监听器,这些都是在https上完成的。 这只是为了certificate你控制的领域,它不提供任何私人或秘密。

 # Answer let's encrypt requests, but forward everything else to https server { listen 80; server_name example.com www.example.com access_log /var/log/nginx/access.log main; # Let's Encrypt certificates with Acmetool location /.well-known/acme-challenge/ { alias /var/www/.well-known/acme-challenge/; } location / { return 301 https://www.example.com$request_uri; } } 

上面链接完整的一步一步的指导。

Nginx使用正则expression式按照它们在configuration文件中出现的顺序应用位置。

因此, 您当前的位置之前添加这样的条目将有助于您。

 location ~ /.well-known { allow all; } 

其他解决scheme并没有帮助我。

我的解决scheme是包含一个负面的正则expression式 。 你的代码块应该如下所示:

 ## Disable .htaccess and other hidden files location ~ /\.(?!well-known).* { deny all; access_log off; log_not_found off; } 

它会阻止除了以.well-known开头的所有点文件

PS:我也会加return 404; 到块。

添加(在之前或之后):

 location ^~ /.well-known/ { log_not_found off; } 

在阅读文档并尝试之后find答案。