HTTP Authentication with Nginx reverse Proxy + PHP-FPM
I tried to create a simple HTTP Authentication script with php but i had the problem that the two $_SERVER variables PHP_AUTH_USER and PHP_AUTH_PW didn’t exist in the first place. If you read the php documentation everyone said that this works great if you run php as an apache module 🙂
Running php as FastCGI Process Manager i had to make some changes to see the two variables.
What made things worse is that i run apache under a nginx server that i use as remote proxy. So were the authorization variables striped from ngixn or something had i configured wrong?
The first thing that i found was a simple line of apache configuration that i could add in my .htaccess in order to stop the header striping:
SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
This has brought back the variables but they were still empty. So i had to search about nginx. How could i send the two headers over the proxy? Well i had to add two lines on the nginx configuration file under location in order to succeed this.
proxy_set_header Authorization $http_authorization;
proxy_pass_header Authorization;
This has done the trick and now the data are traveling directly to my script.