2

I have a problem with my Apache2 web server as reverse proxy server. The simplified server setup with my domain "example.com" looks like this:

server setup

If clients now go to the page https://example.com/guacamole, then the Apache2 web server should forward the requests to the Tomcat server (https://127.0.0.1:8080/guacamole). This part works.

If clients go to https://example.com/mydjangoproject, then the Apache2 web server should forward requests to the web server using Gunicorn and the Django project (https://192.168.30.5:8000). However, this configuration works only partially. The page https://example.com/mydjangoproject shows the main page of the Django project, but when calling the admin page https://example.com/mydjangoproject/admin the django application gives the error that the page http://192.168.30.5:8000//admin does not exist. It seems like the sub-URL "/mydjangoproject/" is removed when calling the admin page. What could that be?

This is my configuration:

<IfModule mod_ssl.c>
<VirtualHost *:443>
    ServerAdmin admin@example.com
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com/html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    Include /etc/letsencrypt/options-ssl-apache.conf
    SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem

    JKMount /* ajp13_worker

    SSLProxyEngine on
    SSLProxyVerify none
    SSLProxyCheckPeerCN off
    SSLProxyCheckPeerName off
    ProxyPreserveHost On

    ProxyPass /static/ !
    ProxyPass /mydjangoproject https://192.168.30.5:8000/
    ProxyPassReverse /mydjangoproject https://192.168.30.5:8000/
</VirtualHost>
</IfModule>

Thanks for your help!

Edit: When i try to enter the admin-page via https://example.com/mydjangoproject/admin then this error occurs:

error screenshot

2
  • 1
    First thing that I'd look at is the proxypass directives. Since /mydjangoproject is proxying https://192.168.30.5:8000/, then https://example.com/mydjangoproject/admin will return whatever it finds at https://192.168.30.5:8000/admin. Is the admin panel you're expecting being served from there?
    – Kefka
    Jul 29, 2019 at 13:49
  • Hi Kefka! Thanks for your reply. Normally there should be the admin-panel. I edited my post with the error.
    – Markus
    Jul 29, 2019 at 16:42

1 Answer 1

3

You'll likely need to alter your ProxyPass and ProxyPassReverse directives to get this to work.

You should try replacing these lines:

ProxyPass /static/ !
ProxyPass /mydjangoproject https://192.168.30.5:8000/
ProxyPassReverse /mydjangoproject https://192.168.30.5:8000/

With e.g.:

# "Convenience" URL
ProxyPass /mydjangoproject https://192.168.30.5:8000
ProxyPassReverse /mydjangoproject https://192.168.30.5:8000

# --- The lines below are what actually allow access to /admin ---

# Proxy /admin for login
ProxyPass /admin http://192.168.30.5:8000/admin
ProxyPassReverse /admin http://192.168.30.5:8000/admin

# Proxy /static for CSS, etc.
ProxyPass /static http://192.168.30.5:8000/static
ProxyPassReverse /static http://192.168.30.5:8000/static

You should then be able to use e.g. https://example.com/admin to access your Django /admin page and get the correct login dialog:

ex. Django Admin Login

Django Admin Login - Screenshot

When I try to enter the admin page via https://example.com/mydjangoproject/admin an error occurs.

You should be able to add the following lines (in addition to those recommended above) to get this to (kind of) work:

ProxyPass /mydjangoproject/admin http://192.168.30.5:8000
ProxyPassReverse /mydjangoproject/admin http://192.168.30.5:8000

Note that Django seems to redirect ex. https://example.com/mydjangoproject/admin to ex. https://example.com/admin automatically, which breaks things unless you already have ex. ProxyPass /admin http://192.168.30.5:8000/admin as an existing entry (included above).

3
  • Thanks for your help! Now it works! Perfect :)
    – Markus
    Jul 30, 2019 at 11:31
  • You're welcome. Glad you got it working. =) Jul 30, 2019 at 18:24
  • @Anaksunaman, your solution is fine, I had the same problem, thanks! Now, there is an issue: I'm using netbox (which is an IPAM) that faces this same situation. Now, your workaround is somehow rigid if there are plenty of redirections to configure under apache with the several ProxyPass and ProxyPassReverse. Is there any solution that may do this for all the URLs? Aug 30, 2020 at 0:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.