简体   繁体   English

在Ubuntu 14.04信任服务器上使用Nginx和Gunicorn的多个Django应用

[英]Multiple Django app using nginx and gunicorn on Ubuntu 14.04 trusty server

I am new to server configuration. 我是服务器配置的新手。 I do some google and config django app using gunicorn and nginx on ubuntu 14.04 trusty server. 我在ubuntu 14.04可信服务器上使用gunicorn和nginx做一些google和django应用程序。 For the first django app I use port number 80 and my configfiles are : 对于第一个django应用程序,我使用端口号80,我的配置文件为:

/etc/init/gunicorn.conf :- /etc/init/gunicorn.conf:-

description "Gunicorn application server handling myproject"
start on runlevel [2345]
stop on runlevel [!2345]
respawn
setuid 
setgid www-data
chdir /home/myserver/my_virtualenv_path/myproject
exec /home/myserver/my_virtualenv_path/myproject/gunicorn --workers 2 --bind unix:/home/myserver/my_virtualenv_path/myproject/myproject.sock myproject.wsgi:application

My nginx configuration file for first django app: 我的第一个Django应用程序的Nginx配置文件:

/etc/nginx/site-available :- / etc / nginx / site-available:-

server {
    listen 80;
    server_name myapp.com;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/myserver/my_virtualenv_path/myproject;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/home/myserver/my_virtualenv_path/myproject/myproject.sock;
    }
}

After that, i link site to site-enabled . 之后,我将站点链接到启用站点的站点。 Next, i create a new django app inside the first django app virtualenv like: 接下来,我在第一个django应用程序virtualenv中创建一个新的django应用程序,如下所示:

FirstApp_Virtual_Env\\first_djangoapp\\app files FirstApp_Virtual_Env \\ first_djangoapp \\ app文件

FirstApp_Virtual_Env\\Second_djangoapp\\app files FirstApp_Virtual_Env \\ Second_djangoapp \\ app文件

Now i configure gunicorn for second app like : 现在我为第二个应用程序配置gunicorn:

/etc/init/gunicorn_t :- / etc / init / gunicorn_t:-

description "Gunicorn application server handling myproject2"
    start on runlevel [2345]
    stop on runlevel [!2345]
    respawn
    setuid 
    setgid www-data
    chdir /home/myserver/my_virtualenv_path/myproject2
    exec /home/myserver/my_virtualenv_path/myproject/gunicorn --workers 2 --bind unix:/home/myserver/my_virtualenv_path/myproject2/myproject2.sock myproject2.wsgi:application

My nginx configuration file for second django app: 我的第二个Django应用程序的Nginx配置文件:

/etc/nginx/site-available :- / etc / nginx / site-available:-

server {
    listen 8000;
    server_name myapp2.com;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/myserver/my_virtualenv_path/myproject2;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/home/myserver/my_virtualenv_path/myproject2/myproject2.sock;
    }
}

After that i link site to site-enabled . 之后,我将站点链接到启用站点的站点。

Now here is my problem: when i type myapp.com then my first django app is working fine but for second app when i type myapp2.com its showing nginx page and when i type myapp2.com:8000 it's working fine . 现在这是我的问题:当我键入myapp.com时,我的第一个django应用程序运行正常,但是对于第二个应用程序,当我键入myapp2.com其显示的nginx页面,而当我键入myapp2.com:8000时,它运行正常。 I do some google for that but i am unable to find solution. 我为此做一些谷歌,但我找不到解决方案。 I am newbie to this so please give me a hint for that how to correct my problem. 我是新手,因此请给我一个有关如何解决我的问题的提示。 Thanks for your time. 谢谢你的时间。

You configured nginx to serve myapp2.com on port 8000: 您将nginx配置为在端口8000上提供myapp2.com:

server {
    listen 8000;
    server_name myapp2.com;
    # ...
}

so why would you expect nginx to serve it on port 80 ? 那么,为什么您希望nginx在端口80上提供服务呢?

[edit] I thought the above was enough to make the problem clear but obviously not, so let's start again: [编辑]我认为以上内容足以使问题明确,但显然还不能,因此让我们重新开始:

You configured nginx to serve myapp2.com on port 8000 (the listen 8000; line in your conf, so nginx do what you asked for: it serves myapp2.com on port 8000. 您将nginx配置为在端口8000上服务myapp2.com( listen 8000; conf中的行),因此nginx会执行您要求的操作:它在端口8000上服务myapp2.com。

If you want nginx to serve myapp2.com on port 80 (which is the implied default port for http so you don't have to specify it explicitely in your url - IOW ' http://myapp2.com/ ' is a shortcut for ' http://myapp2.com:80/ '), all you have to do is to configure nginx to serve it on port 80 just like you did for 'myapp.com': replace listen 8000; 如果你想nginx的服务myapp2.com端口80(这是隐含的默认端口用于HTTP,所以你不必在你的URL显式地指定它-督察“ http://myapp2.com/ ”是一个快捷方式' http://myapp2.com:80/ '),您只需配置nginx即可在端口80上提供服务,就像您对'myapp.com'所做的那样:replace listen 8000; by listen 80; listen 80; .

If you don't type in a port, your client will automatically use port 80. 如果您不输入端口,则客户端将自动使用端口80。

Typing myapp2.com is the same as typing myapp2.com:80 键入myapp2.com与输入myapp2.com:80相同

But myapp2.com is not running on port 80, it's running on port 8000. 但是myapp2.com不在端口80上运行,而是在端口8000上运行。

When you go into production it is possible to redirect myapp2.com to port 8000 without explicitly typing it. 当您投入生产时,可以将myapp2.com重定向到端口8000,而无需显式键入它。 You register myapp2.com with a DNS name server and point it towards myapp2.com:8000 您使用DNS名称服务器注册myapp2.com,并将其指向myapp2.com:8000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM