OnToTheNextOne
OnToTheNextOne

Reputation: 75

Nginx Basic Auth not Working

I am trying to passwoard protect the default server in my Nginx config. However, no username/password dialog is shown when I visit the site. Nginx returns the content as usual. Here is the complete configuration:

worker_processes 1;

events
{
    multi_accept on;
}

http
{
    include       mime.types;
    sendfile           on;
    tcp_nopush         on;
    keepalive_timeout  30;
    tcp_nodelay        on;
    gzip  on;

    # Set path for Maxmind GeoLite database
    geoip_country /usr/share/GeoIP/GeoIP.dat;

    # Get the header set by the load balancer
    real_ip_header   X-Forwarded-For;
    set_real_ip_from 0.0.0.0/0;
    real_ip_recursive on;

    server {
        listen       80;
        server_name  sub.domain.com;

        auth_basic "Restricted";
        auth_basic_user_file /etc/nginx/htpasswd/sub.domain.com.htpasswd;

        expires -1;

        access_log /var/log/nginx/sub.domain.com.access default;
        error_log  /var/log/nginx/sub.domain.com.error debug;

        location / {
            return 200 '{hello}';
        }
    }
}

Interestingly, when I tried using an invalid file path as the value of auth_basic_user_file, the configtest still passes. This should not be the case.

Here's the Nginx and system info:

[root@ip nginx]# nginx -v
nginx version: nginx/1.8.0

[root@ip nginx]# uname -a
Linux 4.1.7-15.23.amzn1.x86_64 #1 SMP Mon Sep 14 23:20:33 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux

We are using the Nginx RPM available through yum.

Upvotes: 4

Views: 12092

Answers (3)

GChuf
GChuf

Reputation: 2230

In my case, adding the directives to /etc/nginx/sites-available/default worked, whereas adding the directives to /etc/nginx/nginx.conf did not.

Of course this only happens if you have this in your nginx.conf file:

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;

The config is simple (put it under location for specific part of your website, or under server for your whole website):

server {
        location /foo/ {
                auth_basic "This part of website is restricted";
                auth_basic_user_file /etc/apache2/.htpasswd;
        }
}

Upvotes: 0

Alexander Ites
Alexander Ites

Reputation: 490

Did you tried to reload/stop-and-start your nginx after basic auth was added to config? It is necessary to reload nginx with something like:

sudo -i service nginx reload

---- in order to make new settings work.

Also I would double check the URLs that are under your tests. (Once I tried to test Nginx Basic Auth in an Nginx proxy configuration accessing the actual URL of the resource that was behind the Nginx proxy and not the actual URL of Nginx.)


P.S.

Using an invalid file path as the value of auth_basic_user_file still doesn't cause the configtest to fail in 2018 as well. Here's my version of Nginx:

nginx version: nginx/1.10.2

Though an invalid file path causes Basic Auth check to be failed and results in:

403 Forbidden

---- HTTP response after credentials provided.

Upvotes: 0

bobbywebz
bobbywebz

Reputation: 63

You need to add auth_basic and auth_basic_user_file inside of your location block instead of the server block.

location / {
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/htpasswd/sub.domain.com.htpasswd;
return 200 '{hello}';

    }

Upvotes: 1

Related Questions