Laravel 使用反向代理加载站点时路由不起作用
Laravel routes not working when site is loaded using reverse proxy
我在我的项目中使用反向代理将路径重定向到不同的服务器。
站点 1:abc.com(服务器 1)
站点 2:内容存储在从服务器 1 - testsite 的子域映射的不同服务器中。abc.com(服务器 2)
服务器 2 中的站点是一个 laravel 项目。服务器 1 中的站点是普通 html 站点。
使用反向代理将 "abc.com/new-url/" 重定向到 "testsite.abc.com/" 的 apache 配置是:
<VirtualHost *:80>
ServerName abc.com
ServerAlias www.abc.com
DocumentRoot /var/www/vhosts/abc-livesite
<Directory /var/www/vhosts/abc-livesite>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
</Directory>
ProxyPreserveHost On
<Location "/new-url/">
ProxyPass "http://testsite.abc.com/"
</Location>
</VirtualHost>
如果不使用 index.php,laravel 路由不适用于 "abc.com/new-url/"。但是 laravel 路由在 "testsite.abc.com/" 上运行良好。
Example:
The route - "testsite.abc.com/test-route" is working fine.
The route - "abc.com/new-url/test-route" is showing 404 not found
error. But "abc.com/new-url/index.php/test-route" is working fine.
我认为,这里覆盖 index.php 的重写规则在使用反向代理时不起作用。
站点 2 的 .htaccess 文件是:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ / [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
导致此问题的原因是什么?有人可以帮忙吗?
这是解决此问题的解决方法。
更新反向代理规则以首先解析静态路径,
# Add static path
ProxyPass /assets/js http://example.com/js
ProxyPassReverse /assets/js http://example.com/js
ProxyPass /assets/css http://example.com/css
ProxyPassReverse /assets/css http://example.com/css
# Resolve index.php
ProxyPass /path http://example.com/index.php
ProxyPassReverse /path http://example.com/index.php
这将按照优先顺序工作。
希望对您有所帮助。
我在我的项目中使用反向代理将路径重定向到不同的服务器。
站点 1:abc.com(服务器 1)
站点 2:内容存储在从服务器 1 - testsite 的子域映射的不同服务器中。abc.com(服务器 2)
服务器 2 中的站点是一个 laravel 项目。服务器 1 中的站点是普通 html 站点。
使用反向代理将 "abc.com/new-url/" 重定向到 "testsite.abc.com/" 的 apache 配置是:
<VirtualHost *:80>
ServerName abc.com
ServerAlias www.abc.com
DocumentRoot /var/www/vhosts/abc-livesite
<Directory /var/www/vhosts/abc-livesite>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
</Directory>
ProxyPreserveHost On
<Location "/new-url/">
ProxyPass "http://testsite.abc.com/"
</Location>
</VirtualHost>
如果不使用 index.php,laravel 路由不适用于 "abc.com/new-url/"。但是 laravel 路由在 "testsite.abc.com/" 上运行良好。
Example:
The route - "testsite.abc.com/test-route" is working fine.
The route - "abc.com/new-url/test-route" is showing 404 not found error. But "abc.com/new-url/index.php/test-route" is working fine.
我认为,这里覆盖 index.php 的重写规则在使用反向代理时不起作用。
站点 2 的 .htaccess 文件是:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ / [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
导致此问题的原因是什么?有人可以帮忙吗?
这是解决此问题的解决方法。
更新反向代理规则以首先解析静态路径,
# Add static path
ProxyPass /assets/js http://example.com/js
ProxyPassReverse /assets/js http://example.com/js
ProxyPass /assets/css http://example.com/css
ProxyPassReverse /assets/css http://example.com/css
# Resolve index.php
ProxyPass /path http://example.com/index.php
ProxyPassReverse /path http://example.com/index.php
这将按照优先顺序工作。
希望对您有所帮助。