Django site not accessible after gunicorn started

与世无争的帅哥 提交于 2019-12-23 23:17:36

问题


I'm trying to start a gunicorn server with a shell script. The problem is that a site is not accessible (connection times out when I try to access it via browser). I have here 2 scripts, one which works and one which doesn't. I'm trying to understand why the first script doesn't work and how to fix the problem.

First sript (doesn't work; connection timeout when I try to access the site at domain.com:8000; no errors in the logs):

#!/bin/bash
  set -e
  LOGFILE=/var/log/gunicorn/hello.log
  LOGDIR=$(dirname $LOGFILE)
  NUM_WORKERS=3

  USER=lumiawor
  GROUP=lumiawor

  cd /home/lumiawor/public_html/test/hello
  source /home/lumiawor/public_html/test/bin/activate
  test -d $LOGDIR || mkdir -p $LOGDIR

  gunicorn_django -w $NUM_WORKERS \
      --user $USER --group $GROUP --log-level debug \
      --log-file $LOGFILE 2>>$LOGFILE

Second script (works; site loads at domain.com:8000):

#!/bin/bash
  set -e
  LOGFILE=/var/log/gunicorn/hello.log
  LOGDIR=$(dirname $LOGFILE)
  NUM_WORKERS=3

  USER=lumiawor
  GROUP=lumiawor

  cd /home/lumiawor/public_html/test/hello
  source /home/lumiawor/public_html/test/bin/activate
  test -d $LOGDIR || mkdir -p $LOGDIR

  gunicorn_django -w $NUM_WORKERS -b 0.0.0.0:8000
      --user $USER --group $GROUP --log-level debug \
      --log-file $LOGFILE 2>>$LOGFILE

The only difference is at the gunicorn_django line. I'm not actually sure what the backslash means. Does it tell gunicorn to run on the default address?


回答1:


The blackslashes are just a way of continuing a single command to the next line.

The key here is -b 0.0.0.0:8000 which is telling gunicorn to serve on all your network interfaces. Without the -b parameter, it will bind to 127.0.0.1:8000 and only serve requests coming from the local machine.

Most likely, -b 0.0.0.0:8000 is only what you want if gunicorn is handling requests directly from the outside world. If you are using nginx or some other reverse proxy server (a common setup) and only want gunicorn to serve requests coming from the same machine, you'll want to use the first script.

The typical setup goes like this:

  • User -> nginx listening on 0.0.0.0:80 or public ip -> gunicorn listening on 127.0.0.1:8000

By the way, you can see a list of your machine's available interfaces with the ifconfig command.



来源:https://stackoverflow.com/questions/14697291/django-site-not-accessible-after-gunicorn-started

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!