首页 文章

使用反向代理加载站点时,Laravel路由不起作用

提问于
浏览
0

我在我的项目中使用反向代理将路径重定向到不同的服务器 .

站点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/” .

示例:路由 - “testsite.abc.com/test-route”工作正常 . 路线 - “abc.com/new-url/test-route”显示404未找到错误 . 但“abc.com/new-url/index.php/test-route”工作正常 .

我认为,这里重写规则覆盖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 ^(.*)/$ /$1 [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>

造成这个问题的原因是什么?有人可以帮忙吗?

1 回答

  • 1

    这是解决此问题的解决方法 .

    更新反向代理规则以首先解析静态路径,

    # 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
    

    这将按优先顺序运行 .

    我希望这有帮助 .

相关问题