How to use nginx to proxy to a host requiring authentication?

I did a writeup on this a while ago. See the details here:

http://shairosenfeld.blogspot.com/2011/03/authorization-header-in-nginx-for.html

For example:

 location / {
    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_pass http://6.6.6.6:80;
    proxy_set_header Authorization "Basic a2luZzppc25ha2Vk";
 }

"a2luZzppc25ha2Vk" is "king:isnaked" base64 encoded, so that would work for

http://king:[email protected]

Feel free to check out blog post for more details.


I got this working with alvosu's answer but I had to enter the word "Basic" inside the quotation of the base64 string so it looked like this:

proxy_set_header Authorization "Basic dGVzdHN0cmluZw==";

Set

proxy_set_header Authorization "USER_AND_PASS"

where USER_AND_PASS = base64(user:pass).


Remove the authorization header that gets passed forwarded by nginx with proxy_set_header Authorization "";.

I configured nginx to do basic auth but the Authorization header was getting passed along in the proxy_pass directive and the receiving end couldn't handle the token.

# Basic Auth
auth_basic "Private Stuff";
auth_basic_user_file /etc/nginx/.htpasswd;

location /server {
    proxy_pass http://172.31.31.140:9090;
    proxy_set_header Authorization "";
}

(Specific to my case, this error was returned Reason: No AuthenticationProvider found for org.springframework.security.authentication.UsernamePasswordAuthenticationToken)