我已按照说明操作,但仍无法密码保护我的网站.这是我的app-nginx.config看起来像:
server {
listen 80;
server_name Server_Test;
auth_basic "Restricted";
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
...
}
Run Code Online (Sandbox Code Playgroud)
我哪里错了?我从教程网站复制并粘贴了这个.
Ama*_*man 15
确保Nginx可以访问密码文件.auth_basic_user_file的路径是相对于目录的nginx.conf.因此,如果您nginx.conf位于,/usr/local/nginx您可以将您的指令更改为:
auth_basic_user_file conf/htpasswd;
Run Code Online (Sandbox Code Playgroud)
并且文件必须是可读的.
工作人员应该可以读取此文件,从非特权用户运行.E. g.当nginx从www运行时,您可以将权限设置为:
Run Code Online (Sandbox Code Playgroud)chown root:nobody htpasswd_file chmod 640 htpasswd_file
- 来自http://wiki.nginx.org/HttpAuthBasicModule
刚刚使我的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.配置当前服务器("default")以限制此页面并使用1中的文件启用密码保护.
1.让我们创建一个密码:
我想要使用的这一行是:(
sudo htpasswd -c /etc/nginx/.htpasswd john你会得到一个输入并重新输入密码的提示),你可以在一行中完成:
sudo htpasswd -c /etc/nginx/.htpasswd john [your password]
我将解释命令的每个部分:
sudo htpasswd - 使用更高的权限做到这一点.-c - for:create file(将另一个用户添加到现有用户跳过此参数)/etc/nginx/.htpasswd - 创建的文件的名称(文件夹/ etc/nginx中的'.htpsswd') john 是用户的名称(在提示的'user'字段中输入) password是此特定用户名所需的密码.(提示时..)通常htpasswd命令对你不起作用,所以你必须安装它的包:
使用:( sudo apt-get install apache2-utils如果失败尝试使用sudo apt-get update并再试一次)
2.让我们配置服务器以使用此文件进行身份验证
让我们用这一行编辑当前(默认)服务器配置文件:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
我们需要在块内添加两行('/'指向站点的根文件夹),所以它看起来像这样:
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;
}
Run Code Online (Sandbox Code Playgroud)
我将解释这些新线:
auth_basic "Restricted Content"; - 定义访问管理的类型auth_basic_user_file /etc/nginx/.htpasswd; - 将我们创建的文件(/etc/nginx/.htppasswd)定义为此身份验证的密码文件.让我们重启服务并享受受密码保护的网站:
sudo service nginx restart
中提琴 - 享受......
这里有一些更好的教程:
| 归档时间: |
|
| 查看次数: |
18625 次 |
| 最近记录: |