如何使用nginx和uwsgi在一个Django中运行多域名

2024-09-30 08:27:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我想为一个django项目使用多域

一个应用的一个域名

现在我的项目是这样的

test.com/        -> Homepage
test.com/appa    -> App A
test.com/appb    -> App B

我想换成

^{pr2}$

我想我可以用nginx虚拟主机来解决这个问题

但现在我不知道了

请帮帮我。在

现在我用重写它,但不是很好

nginx公司

server {
    ...

    server_name domain2

    ...

    location / {
        rewrite / /app1/
        ...
    }
}

当我浏览域2/

它可以重定向到domain2/app1/

但我想要domain2/as/app1/


Tags: 项目djangotestcomappservernginx域名
3条回答

Nginx可以使用服务器命名空间处理多个域

server {
    listen       80;
    server_name  example.org  www.example.org;
    ...
}

您可以设置多个服务器(最佳做法为多个文件)和多个上游服务器

^{pr2}$

因此每个服务器将被不同的域调用,并将请求传递给不同的uwsgi进程(也称为不同的django app)

Nginx配置的一个示例可能如下所示,请记住,您将需要多个uwsgi进程,每个应用程序一个进程

upstream uwsg_app_a {
  server unix:///tmp/uwsg_app_b.sock;
}

upstream uwsg_app_b {
  server unix:///tmp/uwsg_app_a.sock;
}


server {
    listen       80;
    server_name  www.test.com;

    location / {
        root /homapge/static;
    }
 }

server {
    listen       80;
    server_name  appa.test.com;

    location / {
      uwsgi_pass uwsg_app_a;
    }
 }

server {
    listen       80;
    server_name  appb.test.com;

    location / {
      uwsgi_pass uwsg_app_b;
    }
}

对于uwsgi解决方案,如果您计划托管多个uwsgi应用程序,请看一下uwsgi emperor。在

它使用一个vassals文件夹,必须将它放在您托管的每个uwsgi应用程序的配置文件中。例如: /etc/uwsgi/vassals

为了更新应用程序,您不需要停止|启动uwsgi,只需使用以下命令: touch no-dereference /etc/uwsgi/vassals/any-web-app.ini

附庸配置示例:

[uwsgi]
chdir = /opt/apps/myapp
threads = 20
; bind to the socket
socket = /tmp/sockets/myapp.sock
env = DJANGO_SETTINGS_MODULE=myapp.settings
module = django.core.handlers.wsgi:WSGIHandler()
; privileges
uid = foo
gid = bar

它比使用单独的uwsgi命令减少了一般的RAM消耗。线程的工作方式相同,能够为每个应用程序选择多个进程。在

关于nginx,上一篇文章中提到的上游方法工作得很好,而且使用类unix的套接字更快。 关于nginx的nginx上游配置的进一步帮助可以在uwsgi文档的this anchor中找到

最后我用proxy_pass来解决这个问题

http://site.localhost/app1/        => http://app1.site.localhost/
http://site.localhost/app1/test/   => http://app1.site.localhost/test/
http://site.localhost/app2/        => http://app2.site.localhost/
http://site.localhost/app2/test/   => http://app2.site.localhost/test/

通过

^{pr2}$

相关问题 更多 >

    热门问题