0

I have this configuration :

location / {
    root /etc/nginx/euchet;
}

location /app/ {
    proxy_pass http://172.16.184.2:8080;
    proxy_redirect off;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header REMOTE_ADDR $remote_addr;
}

And I need to add another location with auth_basic enabled only for the request: /app/f?p=3000

I tried with rexexp:

   location ~ \/appt\/f\?p=3000 {
            auth_basic "Hello! Enter password";
            auth_basic_user_file /etc/nginx/euchet.htpasswd;

            proxy_pass http://172.16.184.2:8080;
            proxy_redirect off;

            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header REMOTE_ADDR $remote_addr;
    }

Nginx use previous location /app/.

I tried another way:

location ~* /euchet/f {

    if ($arg_p = "4550") {
        return 403;
    }

    error_page 403 = @hidden;
}


location @hidden {
            auth_basic "Hello! Enter password";
            auth_basic_user_file /etc/nginx/euchet.htpasswd;

            proxy_pass http://172.16.184.2:8080;
            proxy_redirect off;

            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header REMOTE_ADDR $remote_addr;
}

And I get 403 error on f?p=3000, and 404 errors on f?p=<another_number>

How can I fix this problem?

Xavier Lucas
  • 12,815
  • 2
  • 44
  • 50
  • 2
    Hello and welcome to ServerFault. We are not a free nginx's configuration delivery service and we expect users to make an effort of researching information (like reading [official documentations](http://nginx.org/en/docs/)) and spending time to try to solve their issues before posting a specific question. Please follow these advices, then update your post with attempted solutions. – Xavier Lucas Mar 17 '15 at 12:35
  • 1
    I've updated the post – Nikolay Yakubitskiy Mar 17 '15 at 15:23

1 Answers1

3

You better do this using a map as the auth_basic directive can contain variables.

map $request_uri $auth {
    default         "off";
    "/app/f?p=3000" "MyRealm";
}


server {

    location / {
        root /etc/nginx/euchet;
    }

    location /app/ {
        auth_basic $auth;
        auth_basic_user_file /etc/nginx/euchet.htpasswd;
        proxy_pass http://172.16.184.2:8080;
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header REMOTE_ADDR $remote_addr;
    }   

}
Xavier Lucas
  • 12,815
  • 2
  • 44
  • 50