0

I have nginx installed on my centos server. I have blocked all external access to a test website I am working on. With this code.

location / {
    auth_basic "Administrator Login";
    auth_basic_user_file /home/config-files/.htpasswd;
}

The problem is it's not blocking access to static files. I can still access files like this.

wp-admin/images/icons32-vs-2x.png
wp-admin/css/colors/midnight/colors.min.css
wp-content/themes/testtheme/style.css

Why Nginx is not blocing access to these files.

EDIT Here is full virtual host file.

server {

    listen 80;
    server_name www.domainname.com;

    root /home/myuser/domainname.com/public;
    index index.html index.php;

    access_log /home/myuser/domainname.com/logs/access.log;
    error_log /home/myuser/domainname.com/logs/error.log;

    location ~ /\.svn/* {
        deny all;
    }

    location ~ \.(htaccess|htpasswd) {
        deny all;
    }

    location ~ \.conf$ {
        deny all;
    }

    location / {
        try_files $uri $uri/ /index.php?$args;
        auth_basic "Administrator Login";
        auth_basic_user_file /home/config-files/.htpasswd;
    }

    location ~ \.(css|js) {
        rewrite ^(.*/?)([a-z]+)-([0-9]+)\.(css|js)$ /$1$2.$4 last;
    }

    rewrite /wp-admin$ $scheme://$host$uri/ permanent;

    location ~* \.(js|css)$ {
        expires 30d;
        log_not_found off;
    }

    location ~* \.(?:ico|gif|jpe?g|png|svg)$ {
        expires 180d;
        add_header Pragma public;
        add_header Cache-Control "public";
        log_not_found off;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$; #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
        include fastcgi_params;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass 127.0.0.1:9000;
    }

    rewrite ^(.*)/undefined$ /$1 permanent;
    rewrite ^(.*)/undefined/$ /$1 permanent;

}
2
  • please post full nginx virtual server configuration.
    – Navern
    Sep 9, 2014 at 7:58
  • Hi I just added it in question. See edits.
    – Robert hue
    Sep 9, 2014 at 8:04

1 Answer 1

1

nginx is matching 'location' sections not by order but by most-specific, read here. It is then applying only the best-matching location section to the request, and none of the others. Therefore, "location /" will only act on requests that are not matched by any of the other location sections (all of them being more specific).

Try to put the auth directives outside any location bracket, straight into server{}.

7
  • Hi Nils, it's working but I am not sure why location / did not worked. Should I use location \* or something like that. I want to know because I will be using it on many folders.
    – Robert hue
    Sep 9, 2014 at 8:47
  • Let me try again. For each URI, nginx tries to find the best matching 'location' section. ONLY THAT location section is then applied to the request, NONE of the other (less specific) location sections. Hence "location *" would not help: it is still less specific; and you still assume you could apply more that one location section to the same request. If you want a directive to work for all location brackets, put it outside location, straight into server{} Sep 9, 2014 at 8:58
  • So that means if I apply some rule on location /folder/ then they might now work for location /folder/subfolder/. Right?
    – Robert hue
    Sep 9, 2014 at 9:01
  • If you have location /folder/ { A }; only, then A is applied to requests for /folder/ or /folder/subfolder/. If you have location /folder/ { A }; location /folder/subfolder/ { B }; then A is NOT applied to a request for /folder/subfolder/, only B is applied. Sep 9, 2014 at 9:06
  • Oh that makes sense. Thanks for explaining. I am used to work with apache/htaccess and I was expecting it to work like that. :)
    – Robert hue
    Sep 9, 2014 at 9:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .