Hosting a django project behind proxypass

2019-07-17 20:39发布

I have hosted to django admin project on a local machine X. http://10.4.x.y/myapp/admin works. I have an external IP on another machine Y and i am doing a proxy pass from the Y to X.

http://proxypassname.com/myapp/admin works. But, When i click the link "Save" or "Save continue editing" button after editing in admin page, it redirects to local machine ip (i.e. http://10.4.x.y/myapp/blah_blah_blah).

How to make sure that the django project redirects to proxypass name instead of local IP?

2条回答
该账号已被封号
2楼-- · 2019-07-17 20:51

I did these two things and it worked.

  1. Whenever you add a ProxyPass, you should add ProxyPassReverse

  2. SITE_ID should be set to the domain where you want to point this django project.

查看更多
Evening l夕情丶
3楼-- · 2019-07-17 20:55

This is happening because the admin redirects to IP it thinks it has. It gets in in the HTTP request's header.

However, the fix is very easy. Assuming you proxy server implements the X-Forwarded-For standard, it could be easily fixed.

in your settings.py, simply set:

USE_X_FORWARDED_HOST = True

and restart your Django.

If that doesn't work, you can try to see if your proxy sets a different kind of header, and write a middleware that does the same thing. It's the first example on Django's documentation chapter on middleware

查看更多
登录 后发表回答