Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
394 views
in Technique[技术] by (71.8m points)

apache - Django: URLs contain 127.0.0.1 and not the real host-name

I run Django in gunicorn (started via systemd) behind Apache:


<VirtualHost *:443>
    ...
    <Location /relayrace/>
            ProxyPass unix:/run/gunicorn-traverse.sock|http://127.0.0.1/
            ProxyPassReverse unix:/run/gunicorn-traverse.sock|http://127.0.0.1/
    </Location>

But the URLs (for example email created by django-allauth) use 127.0.0.1 and not the real FQDN.

How to fix this?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

After setting USE_X_FORWARDED_HOST and SECURE_PROXY_SSL_HEADER it worked:

settings.py:

USE_X_FORWARDED_HOST = True
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

And I needed to tell Apache to provide the forwarded protocol:

    <Location /relayrace/>
        RequestHeader set "X-Forwarded-Proto" expr=%{REQUEST_SCHEME}
        ProxyPass unix:/run/gunicorn-traverse.sock|http://127.0.0.1/
        ProxyPassReverse unix:/run/gunicorn-traverse.sock|http://127.0.0.1/
    </Location>

I used config_view to debug this.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...