简体   繁体   English

使用systemD服务在带有gunicorn + django的Nginx服务器中设置2个站点

[英]Setting 2 sites in a Nginx server with gunicorn + django using systemD service

I am following the tutorial: http://www.obeythetestinggoat.com/book/chapter_09.html 我正在学习该教程: http : //www.obeythetestinggoat.com/book/chapter_09.html

So, i have two sites in DigitalOcean with nginx and gunicorn , one for stage and the normal one... stage.nossalista.tk and nossalista.tk , first i manage to successfully deploy and run the stage one, but after upload the normal and added another systemd service for my normal one, everything stops working.. i gonna put all my configs and hope you guys give me some hints were may be the problem.... 因此,我在DigitalOcean中有两个站点,分别是nginxgunicorn ,一个是舞台,另一个是普通的... stage.nossalista.tknossalista.tk ,首先,我成功地成功部署并运行了第一阶段,但是在上传正常后并为我的普通服务添加了另一个systemd服务,一切都停止了工作..我要放置所有配置,并希望你们给我一些提示可能是问题所在。

im getting error 400 from the nossalista.tk and 502 for my stage.nossalista.tk 即时通讯从nossalista.tk获得错误400,并为我的stage.nossalista.tk获得502错误

[UPDATE]: I was able to access the site only in stage.nossalista.tk:9000 .... hope this helps who wants to help me [更新]:我只能在stage.nossalista.tk:9000中访问该网站....希望这对希望帮助我的人有所帮助

First i did is configure the 首先我要做的是配置

/etc/nginx/sites-available/stage.nossalista.tk: /etc/nginx/sites-available/stage.nossalista.tk:

server {
    listen 80;
    server_name nossalista.tk;

    location /static {
        alias /home/tyago/sites/nossalista.tk/static;
    }

    location / {
        proxy_set_header Host $host;
        proxy_pass http://unix:/tmp/nossalista.tk.socket;
    }
}

Second i configure the service for this stage site in 其次,我在以下位置为此阶段站点配置服务

/etc/systemd/system/gunicorn.service: /etc/systemd/system/gunicorn.service:

[Unit]
Description=Gunicorn server for stage.nossalista.tk
After=network.target

[Service]
User=tyago
Group=www-data
WorkingDirectory=/home/tyago/sites/stage.nossalista.tk/source
ExecStart=/home/tyago/sites/stage.nossalista.tk/virtualenv/bin/gunicorn --workers 3 --bind unix:/tmp/stage.nossalista.tk.socket superlists.wsgi:application
Restart=always

[Install]
WantedBy=multi-user.target

and then configure my socket service in 然后在中配置我的套接字服务

/etc/systemd/system/gunicorn.socket: /etc/systemd/system/gunicorn.socket:

[Unit]
Description=gunicorn socket

[Socket]
ListenStream=/run/gunicorn/socket
ListenStream=0.0.0.0:9000
ListenStream=[::]:8000

[Install]
WantedBy=sockets.target

and then the gunicorn.conf file in (tyago is my user) 然后是(tyago是我的用户)中的gunicorn.conf文件

/usr/lib/tmpfiles.d/gunicorn.conf /usr/lib/tmpfiles.d/gunicorn.conf

d /run/gunicorn 0755 tyago tyago  -

After i enable both with: 之后,我都启用了:

systemctl enable gunicorn.service
systemctl enable gunicorn.socket
systemctl start gunicorn.service
systemctl start gunicorn.socket

after reboot the server i was able to access stage.nossalista.tk. 重新启动服务器后,我能够访问stage.nossalista.tk。 Great! 大! but the problem was when put the normal site up i make another service in systemd, this time called 但是问题是当我把普通站点放起来时,我在systemd中做了另一个服务,这次叫做

gunicorn-nossalista.tk.service: gunicorn-nossalista.tk.service:

[Unit]
Description=Gunicorn server for www.nossalista.tk
After=network.target

[Service]
User=tyago
Group=www-data
WorkingDirectory=/home/tyago/sites/nossalista.tk/source
ExecStart=/home/tyago/sites/nossalista.tk/virtualenv/bin/gunicorn --workers 3 --bind unix:/tmp/nossalista.tk.socket superlists.wsgi:application
Restart=always

[Install]
WantedBy=multi-user.target

and configured in 并在

/etc/nginx/sites-available/nossalista.tk as: /etc/nginx/sites-available/nossalista.tk为:

[Unit]
Description=Gunicorn server for www.nossalista.tk
After=network.target

