I am using Django 1.8 by the way.
I am trying to deploy multiple Django app at with same domain/subdomain but different URLs so I am using SCRIPT_NAME header.
My Nginx config:
location /myapp/ {
proxy_pass http://127.0.0.1:8000/;
proxy_set_header SCRIPT_NAME /myapp;
}
Site is loading with this conf but the request. META['SCRIPT_NAME'] is empty as well as when I hover any links, they are showing without 'myapp' in the URL.
Any help?
I didn't find the answer to this in all my searching, so here it is:
proxy_pass http://127.0.0.1:8000/ is wrong, you need proxy_pass http://127.0.0.1:8000 (without the trailing backslash) in order to keep /myapp at the start of the URL path. Django expects its requests to start with the value of the SCRIPT_NAME header, and strips it off before your apps see it. Django will return an error for any URL that doesn't start like this.settings.py defines STATIC_URL as an absolute path like /static/, Django will not prepend it with your script name. Use a relative path like static/.STATIC_FILES header. For example, if you have another nginx in between then you need to make sure that your server block contains the line underscores_in_headers on;.LOGIN_REDIRECT_URL are not prepended (and you cannot use the relative path trick as this does something different), so you can fudge this with a line: proxy_redirect / /myapp/;, but then most redirects are prepended, so you also need a line before that: proxy_redirect /myapp/ /myapp/;. This technique will not work if /myapp/myapp is actually a valid path, of course.So your nginx block should look a bit like this:
location /myapp/ {
proxy_pass http://127.0.0.1:8000;
proxy_set_header SCRIPT_NAME /myapp;
proxy_redirect /myapp/ /myapp/;
proxy_redirect / /myapp/;
location /myapp/static/ {
root /var/www; # files are deployed to /var/www/myapp/static
}
}
and in settings.py:
STATIC_URL=`static/`
and in requirements.txt:
Django==3.1.6
What a nightmare. Does anybody have any better ideas?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With