使用基本身份验证登录后 Nginx [=10th=] 404

Nginx returns 404 after loggin in with basic authentication

我用 next.js 创建了一个网站,在我自己的测试服务器上部署后,一切正常 expected.As 有管理区域,我做了一个配置来保护 /admin url 使用 nginx 基础 auth.Here 是我的步骤:

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

    中做了如下配置

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

这是我的完整配置 app.But 我隐藏了其他配置细节(TLS、http 重定向等):

 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

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

我也尝试了其他解决方案,即使它们没有被标记为解决问题。

试试这个?

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