简体   繁体   中英

Injecting reverse proxy HTTP headers into Express.js app

My Express.js app uses absolute paths (eg, /bootstrap/css/bootstrap.css , etc.) When I reverse proxy this, in Apache, at the domain root, it works fine. However, if I RP it at any different path, the app's paths don't resolve. For example, if I RP at /app then the app will attempt to fetch /bootstrap/css/bootstrap.css , whereas it should be fetching /app/bootstrap/css/bootstrap.css , etc.

As an attempt to resolve this, I have my Apache configuration setting the X-Script-Name header to /app :

Header add "X-Script-Name" /app

I hope to then inject this into the frontend code as it's served by Express. For example, instead of:

<script src='/assets/something.js'></script>

...we would have something akin to:

<script src='${X-Script-Name}/assets/something.js'></script>

The problem I'm having is getting the X-Script-Name value in Express. I had assumed it would be in the router's req.headers , but this is just the headers being pushed through by Express... How do I get this to work?

Using a plain Header will set a response header.

You need to add a request header so your downstream app can access it, therefore you need to use the RequestHeader directive like so:

RequestHeader set X-Script-Name "/app"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM