Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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

I have a server that is password protected, where I store files that I want my users to be able to access only if they have the rights.

In the NGiNX configs I want to be able to do something like this:

proxy_pass http://username:password@domain:80/

So that a user could go to this address:

And then in the server the configs will point to this address:


Also is there a way to call a script (best would be PHP) to see if the person accessing the file as been logged in.

My sessions are saved on another server (on a Redis DB) so if it is not possible to do it with PHP is there another way?

So how do I configure this?

share|improve this question
up vote 2 down vote accepted

Please check out the following two links for your first question about HTTP Authentication with nginx in proxy context:

And here's a basic 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://domain:80;
  proxy_set_header Authorization "Basic dXNlcm5hbWU6cGFzc3dvcmQ=";

Where dXNlcm5hbWU6cGFzc3dvcmQ= is username:password encoded in Base64.

Regarding your second question. Yes, it's easily possible to check for an existing HTTP Authentication session with PHP. Please head over to the official PHP documentation for this feature. There you'll find many useful examples that help you getting started:

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.