Nginx密码认证一直提示输入密码

Nginx password authentication keeps prompting for password

我想上传我网站的开发分支,以便我可以将其展示给客户并在尽可能接近生产的环境中进行测试(使用可能尚未准备好用于生产的代码)。因此我想用密码保护这个网站。

我正在使用 Django 开发网站并使用 nginx 为网站提供服务(使用 uWsgi)。我设法收到应用以下指令的密码提示:

auth_basic "Restricted Content";  # also tried "Private Property"
auth_basic_user_file /etc/nginx/.htpasswd;

但问题是正确输入第一个密码后,它一直提示我再次输入用户名和密码;好像每个 API 调用都需要进行身份验证。

我认为问题可能出在我的配置文件上,所以这是我的 site.conf 文件:

server {
    listen 80;
    server_name panel.mysite.dev;
    root /path/to/my/app/front/dist;

    ### I've also tried 'auth_basic' here

    location / {

        root /path/to/my/app/front/dist;
        index index.html;

        auth_basic "Private Property";
        auth_basic_user_file /etc/nginx/.htpasswd;
    }
    location /media {
        rewrite ^(.*)$ http://media.mysite.dev;
    }
    location /static {
        rewrite ^(.*)$ http://static.mysite.dev;
    }

}

server {
    listen 80;
    server_name api.mysite.dev;

    ### I've also tried 'auth_basic' here

    location /api {
        client_max_body_size 25m;
        uwsgi_pass unix:/tmp/api.mysite.dev.sock;
        include /path/to/my/app/back/uwsgi_params;
    }

}
server {
    listen 80;
    server_name media.mysite.dev;
    root /path/to/my/app/media;
    add_header 'Access-Control-Allow-Origin' '.*\.mysite\.[com|dev]';

    location / {
        root /path/to/my/app/media;
    }
}
server {
    listen 80;
    server_name static.mysite.dev;
    root /path/to/my/app/static;
    if ($http_origin ~* (https?://.*\.mysite\.[com|dev](:[0-9]+)?)) {
        set $cors "true";
    }
    location / {
        if ($cors = "true") {
            add_header 'Access-Control-Allow-Origin' "$http_origin";
        }
    }
}

我的问题:有什么方法可以记住输入的密码并允许经过身份验证的用户轻松导航?还是我遗漏了一些微不足道的东西?

编辑: 在我的 Django settings.py:

AUTHENTICATION_BACKENDS = (
    'oauth2_provider.backends.OAuth2Backend',
    'django.contrib.auth.backends.ModelBackend',
    'allauth.account.auth_backends.AuthenticationBackend',
)
...
REST_FRAMEWORK = {
    ...
    DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.TokenAuthentication',
        'oauth2_provider.ext.rest_framework.OAuth2Authentication',
    ),

非常感谢您。任何帮助将不胜感激

基本身份验证使用Authorization header 传输用户名和密码。 Django REST also uses this header in the TokenAuthentication authentication backend. Nginx does not support multiple Authorization headers,因此如果您尝试同时登录和使用 Token 身份验证,事情就会崩溃。

无需更改 Django 应用程序的解决方案是在 nginx 中使用另一种身份验证方法,例如,client certificates, or, you can use the ngx_http_auth_request_module 检查已签名的 session cookie 是否为 set/valid 或如果请求 IP 在(临时)白名单中,否则将用户重定向到带有登录表单的页面。

使用 Nginx 进行基本身份验证有一种出色、简单且更安全的方法。它是,使用 oAuth2_proxy 具有基本身份验证支持,包括 Web 表单(非常好,浏览器不会保持连接有效)来输入 user/password 凭据,并可选择包含密码文件,如 htpasswd。它还支持许多其他身份提供者。 这是我的 oAuth2_proxy 基本身份验证配置文件:

http_address = "0.0.0.0:5160" #use any port you want

upstreams = [
    "https://url-that-you-want-to-protect/"
]

request_logging = false
custom_sign_in_logo = "/etc/apache2/small-email-icon.png" #custom logo on form
authenticated_emails_file = "/etc/apache2/emails" #required option, file can be empty

htpasswd_file = "/etc/apache2/.htpasswd" #basic auth password file
display_htpasswd_form = true

client_id = "hfytr76r7686887"
client_secret = "ghgh6767ghgh7654fghj6543dfgh5432"
cookie_name = "_oauth2_proxy"
cookie_secret = "ghgh6gguujgh7654fghj6543dfgh5432"
cookie_expire = "2h15m0s"
# cookie_refresh = ""
cookie_secure = true
footer = "Unauthorized access prohibited."