Nginx auth_basic不适用于特定网址

时间:2015-02-12 15:24:21

标签: authentication nginx

我想用密码保护我拥有的其中一个网址,我正在尝试使用以下网址:

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

问题是我被问到用户名和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"

编辑:

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

2 个答案:

答案 0 :(得分:4)

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

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

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

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

    root ...;
    try_files $uri @symfonyapp;
}

答案 1 :(得分:0)

例如对于Wordpress网站,我想锁定URL

location ~* /block-url/ {
    auth_basic           "Internal Staging Site";
    auth_basic_user_file /etc/nginx/.htpasswd;
    try_files $uri $uri/ /index.php?$args; # Most important!
}