Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How can I setup an nginx proxy_pass directive that will also include HTTP Basic authentication information sent to the proxy host?

This is an example of the URL I need to proxy to:


The end goal is to allow 1 server present files from another server (the one we're proxying to) without exposing the URI of the proxy server. I have this working 90% correct now from following the Nginx config found here:

I just need to add in the HTTP Basic authentication to send to the proxy server

share|improve this question
@all: Be sure you need HTTP Basic authentication when using this solution - not HTTP Digest Authentication ;) Had quite a hard time debugging around until I figured it out ...… – SimonSimCity Jun 6 '13 at 9:27

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

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_set_header Authorization "Basic a2luZzppc25ha2Vk";

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


Feel free to check out blog post for more details.

share|improve this answer
The link is broken – Alex Jul 26 '13 at 6:30
Link is now here: in case anyone is wondering – ckm Oct 22 '14 at 22:25
I need something more difficult – Ilja Jul 4 '15 at 0:02
Your solution is not flexible enough. It could be very useful to encode username:password on the fly. First, nginx must parse username:password from URL, secondly, nginx must encode this data and set in appropriate header. I don't want to hardcode encoded credentials. – Johnny Aug 26 '15 at 16:42
I get "bad request" when trying it on my setup.. any idea? – Spock Oct 9 '15 at 21:45
up vote 11 down vote accepted

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==";
share|improve this answer
Do you know how to encode username:password on the fly with nginx? Hardcoded credentials is not flexible, because I want to authenticate user with credentials specified by him in URL. – Johnny Aug 26 '15 at 16:46
I've found how to encode to base64 with nginx This is more useful than hardcoded credentials. – Johnny Aug 26 '15 at 16:53

Set proxy_set_header Authorization "USER_AND_PASS", where USER_AND_PASS = base64(user:pass)

share|improve this answer

Your Answer


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.