Search code examples
phpnode.jsapachehttpshttp-proxy

php behind nodejs proxy server without ssl


I have a node js app that runs on "https" using port 443. this app acts as a reverse proxy server if the URL path starts with "/blog". all the requests with URL starting with "/blog" are forwarded to apache without using an SSL connection. But the browser uses the SSL connection to connect with Nodejs. As the apache receives the requests with "http://" URL scheme the PHP's $_SERVER['https'] is not set to "on". which results in all the URLs for the static resources like CSS, javascript files start with "http://".

Therefore browser gives the error that the page is loaded over "https". But requesting a resource from "http://". which results in blocking of all the CSS and javascript files.

Now I am looking a way to somehow fool the PHP that the request is from "https" instead of "http". I know I can just edit the code and place $_SERVER['https'] = 'on'; at the beginning of each script. But it doesn't appear to be an elegant way to solve the problem. I would like to know about any solution that works per directory. Just like an "htaccess" file.


Solution

  • Make sure that your reverse proxy server sets the X-Forwarded headers. Then in your apache document root create an .htaccess file and place the following code in it.

    SetEnvIf X-Forwarded-Proto https HTTPS
    

    This will be only apply to the directory where this file is placed. Therefore instead of placing this file in document root you can place it where you want to set the url scheme to "https".