Configuration of NGINX + uWSGI + Django in a multi-tenancy application

Asked

Viewed 161 times

-2

I have a single application in Django hosted on AWS. But these days, I turned it into multi-tenancy, using Django-tenant-schemas. Locally, it runs normally. I can create my tenants and access them on my local Django server. However, I don’t know how to run on AWS.

My file . conf for NGINX looks like this:

upstream django {
    server unix:///home/ubuntu/pasta_meu_projeto/mysite.sock; # for a file s$
}

# configuration of the server
server {
    listen      80; address or $
    server_name ssh *.example.com.br;
    charset     utf-8;

    # max upload size
    client_max_body_size 75M;   # adjust to taste

    location /media  {
        alias /home/ubuntu/pasta_meu_projeto/media;  # your Django project's$
    }

    location /static {
        alias /home/ubuntu/pasta_meu_projeto/static; # your Django project's$
    }

    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django;
        include     /home/ubuntu/pasta_meu_projeto/uwsgi_params; # the uwsgi$
    }
}

I pulled for my project that runs on AWS, everything worked. And my only change to access the subdomains (only what I changed) was in the file above, pasting the * before the point: *.example.com.br. I tried to use regex too, but it didn’t work out too well either (server_name ~^(?<subdomain>.+)\.example\.com\.br$;). Can anyone tell me what I should do? What are the settings of Nginx? If besides tinkering with .conf, I must do some other setting, in some other "place"?

  • 1

    Which wsgi server are you using? what is your question?

  • Hi, @Sidon! Thanks for replying! I’m using uWSGI. I wonder if there’s something missing in that mine .conf and if I need to do some more configuration, beyond what I’ve already done.

  • 1

    Have some special reason for Voce not to use what is almost a standard in the Django world? Gunicorn What problem are you having? Your question is not clear, see that has already been denied.

  • No, I don’t have any special reason. The two are equivalently good! I just decided to use uWSGI. I’ll try to be clearer...

  • 1

    I find it very difficult to say that qq sw is better than others, in that case it would be more difficult even if I didn’t know about uWSGI, but if you asked me for a recommendation I would recommend Gunicorn, for the reason that it is much more widespread in the Django world, see this comparison

  • When I create the subdomain locally, for example, prefix.localhost, I can access the application: http://prefixo.localhost, on port 8000. I would also like to access prefix.exemplo.com.br on my VPN. Of my initial settings, the only thing I changed was this quoted line (*server_name .example.com.br). When accessing, the created subdomain, does not return me anything, despite realizing that my NGINX is running through that classic message (Welcome to Nginx!), trying to access the domain without prefix, http://exemplo.com.br.

  • 1
  • All right, that’s not my question. So please, if you can tell me the steps I should take to turn my single app into a multi, using Gunicorn, I really appreciate it. I believe that the changes of my ngnix settings will not affect my application, even because who is at the door is my NGINX and not who talks to Django.

  • I sent you the link to the solution of multsite in Nginx+gunicorn in chat, try first and, if something goes wrong, rephrase the question or ask another. In time... Your question remains obscure and candidate to close.

Show 4 more comments

1 answer

1


The answer to my question was simple and straightforward when posted on Stackoverflow in English. To many it seems my question was shallow. However, I would like to summarize once again what my doubt was. I would like to know what you should set up in my . conf of Nginx to be able to access my tenants, subdomains. The answer came directly, without question:

  • Barter server_name ssh *.example.com.br;

FOR

  • server_name ssh.example.com.br *.example.com.br;

  • Finally, Add for each subdomain:

server {
  server_name subdmino1.example.com.br;
  # ...
}

server {
  server_name subdominio2.example.com.br;
  # ...
}


server {
  server_name subdominio3.example.com.br;
  # ...
}

With the help of @Jean-Jacques MOIROUX, reply, and this tutorial, solved my problem in just over 5 min. Below follow the settings for my file:

upstream django {
    server unix:///home/ubuntu//mysite.sock; # for a file s$
}

# configuration of the server
server {
    listen      80;
    # Linha acrescentada do tutorial
    listen [::]:80;
    server_name ssh.example.com.br *.example.com.br;
    charset     utf-8;

    # max upload size
    client_max_body_size 75M;   # adjust to taste

    location /media  {
        alias /home/ubuntu/pasta_meu_projeto/media;  # your Django project's$
    }

    location /static {
        alias /home/ubuntu/pasta_meu_projeto/static; # your Django project's$
    }

    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django;
        include     /home/ubuntu/pasta_meu_projeto/uwsgi_params; # the uwsgi$
    }
}

server {
    listen      80;
    # Linha acrescentada do tutorial
    listen [::]:80;
    server_name subdominio1.example.com.brr;
    charset     utf-8;

    # max upload size
    client_max_body_size 75M;   # adjust to taste

    location /media  {
        alias /home/ubuntu/pasta_meu_projeto/media;  # your Django project's$
    }

    location /static {
        alias /home/ubuntu/pasta_meu_projeto/static; # your Django project's$
    }

    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django;
        include     /home/ubuntu/pasta_meu_projeto/uwsgi_params; # the uwsgi$
    }
}

server {
    listen      80;
    # Linha acrescentada do tutorial
    listen [::]:80;
    server_name subdominio2.example.com.br;
    # mesmas configurações acima
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.