T
T
TTV2019-06-05 08:05:05
Django
TTV, 2019-06-05 08:05:05

Setting up virtualhosts in uWSGI?

I want to try to make two sites with one backend.
I plan to use Django-sites-framework + uWSGI + NGINX . I found information on setting up the latter on the network, but I didn’t manage to find much on the Django-sites-framework + uWSGI bundle. As far as I understood from what I managed to read, you need to configure uWSGI sockets and virtualhosts to direct requests to the appropriate sites. Does anyone know how to configure using two domains as an example?
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dirlandets, 2019-06-05
@Dirlandets

Hey!
On the example of my task: one database for several sites of the same type, it was necessary to display a different main page, load slightly different css styles, contacts, etc.
In settings.py

INSTALLED_APPS = [
    ...
    'django.contrib.sites',
    ...
]
ALLOWED_HOSTS = ['subdomen.example.com', 'example.com', 'subdomen2.example.com']

Don't forget to migrate.
The "sites" section will appear in the admin panel, where you can add the domains 'subdomen.example.com', 'example.com', 'subdomen2.example.com'.
In views.py
current_site = get_current_site(request).domain # можно использовать id, но мне показалось нагляднее использовать domain
if current_site == 'subdomen.example.com':
   # ...some logic например разный context или template
elif  current_site == 'subdomen2.example.com':
   # ...some logic например разный context или template

For nginx, everything is simple, you create a config for each domain, they just lead to one place (socket / port), you can create one config with if, etc. but it's so uncomfortable for me.
...
location / {
            # в сокет
            proxy_pass http://unix:/tmp/example.socket; 
            # в порт, выбери одно
            #proxy_pass http://127.0.0.1:8000; 
            proxy_set_header Host $server_name;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
...

There is another option when you need completely different logic, then several settings.py files are created
And numbered SITE_ID=1, SITE_ID=2, SITE_ID=3
According to uWSGI, I can’t remember if there are any nuances, I have been using gunicorn for a long time (I just got used to it)
But nothing special you don't need to do anything different from the usual ones:
gunicorn --bind unix:/tmp/unix:/tmp/example.socket project.wsgi:application

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question