[Service]
User=tyago
Group=www-data
WorkingDirectory=/home/tyago/sites/nossalista.tk/source
ExecStart=/home/tyago/sites/nossalista.tk/virtualenv/bin/gunicorn --workers 3 --bind unix:/tmp/nossalista.tk.socket superlists.wsgi:application
Restart=always

[Install]
WantedBy=multi-user.target

my systemctl status of the gunicorn.service , gunicorn.socket and gunicorn-nossalista.tk.serice are all Active 我的gunicorn.service,gunicorn.socket和gunicorn-nossalista.tk.serice的系统状态都处于活动状态

checking my error log of nginx i saw that only my stage file was logging there with the following error: 在检查我的Nginx错误日志时,我看到只有我的舞台文件在那里记录了以下错误:

Nginx error log Nginx错误日志

2016/09/11 21:16:17 [crit] 3173#3173: *39 connect() to unix:/tmp/stage.nossalista.tk.socket failed (2: No such file or directory) while connecting to upstream, client: 189.34.5.XXX, server: stage.nossalista.tk, request: "GET / HTTP/1.1", upstream: "http://unix:/tmp/stage.nossalista.tk.socket:/", host: "stage.nossalista.tk"

ok i change the socket to work with the normal site so probally my error is that i dont have a socket for stage.nossalista ... BUT why i not see any log error message from my normal site in error.log if i configured correct the /etc/nginx/sites-enabled/nossalista.tk ? 好吧,我更改了套接字以与普通站点一起使用,所以可能我的错误是我没有stage.nossalista的套接字...但是,如果我配置正确, 为什么我在error.log 中看不到任何来自于正常站点的日志错误消息 /etc/nginx/sites-enabled/nossalista.tk吗? in access log i got this when i access via normal site(nossalista.tk) 在访问日志中,当我通过普通站点(nossalista.tk)访问时,收到此消息

Access Nginx log: 访问Nginx日志:

189.34.5.XXX - - [11/Sep/2016:21:53:30 +0000] "GET / HTTP/1.1" 400 37 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:48.0) Gecko/20100101 Firefox/48.0"

i know that something is really strange there but cant find why im getting error 400 if i have it in sites-enabled/nossalista.tk all configuration correct pointing to the right socket and all that .... 我知道那里真的很奇怪,但是找不到为什么如果我在sites-enabled / nossalista.tk中遇到错误400时,所有配置正确指向正确的套接字等等。

If you guys help me i would be very great full and i would help anyone with similar errors... i just need some help to get it work! 如果你们能帮助我,我将非常高兴,并且我将帮助任何有类似错误的人……我只需要一些帮助就可以使它正常工作!

my sudo nginx -t: 我的sudo nginx -t:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

i really dont know what to check more .... if you guys have some hints that would be Very Very NICE !!! 我真的不知道该检查更多.....如果你们有一些提示,那将是非常非常好的!!!

i think my problem is in the socket ... probally i need to have 2 sockets but dont know how to do it ! 我认为我的问题出在插座上……可能我需要有2个插座,但不知道该怎么做!

TY and sory for this big big question.... TY,对这个大问题感到抱歉。

It's not clear if you have two servers defined in your nginx.conf (ie, stage.nossalista.tk and nossalista.tk), but from your later comment, it seems like you do. 目前尚不清楚您是否在nginx.conf中定义了两个服务器(例如,stage.nossalista.tk和nossalista.tk),但是从您以后的评论看来,您确实这样做了。

So if both of your gunicorn services are creating sockets, then in your nginx.conf just specify each one as an upstream app server: 因此,如果您的两个gunicorn服务都在创建套接字,则只需在nginx.conf中将每个端口都指定为上游应用服务器:

upstream staging_server {
  server unix:<staging-socket-name> fail_timeout=0;
}

upstream production_server {
  server unix:<production-socket-name> fail_timeout=0;
}

server {
  listen 80;
  server_name stage.nossalista.tk;
  <...>
    proxy_pass http://staging_server; 
  <...>
}

server {
  listen 80;
  server_name nossalista.tk;
  <...> 
    proxy_pass http://production_server; 
  <...>
}

Though I will say that most staging and production environments are on separate servers (droplets). 尽管我会说大多数登台和生产环境都在单独的服务器(液滴)上。 That way the configuration remains the same across the instances. 这样,实例之间的配置将保持不变。 In your case you will need to make sure that you have different database names for your stage and production instances. 在您的情况下,您需要确保您的舞台和生产实例具有不同的数据库名称。

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

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