Nginx使用基本身份验证登录后返回404

时间:2020-05-01 10:43:48

标签: nginx next.js basic-authentication nginx-location nginx-config

我已经用next.js创建了一个网站,并将其部署在自己的测试服务器中后,一切都按预期进行。由于存在admin区域,因此我进行了配置以保护/admin {{1} }使用nginx基本认证。这是我的步骤:

  1. 首先我在url上使用apache2-utils安装了sudo apt-get install apache2-utils
  2. 我通过运行Ubuntu 18.04创建了凭据。单击htpasswd /etc/nginx/.htpasswd myusername之后,我提供了enter
  3. 我尝试查看文件内容,并且一切都很好,因为我看过password
  4. 我在usename:hashedpassword

    中进行了以下配置

    nginx

这是我对该应用的完整配置。但是我隐藏了其他配置详细信息(TLS,http重定向等):

location /admin {
auth_basic "Zone protege";
auth_basic_user_file /etc/nginx/.htpasswd;
}

当我尝试访问受保护的页面时,我得到了: Login Popup

当我点击错误的凭据时,无法获得预期的请求页面。

因此,当我点击所需的凭据时,会得到 server { server_name mydomainename.com; location / { proxy_pass http://localhost:3000; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; } location /admin { auth_basic "Zone protege"; auth_basic_user_file /etc/nginx/.htpasswd; try_files $uri $uri/ =404; } } 404 Error

我已经阅读了以下解决方案: NGINX auth_basic is giving me a '404 not found' message ... NGINX htpasswd 404 not found

尝试过的其他解决方案也没有被标记为解决问题。

1 个答案:

答案 0 :(得分:1)

尝试这个?

server {
    server_name mydomainename.com;
    auth_basic "Zone protege";
    auth_basic_user_file /etc/nginx/.htpasswd;

    location / {
        auth_basic off;

        proxy_pass http://localhost:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }

    location /admin {
        #auth_basic on;

        proxy_pass http://localhost:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    } 
}