Mod rewrite 代理并重写到WebApp

Mod rewrite 代理并重写到WebApp,mod-rewrite,apache2,reverse-proxy,Mod Rewrite,Apache2,Reverse Proxy,我正在本地使用gunicorn在127.0.0.1:8000上运行一个webapp。我想在http://127.0.0.1/internal并将所有对/demo的调用重写为内部调用 我正在使用ApacheV2.4 使用mod_proxy和mod_rewrite的几种方法 # This gives me `ERR_TOO_MANY_REDIRECTS`. <Location /internal> ProxyPass http://localhost

我正在本地使用gunicorn在
127.0.0.1:8000
上运行一个webapp。我想在
http://127.0.0.1/internal
并将所有对/demo的调用重写为内部调用

我正在使用ApacheV2.4

使用
mod_proxy
mod_rewrite
的几种方法

# This gives me `ERR_TOO_MANY_REDIRECTS`.
    <Location /internal>
        ProxyPass        http://localhost:8000/demo
        ProxyPassReverse http://localhost:8000/demo
        RequestHeader    add X-Script-Name "/internal"
    </Location>

# This works, but I need to visit /internal/demo manually.
    <Location /internal>
        ProxyPass        http://localhost:8000
        ProxyPassReverse http://localhost:8000
        RequestHeader    add X-Script-Name "/internal"
    </Location>
但将此添加到Apache2中无助于:

RewriteRule   /internal$ /internal/demo [R,L]

看来你想要实现两件事:

  • 使您的应用程序在url路径下对用户可见
    /internal/
    ,而不是
    /
  • 将应用程序的
    /demo/
    子文件夹上移到应用程序的根
    /
    (就用户的视图而言)
  • 为了实现(1),我将尝试以下方法:

    <Location /internal>
        ProxyPass        http://localhost:8000/internal
        ProxyPassReverse http://localhost:8000/internal
        RequestHeader    add X-Script-Name "/internal"
    </Location>
    
    
    ProxyPasshttp://localhost:8000/internal
    ProxyPassReversehttp://localhost:8000/internal
    RequestHeader添加X-Script-Name“/内部”
    
    然后,为了实现(2),我将让apache proxy完成这项工作:

    <Location /internal>
        ProxyPass        http://localhost:8000/internal/demo
        ProxyPassReverse http://localhost:8000/internal/demo
        RequestHeader    add X-Script-Name "/internal"
    </Location>
    
    
    ProxyPasshttp://localhost:8000/internal/demo
    ProxyPassReversehttp://localhost:8000/internal/demo
    RequestHeader添加X-Script-Name“/内部”
    
    警告:我只是偶然发现了(1)的解决方案,它确实实现了我想要实现的目标,但我还不明白为什么它会起作用

    <Location /internal>
        ProxyPass        http://localhost:8000/internal
        ProxyPassReverse http://localhost:8000/internal
        RequestHeader    add X-Script-Name "/internal"
    </Location>
    
    <Location /internal>
        ProxyPass        http://localhost:8000/internal/demo
        ProxyPassReverse http://localhost:8000/internal/demo
        RequestHeader    add X-Script-Name "/internal"
    </Location>