Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Urls of a symfony app running behind a reverse proxy

I will try to explain the scenario the best i can, since it seems a little complicated.

I am building this web app for a big organization that have a whole lot of other web apps running in their servers. When I finally finished the development stage, they asked me to send them a virtual machine with all the stuff configured and ready to run (the web application, apache, the database, etc.), and, by some sort of magic that is unknown to me (they talked about a reverse proxy), they would make it accesible to the world.

So I sent them the machine and they put it online on http://www.fakedomain.com/fakedirectory (mind the /fakedirectory part)

Now, this indeed redirects external requests to the webserver running in my virtual machine. The problem is that my symfony app is generating all my urls relative to the web server in the virtual machine, which runs on the root ("/"). But, the actual url that users are accessing is /fakedirectory. So, for example, this is what you get when you access /fakedirectory/doc.html:

<html>
...
<body>
    ...
    <a href="/anotherdoc.html">A link</a>
</body>
</html>

If a user hits "A link" it would lead him to a 404 error, cause the actual url should be /fakedirectory/anotherdoc.html

As I see it, this should be addressed in some way from the said reverse proxy that is making the communication with the virtual machine. But an IT from my client's organization has suggested that it would be easier if my symfony app just spits the urls right.

So, before i (probably mistakenly) ask the IT to do something in their proxy, is there a way i can address this in my virtual machine? I have read on symfonys doc about the request contexts , specifically this:

# app/config/parameters.yml
parameters:
    router.request_context.host: www.fakedomain.com
    router.request_context.scheme: http
    router.request_context.base_url: fakedirectory

But this doesn't seem to apply globally to the whole symfony's url system. I also tried some weird configurations in apache's mod_proxy and mod_rewrite with no luck.

So, to put it in short, i am absolutely clueless, so any thoughts would be appreciated. Thanks in advance.

like image 632
Kilian Perdomo Curbelo Avatar asked Sep 28 '22 22:09

Kilian Perdomo Curbelo


2 Answers

I ran into similar problem myself. In this case url Rewrites won't help because symfony gets base path from $_SERVER variables based on app.php script location. REQUEST_URI will be /fakedirectory/ but SCRIPT_NAME will be /app.php so base path becomes / and fakedirectory is treated as symfony router path.

SOLUTION:

Make a fakedirectory symlink. Inside web directory run ln -s . fakedirectory.

Now with REQUEST_URI as /fakedirectory/ you will have SCRITP_NAME value equal to /fakedirectory/app.php and all asset or router paths will have correct /fakedirectory/ base path.

like image 115
matval Avatar answered Oct 01 '22 03:10

matval


I think you can solve this with a RewriteBase rule?

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /fakedirectory/web
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^(.*)$ app.php [QSA,L] 
</IfModule>
like image 32
jhuiting Avatar answered Oct 01 '22 05:10

jhuiting