【发布时间】:2021-09-03 05:55:54
【问题描述】:
我想在 Apache 反向代理后面运行 Symfony 5.3 应用程序,但它没有按预期工作
我在 Apache 反向代理后面有许多 Web 项目 Apache 服务器,没有任何问题,但是这个让我不满意:-/
场景:
| Setup | Browser | Proxy-Server | App-Server |
|---|---|---|---|
| Setup: | http://host/app1/login |
ProxyPass /app1/ http://192.168.1.1/ProxyPassReverse /app1/ http://192.168.1.1/
|
http://192.168.1.1/login |
我希望设置做什么:
| Flow | Browser | Proxy-Server | App-Server |
|---|---|---|---|
| Browser > Proxy > App-Server | Browser requests url (e.g. post credentials) /app1/login
|
Proxy maps request to app /login
|
Server does stuff in /login controller (and redirects to /success see next figure) |
| Flow | App-Server | Proxy-Server | Browser |
|---|---|---|---|
| App-Server > Proxy > Browser | App-Server sends /success redirect |
Proxy maps response to /app1/success
|
Receives redirect pointing to /app1/success
|
现实生活中会发生什么:
| Flow | Browser | Proxy-Server | App-Server |
|---|---|---|---|
| Browser > Proxy > App-Server | Browser requests url (e.g. post credentials) /app1/login
|
Proxy maps request to app /login
|
Server does stuff in /login controller (and redirects to /success see next figure) |
| Flow | App-Server | Proxy-Server | Browser |
|---|---|---|---|
| App-Server > Proxy > Browser | App-Server sends /success redirect |
### ERROR ### ### ERROR ### ### ERROR ### Proxy somehow NOT rewriting to /app1/success but instead goes for /success
|
Receives redirect pointing to /successand calls http://host/success and goes 404 (or worse stuff) |
我想要什么
浏览器: 嘿,我将凭据发布到 /app1/login
代理: 呃,一会儿……必须是 /login - 否则服务器不会知道
服务器:哟,/login 看起来不错,转到 /success,哦,然后加载 /this/css.file
代理: 错误,只是一个第二个 .. 必须是 /app1/success aaand /app1/this/css.file - 否则浏览器会感到困惑
浏览器: 是的,我要去 /app1/success
浏览器:哇,/app1/this/css.file 中的设计真好
大家开心!
让事情变得更糟
- 我只有网络/服务器的权力,而不是 Symfony 应用程序本身
- 所以,我不能更改应用程序代码/配置本身,我只需要“托管”这个东西
我已经阅读(字面上)数百篇关于这个/类似问题的 Stack 帖子。
我已经阅读了有关 Apache、ModProxy、Symfony 的“所有”文档,并在 Google 上呆了几天。
我现在运行这个程序的希望很低,我的大脑很痛。
问题
- 您还需要哪些额外信息来解决我的问题
- 我错过了什么 - 到底 - (可能很明显隐藏在显而易见的地方)配置的东西??
- 即使解决方案在 Symfony 应用程序中并且维护人员必须修复它(
附加信息
已安装服务器:
- Ubuntu 20.04
- Apache 2.4
- PHP 7.4 (FPM)
服务器/应用配置:
- vHost Proxy-Server(相关部分)
ProxyRequests Off
ProxyVia Off
ProxyPreserveHost On
ProxyTimeout 1200
ProxyReceiveBufferSize 4096
<VirtualHost *:80>
ProxyPass /app1/ http://192.168.1.1/
ProxyPassReverse /app1/ http://192.168.1.1/
</VirtualHost>
- vHost App-Server(相关部分)
<VirtualHost *:80>
DocumentRoot /var/www/public
<Directory /var/www/public>
AllowOverride All
</Directory>
</VirtualHost>
- htaccess App-Server(重写部分)
# Determine the RewriteBase automatically and set it as environment variable.
# If you are using Apache aliases to do mass virtual hosting or installed the
# project in a subdirectory, the base path will be prepended to allow proper
# resolution of the index.php file and to redirect to the correct URI. It will
# work in environments without path prefix as well, providing a safe, one-size
# fits all solution. But as you do not need it in this case, you can comment
# the following 2 lines to eliminate the overhead.
RewriteCond %{REQUEST_URI}::$0 ^(/.+)/(.*)::\2$
RewriteRule .* - [E=BASE:%1]
# Sets the HTTP_AUTHORIZATION header removed by Apache
RewriteCond %{HTTP:Authorization} .+
RewriteRule ^ - [E=HTTP_AUTHORIZATION:%0]
# Redirect to URI without front controller to prevent duplicate content
# (with and without `/index.php`). Only do this redirect on the initial
# rewrite by Apache and not on subsequent cycles. Otherwise we would get an
# endless redirect loop (request -> rewrite to front controller ->
# redirect -> request -> ...).
# So in case you get a "too many redirects" error or you always get redirected
# to the start page because your Apache does not expose the REDIRECT_STATUS
# environment variable, you have 2 choices:
# - disable this feature by commenting the following 2 lines or
# - use Apache >= 2.3.9 and replace all L flags by END flags and remove the
# following RewriteCond (best solution)
RewriteCond %{ENV:REDIRECT_STATUS} =""
RewriteRule ^index\.php(?:/(.*)|$) %{ENV:BASE}/$1 [R=301,L]
# If the requested filename exists, simply serve it.
# We only want to let Apache serve files and not directories.
# Rewrite all other queries to the front controller.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ %{ENV:BASE}/index.php [L]
【问题讨论】:
-
你试过在你的 apache conf 上使用 ProxyPreserveHost On 吗?
-
是的,如“附加信息”、“服务器/应用程序配置”中所述;)
标签: url-rewriting apache2 reverse-proxy symfony5