Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I have an nginx.conf that looks something like this:

server {
    server_name subdomain.example.com;

    listen 443 ssl;
    ssl_certificate      /srv/ssl/something.crt;
    ssl_certificate_key  /srv/ssl/something.key;

    access_log /var/log/something-nginx-access.log;
    error_log /var/log/something-nginx-error.log;

    location /private { try_files $uri @yourapplication; }
    location @yourapplication {
        include uwsgi_params;
        uwsgi_pass 127.0.0.1:3131;

        auth_basic "Restricted";
        auth_basic_user_file /home/ubuntu/something.htpassword;
    }

    location / { try_files $uri @yourapplication; }
    location @yourapplication {
        include uwsgi_params;
        uwsgi_pass 127.0.0.1:3131;
    }

    keepalive_timeout 70;
}

server {
    listen 80;
    server_name subdomain.example.com;

    access_log /var/log/something-nginx-access.log;
    error_log /var/log/something-nginx-error.log;

    location /private { try_files $uri @yourapplication; }
    location @yourapplication {
        include uwsgi_params;
        uwsgi_pass 127.0.0.1:3131;

        auth_basic "Restricted";
        auth_basic_user_file /home/ubuntu/something.htpassword;
    }

    location / { try_files $uri @yourapplication; }
    location @yourapplication {
        include uwsgi_params;
        uwsgi_pass 127.0.0.1:3131;
    }
}

That is to say -- I'm running my app over http and https, and forwarding all request to uWSGI, which is running a Flask application.

I wish to have a private path, /private/ where anything underneath requires HTTP AUTH. Any other paths should not require auth.

My problem is that nginx is asking for auth no matter where I make a request, e.g., '/', '/cool_page'.

How can I configure nginx correctly to only ask for authentication under /private?

Finally, the config seems to have a lot of repetition etc, is there a way of reducing my configuration down further?

Sorry, I'm new to nginx!

share|improve this question

1 Answer 1

Currently you have two named locations with the same name in one server section. This will cause nginx to always take the first one defined. To solve this rename one of the named locations. Name one @private and the other @public for example.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.