Nginx-密码保护不起作用 [英] Nginx - Password Protect Not Working

查看:296
本文介绍了Nginx-密码保护不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已按照说明进行操作,但仍然无法使用密码保护我的网站.这是我的app-nginx.config的样子:

I have followed instructions and still I cant password protect my site. This is what my app-nginx.config looks like:

server {
    listen       80;
    server_name  Server_Test;
    auth_basic            "Restricted";
    auth_basic_user_file  /usr/local/nginx/conf/htpasswd;

...

}

我要去哪里错了?我从教程站点复制并粘贴了此权限.

Where am I going wrong? I copied and pasted this right from a tutorial site.

推荐答案

仅使我的nginx服务器可以工作,甚至对其进行配置以保护我的根文件夹访问权限.我想与您分享我的发现,并在此过程中也为此问题提供一个很好且有效的答案.

Just made my nginx server to work, and even configured it to protect my root folder access. I'd like to share my findings with you and on the way also give a good and working answer to the question in this page.

作为nginx的新用户(版本1.10.0-Ubuntu). 我遇到的第一个问题是知道文件位置,所以这里是关键位置:

As a new user to nginx (Version 1.10.0 - Ubuntu). The first problem I've got was to know the file locations, so here are the critical locations:

了解您的位置:

主文件夹位置:/etc/nginx

默认站点位置:/var/www/甚至/ver/www/html/(html文件夹内将是 index.html 文件-希望您知道该怎么做.)

Default site location: /var/www/ or even /ver/www/html/ (inside the html folder will be the index.html file - hope you know what to do from there.)

配置文件:

主要配置文件:/etc/nginx/nginx.conf

当前站点服务器 conf:/etc/nginx/sites-enabled(在首次安装时,有一个名为default的文件,您需要使用sudo进行更改它(例如: sudo vi default)

Current site server conf: /etc/nginx/sites-enabled (upon first installation there is a single file there that is called default, and you'll need to use sudo to be able to change it (for example: sudo vi default)

添加密码:

因此,既然您已经知道了播放器(无论如何对于静态的开箱即用的站点),我们就将一些文件放在"html"文件夹中,并为其添加密码保护.

So, now that e know the players (for a static out-of-the-box site anyway) let's put some files in the 'html' folder and let's add password protection to it.

要设置密码,我们需要做两件事: 1.创建一个密码文件(具有我们想要的尽可能多的用户,但我将设置为1). 2.配置当前服务器(默认")以限制此页面,并使用1中的文件启用密码保护.

To setup a password we need to do 2 things: 1. create a passwords file (with as many users as we want, but I'll settle with 1). 2. Configure the current server ('default') to restrict this page and use the file in 1 to enable the password protection.

1.让我们创建一个密码:

我要为此使用的行是: sudo htpasswd -c /etc/nginx/.htpasswd john(将提示您输入并重新输入密码),您可以在此处单行执行: sudo htpasswd -c /etc/nginx/.htpasswd john [your password]

The line I'd like to use for this is: sudo htpasswd -c /etc/nginx/.htpasswd john (you'll get a prompt to enter and re-enter the password) of you can do it in a single line here: sudo htpasswd -c /etc/nginx/.htpasswd john [your password]

我将解释命令的每个部分:

I'll explain each part of the command:

  • sudo htpasswd-使用更高的权限进行操作.
  • -c-用于:创建文件(将其他用户添加到现有用户中,请跳过此参数)
  • /etc/nginx/.htpasswd-创建的文件的名称 (文件夹/etc/nginx中的".htpsswd")
  • john是用户名(在提示的用户"字段中输入)
  • password是此特定用户名所需的密码. (系统提示时.)
  • sudo htpasswd - do it using higher permission.
  • -c - for: create file (to add another user to an existing user skip this argument)
  • /etc/nginx/.htpasswd - the name of the file created ('.htpsswd' in the folder /etc/nginx)
  • john is the name of the user (to enter in the prompted 'user' field)
  • password is the needed password for this specific user name. (when prompted..)

通常htpasswd命令对您不起作用,因此您必须安装它的软件包:

Usually the htpasswd command won't work for you, so you'll have to install it's package:

使用:sudo apt-get install apache2-utils(如果失败,请尝试使用sudo apt-get update并重试)

Use: sudo apt-get install apache2-utils (if it fails try using sudo apt-get update and try again)

2.让我们将服务器配置为使用此文件进行身份验证

让我们使用此行来编辑当前(默认)服务器conf文件:

Let's use this line to edit the current (default) server conf file:

sudo vi /etc/nginx/sites-enabled/default(您不必使用'vi',但我喜欢.)

sudo vi /etc/nginx/sites-enabled/default (You don't have to use 'vi' but I like it..)

删除大部分注释(#)后,文件看起来像这样

The file looks like this after removing most of the comments (#)

# 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;
    }

}

我们需要在该位置的块内添加两行('/'指向站点的根文件夹),因此它看起来像这样:

We'll need to add two lines inside the block the location ('/' points to the root folder of the site) so it'll look like this:

    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)定义为此身份验证的密码文件.
  • auth_basic "Restricted Content"; - defines the type of access management
  • auth_basic_user_file /etc/nginx/.htpasswd; - defines the file we've created (/etc/nginx/.htppasswd) as the passwords file for this authentication.

让我们重新启动该服务并享受一个受密码保护的网站:

Let's restart the service and enjoy a password protected site:

sudo service nginx restart

中提琴-享受...

这里有一些很棒的教程:

Here are some more great tutorials for this:

查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