Martin Weber
Martin Weber

Reputation: 1

Proxy and rewrite to WebApp

I'm running a webapp on local using gunicorn on 127.0.0.1:8000. I want to make its service-page /demo accessible on http://127.0.0.1/internal and rewrite all calls to /demo to internal

I'm using Apache v2.4.

Several approaches using mod_proxy and 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>

It doesn't matter, where to place the ProxyPass...

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

According to the developer, RequestHeader add X-Script-Name is supported.

Using NGINX, this works as expected:

    location /internal {
        proxy_pass http://localhost:8000;
        proxy_set_header Host $http_host;
        proxy_set_header X-Script-Name /internal;
        rewrite /internal$ /internal/demo redirect;
    }

But adding this to Apache2 doesn't help:

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

Upvotes: 0

Views: 69

Answers (1)

PMc
PMc

Reputation: 118

it seems You want to achieve two things:

  1. make your application visible to the users under the url path /internal/ instead of /.
  2. move the /demo/ subfolder of your application up to the root / of your application (as far as the view of the users is concerned).

To achieve (1) I would try this one:

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

And then to achieve (2) I would just let apache proxy do the job:

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

Caveat: I just found out, by accident, that solution to (1), and it does exactly what I wanted to achieve, but I do not yet understand why it works.

Upvotes: 1

Related Questions