我已按照说明进行操作,但仍然无法使用密码保护我的网站。这是我的app-nginx.config的样子:
server {
listen 80;
server_name Server_Test;
auth_basic "Restricted";
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
...
}
我要去哪里错了?我从教程站点复制并粘贴了此权限。
最佳答案
刚刚使我的nginx服务器正常工作,甚至对其进行了配置以保护我的根文件夹访问。我想与您分享我的发现,并且在此过程中也为该问题提供了一个很好且有效的答案。
作为nginx的新用户(版本1.10.0-Ubuntu)。
我遇到的第一个问题是知道文件位置,所以这里是关键位置:
了解您的位置:
主文件夹位置:/etc/nginx
默认站点位置:/var/www/
甚至/ver/www/html/
(html
文件夹内将是index.html文件-希望您知道该怎么做。)
配置文件:
主配置文件:/etc/nginx/nginx.conf
当前站点服务器conf:/etc/nginx/sites-enabled
(在第一次安装时,有一个名为default
的文件,您需要使用sudo
进行更改(例如:sudo vi default
)
添加密码:
因此,既然您已经知道了播放器(无论如何对于静态的开箱即用的站点),我们就将一些文件放在“ html”文件夹中,并为其添加密码保护。
要设置密码,我们需要做两件事:
1.创建一个密码文件(我们希望有尽可能多的用户,但我将以1结算)。
2.配置当前服务器(“默认”)以限制此页面,并使用1中的文件启用密码保护。
1.创建一个密码:
我要用于此的行是:sudo htpasswd -c /etc/nginx/.htpasswd john
(将提示您输入并重新输入密码),您可以在此处单行执行:sudo htpasswd -c /etc/nginx/.htpasswd john [your password]
我将解释命令的每个部分:sudo htpasswd
-使用更高的权限进行操作。-c
-用于:创建文件(要将另一个用户添加到现有用户中,请跳过此参数)/etc/nginx/.htpasswd
-创建的文件的名称
(文件夹/ etc / nginx中的“ .htpsswd”)john
是用户名(在提示的“用户”字段中输入)password
是此特定用户名所需的密码。 (提示时..)
通常htpasswd
命令对您不起作用,因此您必须安装它的软件包:
使用:sudo apt-get install apache2-utils
(如果失败,请尝试使用sudo apt-get update
,然后重试)
2.让我们将服务器配置为使用此文件进行身份验证
让我们使用以下行来编辑当前(默认)服务器conf文件:sudo vi /etc/nginx/sites-enabled/default
(您不必使用'vi',但我喜欢。..)
删除大多数注释(#)后,文件如下所示
# Default server configuration
#
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
}
我们需要在位置块内添加两行('/'指向站点的根文件夹),因此看起来像这样:
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
auth_basic "Restricted Content";
auth_basic_user_file /etc/nginx/.htpasswd;
}
我将解释这些新行:
auth_basic "Restricted Content";
-定义访问管理的类型auth_basic_user_file /etc/nginx/.htpasswd;
-将我们创建的文件(/etc/nginx/.htppasswd)定义为此身份验证的密码文件。让我们重新启动服务并享受一个受密码保护的网站:
sudo service nginx restart
中提琴-享受...
这是一些更出色的教程:
Very good explanation
Another goo tutorial
关于nginx - Nginx-密码保护不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12467900/