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

问题描述 投票:0回答:1

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

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

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

这是我对该应用的完整配置。但是我隐藏了其他配置详细信息(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;
      } 
}

[当我尝试访问受保护的页面时,出现以下提示:Login Popup

当我点击错误的凭据时,我没有得到预期的请求页面。

因此,当我点击期望的凭证时,我会得到一个404404 Error

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

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

nginx next.js basic-authentication nginx-location nginx-config
1个回答
0
投票

尝试这个?

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;
    } 
}
© www.soinside.com 2019 - 2024. All rights reserved.