Search code examples
djangoapachemod-proxygunicorn

When I visit Django /admin via a Rewritten URL I am redirected to 127.0.0.1 rather than the correct url


I haven't used Apache with Django very much, normally using nginx, gunicorn and a unix socket instead.

When experimenting with Apache, mod_proxy and Django, I've been trying to pass all requests to a gunicorn backend, listening on 127.0.0.1:8998.

I've achieved this using a Rewrite ... [P] (see below), however, this works fine for the default URL, when I go to http://foo.bar.com/admin I get redirected to 127.0.0.1/admin, which clearly doesn't work remotely. Is this a setup possible to achieve via an htaccess file, and what do I need to do/look at to fix it up?

.htaccess:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule     (.*) http://localhost:8998/$1 [P]

Thanks!


Solution

  • Is it working with http://localhost and http://localhost/ ?

    try this:

    RewriteRule     ^/(.*) http://localhost:8998/$1 [P]
    

    I have recently researched what you are trying to achive and ended up with this:

    ProxyRequests Off
    <Proxy *>
        Order deny,allow
        Allow from all
    </Proxy>
    ProxyPass        /static/ http://example.com:8080/static/
    ProxyPassReverse /static/ http://example.com:8080/static/
    

    I am passing all static path requests to nginx in this case, but it may work with gunicorn for you.

    Of course, you need to enable apache2 module proxy.