Host Django on subfolder

风流意气都作罢 提交于 2019-11-30 18:20:37

问题


I have deployed Django with Gunicorn and NGINX, and it works fine, if the Django app is served on the root url, with this configuration:

server {
    listen 80;

    location = /favicon.ico { access_log off; log_not_found off; }

    location / {
        include proxy_params;
        proxy_pass http://unix:my_app.sock;
    }
}

However when I try to serve the Django app on another URL, it doesn't work. If I try to access http://domain/my_app/admin/ then Django tells me it cannot find the view.

This is the NGINX config:

server {
    listen 80;

    location = /favicon.ico { access_log off; log_not_found off; }

    location /my_app {
        include proxy_params;
        proxy_pass http://unix:/var/my_app/app.sock;
    }
}

How could I make this work? I was not able to find any solution to specify something like a "BASE_URL" so far.


回答1:


My comment doesn't show the whole picture. When I've run Django sites on subfolders, I like to use a dynamic config so that you can still access the machine directly (without the proxy) and have a working web-app. This can help a LOT for debugging tricky stuff like this that is hard to reproduce in dev.

If you do not have the ability to pass the header or modify wsgi.py, you can still set FORCE_SCRIPT_NAME in your Django settings.

3 steps:

  1. set up a proxy in front of the webserver that strips the subfolder out of the URL
  2. set the X-Script-Name header so that your Django site generates its urls with /myapp/ infront of them -- make sure you're using the {% url %} tag and reverse, vs. hard-coding!
  3. modify myapp/wsgi.py to read a new header X-Script-Name into the wsgi environment variable SCRIPT_NAME (based on this flask snippet)

Here is an example Nginx config for a proxy that points to a Django site on a subdirectory and also sets X-Script-Name (steps 1 and 2), note that this doesn't use a unix socket, so it's a little different from the OP's question. Would welcome an edit:

nginx.conf

location /my_app {
    rewrite ^/my_app/(.*)$ /$1 break;
    proxy_pass https://mywebapp.com/$uri$is_args$args;
    proxy_set_header Host $host;
    proxy_set_header X-Script-Name /my_app;
    proxy_cookie_path / /my_app;
}

And to read X-Script-Name:

myapp/wsgi.py

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings")

_application = get_wsgi_application()


def application(environ, start_response):
    # http://flask.pocoo.org/snippets/35/
    script_name = environ.get('HTTP_X_SCRIPT_NAME', '')
    if script_name:
        environ['SCRIPT_NAME'] = script_name
        path_info = environ['PATH_INFO']
        if path_info.startswith(script_name):
            environ['PATH_INFO'] = path_info[len(script_name):]

    scheme = environ.get('HTTP_X_SCHEME', '')
    if scheme:
        environ['wsgi.url_scheme'] = scheme

    return _application(environ, start_response)


来源:https://stackoverflow.com/questions/47941075/host-django-on-subfolder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!