Nginx auth_basic 不适用于特定网址

Mil*_*loš 0 authentication nginx

我想用密码保护我拥有的 URL 之一,我正在尝试使用:

 location /about/payment {
    auth_basic           "secured site";
    auth_basic_user_file /var/www/my.passwd;
}
Run Code Online (Sandbox Code Playgroud)

问题是我被要求提供用户名和 paasword。一旦我输入了正确的用户名和密码,我就会收到此日志的 404 错误:

*55268 open() "/var/www/mysite.com/deployment/web/about/payment" failed (2: No such file or directory), client: 172.16.0.53, server: ~^(?<branch>\w+)\.mysite\.dev$, request: "GET /about/payment HTTP/1.1", host: "deployment.mysite.dev"
Run Code Online (Sandbox Code Playgroud)

编辑:

整个 nginx conf 文件在这里

server {
  listen 80;

  access_log              ...;
  error_log               ...;
  server_name ~^(?<branch>\w+)\.mysite\.dev$  ~^(?<branch>\w+)\.mysite\.com$;
  root /var/www/git/branches/mysite.com/$branch/web;

  location /about/payment {
    auth_basic           "secured site";
    auth_basic_user_file /var/www/mysite.passwd;
  }

  # strip app_eudev.php/ prefix if it is present
  rewrite ^/app_eudev\.php/?(.*)$ /$1 permanent;

  # remove trailing slash
  rewrite ^/(.*)/$ /$1 permanent;

  # sitemap rewrite
  rewrite ^/sitemap_(.*)$ /sitemap/$1 last;

  location / {
     try_files $uri @symfonyapp;
  }

  location @symfonyapp {
    rewrite ^(.*)$ /app_eudev.php/$1 last;
  }

  location /var/www/dms/ {
      internal;
      alias /var/www/dms/;
  }

  location @htmlimages {
      root /var/www/dms/;
  }

  location ~ /html/.*\.(png|gif|jpg|pdf)$ {
    root ...;
    try_files $uri @htmlimages;
  }

  location /files {
    root ...;
  }

  location /assets {
    root ...;
  }

  location /img {
     root ...
  }

  location ~ \.php(/|$) {
  fastcgi_pass                    127.0.0.1:9001;
    fastcgi_split_path_info ^(.+\.php)(/.*)$;
    include fastcgi_params;
    fastcgi_param  SCRIPT_FILENAME         $document_root$fastcgi_script_name;
    fastcgi_param  HTTPS              off;
  }
 }
Run Code Online (Sandbox Code Playgroud)

Ale*_*Ten 5

当 nginx 找到location将处理请求的匹配项时,它会忽略任何其他location可能匹配请求的内容。

在您添加身份验证之前的情况下,请求/about/payment被处理location /,最终将请求传递给 PHP。但是,只要您location /about/payment向该 URL添加请求,该位置就会处理该位置,该位置没有特殊指令,因此 nginx 将尝试提供静态文件。

您应该添加将请求传递给 PHP 的指令,在这种情况下,它非常简单:

location /about/payment {
    auth_basic           "secured site";
    auth_basic_user_file /var/www/my.passwd;

    root ...;
    try_files $uri @symfonyapp;
}
Run Code Online (Sandbox Code Playgroud)