我在我的网站着陆页上设置密码时遇到问题。 我已经看过这个问题几次,并试图在每个postupvoted修复。 到目前为止,我尝试更改.htpasswd的权限,更改.htpasswd的位置,使用.htpasswd的绝对path位置,并将auth_basic和auth_basic_user_file指令放置在“位置”和主“服务器”块之外,但stream行请求用户名和密码的问题仍然没有显示,当我去我的url。 我想要的是当我浏览到我的主要URL没有任何特定的目录或文件时,popup密码提示。 我查看了很多教程,并使用了htpasswd命令来创build.htpasswd文件。 从我所了解的这是/ etc / nginx / sites-enabled / default的正确configuration。 我在这里做错了什么?
这是我的configuration文件:
server { listen 80 default_server; listen [::]:80 default_server; root /var/www/html; server_name example.com www.example.com; return 310 https://$server_name$request_uri; location / { try_files $uri $uri/ =404; auth_basic "Under Construction"; auth_basic_user_file /etc/nginx/.htpasswd; } }
您的return语句会覆盖location语句。
你需要有这样的configuration:
server { listen 80 default_server; listen [::]:80 default_server; root /var/www/html; server_name example.com www.example.com; location = / { try_files $uri $uri/ =404; auth_basic "Under Construction"; auth_basic_user_file /etc/nginx/.htpasswd; } location / { return 310 https://$server_name$request_uri; }
}
所以, location = / statement恰好匹配一个path,这里是网站的根URI。
然后该location /匹配网站上的所有其他path。
nginxparsinglocation块,以便如果多个location块匹配相同的path,则始终使用= location 。