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

Is it possible to reject all nginx request, which don't send basic authentication data with them?

I have requests like this:

xxx.xxx.xxx.xxx - - [24/Oct/2012:10:32:12 +0200] "POST /path HTTP/1.1" 401 1387 "-" "Apache-HttpClient/4.1.1 (java 1.5)" "-"
xxx.xxx.xxx.xxx - paul [24/Oct/2012:10:32:12 +0200] "POST /path HTTP/1.1" 200 192 "-" "Apache-HttpClient/4.1.1 (java 1.5)" "-"

and i want reject the first one, which don't provide the user. As you can see it already sends the 401 HTTP code, but this code is generated not directly from nginx but from an underlying application server. What I want to do is to directly deny it in nginx. Is there a way to check if a user has been provided?

And is this possible without setting up normal basic authentication directly in nginx?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

Nginx stores the username derived from authentication in $remote_user. You should be able to test that it is not empty, and accordingly perform the desired action (e.g. return a 401 error). Something like:

if ($remote_user = ''){
    return 401;
}

By the looks of it, you are already populating the $remote_user variable (since it shows up in your logs) - as far as I know, you must use Nginx's HTTP Auth Basic Module to populate that variable.

Basic authentication sets the Authorization header. In Nginx, the HTTP request headers are accessible through $http_HEADER, so in this case $http_authorization. If you implement basic authentication outside of Nginx and set that header, you can test that the header is not empty. (Alternatively, depending on how you implement authentication, you could set a custom header that you test).

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.