Apache重写,然后代理,哪个顺序?

发布于 2025-01-23 05:08:08 字数 2506 浏览 3 评论 0原文

我知道对于VHOST,使用 mod-prewrite 和mod-proxy 应该很容易。但是我无法获得正确的顺序。

这是我要实现的目标:

  1. 应交付文件(js,cs,图像,字体等),正常交付
  2. 某些路径的XHR请求( ibo-php )应由代理处理,另一个vhost和 http (不是 https
  3. 添加 .php 结尾到所有这些数据请求,匹配API PATH ibo-php
  4. vhost有一个用于Web应用程序,该应用程序具有不同的应用程序状态为路径,例如/my-app/view/subpart/1 ,因此这些路径部分需要是忽略了

重写规则确实适用于文件,但代理不适合。我打电话给API的电话是 404 “ post/ec2-app/ibo-php/Quarter http/1.1” 404 645“ https://ec2.localhost/ec2 -app/“

(apache访问日志, ec2.localhost 是vhost的名称)

,我猜是规则的顺序有问题:

# for avoiding 403 from https://stackoverflow.com/a/38353249/2092322
<Directory "/home/myname/work/sdp/frontends">
    Require all granted
    RewriteEngine On
    ProxyPreserveHost On

    # background https://stackoverflow.com/a/58307829/2092322
    # now: https://gkedge.gitbooks.io/react-router-in-the-real/content/apache.html
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^ - [L]

    # the END flag might be a problem, but I currently think that ibo-php is not matched by this rule
    RewriteRule ec2-app/(.*)/(js|style|resources)/(.+)\.(.+)$ ec2-app/$2/$3.$4 [END]

    # "POST /ec2-app/ibo-php/quarter HTTP/1.1" 404 645 "https://ec2.localhost/ec2-app/"
    RewriteRule ec2-app/ibo-php/(.+)$ ec2-app/ibo-php/$1.php [L,PT]

    # anything else to index.html
    RewriteRule ec2-app/(.+) ec2-app/index.html [L]

    ProxyRequests Off
    <Proxy *>
        Require all granted
    </Proxy>
    ProxyPass ec2-app/ibo-php/ http://ibo-php.localhost:7010/
    ProxyPassReverse ec2-app/ibo-php/ http://ibo-php.localhost:7010/
</Directory>

其他来源: so mod_rewrite

[在这里]我们只有在找不到资源的情况下才能代表请求 本地。当您从一台服务器迁移时,这可能非常有用 到另一个,您不确定是否所有内容都迁移 但是。

  rewriteCond“%{request_filename}”!-f
重新WriteCond“%{request_filename}”!-d
重写“^/(。*)”“ http://old.example.com/jum1” [p]
proxypassReverse”/“” http://old.example.com/”
 

讨论:

在每种情况下,我们都会添加一个proxypassReverse Directerive,以确保任何 后端发出的重定向已正确传递给客户。

尽可能考虑使用proxypass或proxypassmatch 偏爱mod_rewrite。

通过使用别名rewriteBase,问题

都会更容易?

I know it should be pretty easy to use mod-rewrite and mod-proxy for a Vhost. But I cannot get the right order.

Here is what I want to achieve:

  1. Files (JS, CSS, images, fonts, etc.) should be delivered normally
  2. XHR requests for certain path (ibo-php) should be handled by a proxy, using another Vhost and http (not https)
  3. add a .php ending to all these data requests, matching the API path ibo-php
  4. The Vhost is there for a web-app, which has different app states as path, e.g. /my-app/view/sub-part/1, so these path parts need to be ignored

The rewrite rules do work for files, but the proxy does not. I am getting a 404 for my calls to the API: "POST /ec2-app/ibo-php/quarter HTTP/1.1" 404 645 "https://ec2.localhost/ec2-app/"

(Apache access log, ec2.localhost is the name of the Vhost)

I guess that there is something wrong with the order of rules:

# for avoiding 403 from https://stackoverflow.com/a/38353249/2092322
<Directory "/home/myname/work/sdp/frontends">
    Require all granted
    RewriteEngine On
    ProxyPreserveHost On

    # background https://stackoverflow.com/a/58307829/2092322
    # now: https://gkedge.gitbooks.io/react-router-in-the-real/content/apache.html
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^ - [L]

    # the END flag might be a problem, but I currently think that ibo-php is not matched by this rule
    RewriteRule ec2-app/(.*)/(js|style|resources)/(.+)\.(.+)$ ec2-app/$2/$3.$4 [END]

    # "POST /ec2-app/ibo-php/quarter HTTP/1.1" 404 645 "https://ec2.localhost/ec2-app/"
    RewriteRule ec2-app/ibo-php/(.+)$ ec2-app/ibo-php/$1.php [L,PT]

    # anything else to index.html
    RewriteRule ec2-app/(.+) ec2-app/index.html [L]

    ProxyRequests Off
    <Proxy *>
        Require all granted
    </Proxy>
    ProxyPass ec2-app/ibo-php/ http://ibo-php.localhost:7010/
    ProxyPassReverse ec2-app/ibo-php/ http://ibo-php.localhost:7010/
</Directory>

Other sources:
SO, mod_rewrite:

[Here] we proxy the request only if we can't find the resource
locally. This can be very useful when you're migrating from one server
to another, and you're not sure if all the content has been migrated
yet.

RewriteCond "%{REQUEST_FILENAME}"       !-f
RewriteCond "%{REQUEST_FILENAME}"       !-d
RewriteRule "^/(.*)" "http://old.example.com/$1" [P]
ProxyPassReverse "/" "http://old.example.com/"

Discussion:

In each case, we add a ProxyPassReverse directive to ensure that any
redirects issued by the backend are correctly passed on to the client.

Consider using either ProxyPass or ProxyPassMatch whenever possible in
preference to mod_rewrite.

Question

Would all be more easy by using Alias or RewriteBase?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天气好吗我好吗 2025-01-30 05:08:08

解决方案

正确的顺序为

<Directory "/home/myname/work/sdp/frontends">
    Require all granted
    RewriteEngine On

    # RewriteRule ec2-app/ibo-php/(.+)$ /ec2-app/ibo-php/$1.php [L,PT]

    # background https://stackoverflow.com/a/58307829/2092322
    # now: https://gkedge.gitbooks.io/react-router-in-the-real/content/apache.html
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^ - [L]
    RewriteRule ec2-app/(.*)/(js|style|resources)/(.+)\.(.+)$ /ec2-app/$2/$3.$4 [END]

    # anything else to index.html
    RewriteRule ec2-app/(.+) /ec2-app/index.html [L]
</Directory>

LogLevel debug

ProxyPreserveHost On
# "POST /ec2-app/ibo-php/quarter HTTP/1.1" 404 645 "https://ec2.localhost/ec2-app/"
ProxyPassMatch ^/ec2-app/ibo-php/(.+)$ http://ibo-php.localhost:7010/$1.php
ProxyPassReverse ^/ec2-app/ibo-php/ http://ibo-php.localhost:7010/

proxypassmatch &lt;目录&gt;中不允许使用。

Solution

The right order is

<Directory "/home/myname/work/sdp/frontends">
    Require all granted
    RewriteEngine On

    # RewriteRule ec2-app/ibo-php/(.+)$ /ec2-app/ibo-php/$1.php [L,PT]

    # background https://stackoverflow.com/a/58307829/2092322
    # now: https://gkedge.gitbooks.io/react-router-in-the-real/content/apache.html
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^ - [L]
    RewriteRule ec2-app/(.*)/(js|style|resources)/(.+)\.(.+)$ /ec2-app/$2/$3.$4 [END]

    # anything else to index.html
    RewriteRule ec2-app/(.+) /ec2-app/index.html [L]
</Directory>

LogLevel debug

ProxyPreserveHost On
# "POST /ec2-app/ibo-php/quarter HTTP/1.1" 404 645 "https://ec2.localhost/ec2-app/"
ProxyPassMatch ^/ec2-app/ibo-php/(.+)$ http://ibo-php.localhost:7010/$1.php
ProxyPassReverse ^/ec2-app/ibo-php/ http://ibo-php.localhost:7010/

ProxyPassMatch is not allowed in <Directory>.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文