0

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:

http://img.website.com/bob.jpg

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

http://username:password@domain:80/bob.jpg

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?

jnbdz
  • 897
  • 5
  • 22
  • 43

1 Answers1

2

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:

Fleshgrinder
  • 3,638
  • 2
  • 16
  • 19