Milos Cuculovic
Milos Cuculovic

Reputation: 20223

Nginx auth_basic not working for a specific url

I would like to password protect one of the URLs I have and I am trying to do it with:

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

The problem is that I am asked for the username and paasword. AS soon as I put the right username and password, I am getting a 404 Error with this log:

*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"

EDIT:

The entire nginx conf file is here

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

Upvotes: 0

Views: 5929

Answers (2)

Mike Nguyen
Mike Nguyen

Reputation: 1053

E.g. for Wordpress site, I wanna lock the 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!
}

Upvotes: 0

Alexey Ten
Alexey Ten

Reputation: 14344

When nginx find matching location that will process request it ignores any other location that could possibly match request.

In your case before you add auth, request to /about/payment was proceeded by location / which finally passed request to PHP. But as soon as you add location /about/payment request to that URL will be processed by this location which has no special directives so nginx will try to serve static files.

You should add directives that pass request to PHP, in this case it's really simple:

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

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

Upvotes: 5

Related Questions