您的位置:首页 > 运维架构 > Nginx

nginx+gunicorn+django

2016-12-23 23:33 344 查看
1下载(略过)

2配置gunicorn:

2.1 编写gunicorn文件:

  gunicorn.sh

#!/bin/bash
NAME="travel"                              #Name of the application (*)
DJANGODIR=/path/to/app            # Django project directory (*)
SOCKFILE=/path/to/app/run/gunicorn.sock    # we will communicate using this unix socket (*)
NUM_WORKERS=3                          # how many worker processes should Gunicorn spawn (*)
DJANGO_SETTINGS_MODULE=travel.settings         # which settings file should Django use (*)
DJANGO_WSGI_MODULE=travel.wsgi                     # WSGI module name (*)

echo "Starting $NAME as `whoami`"

# Activate the virtual environment
cd $DJANGODIR
source /path/to/app/venv/bin/activate
export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE
PATHONPATH=/path/to/venv/bin/python3
export PYTHONPATH

# Create the run directory if it doesn't exist
RUNDIR=$(dirname $SOCKFILE)
test -d $RUNDIR || mkdir -p $RUNDIR

# Start your Django Unicorn
# Programs meant to be run under supervisor should not daemonize themselves (do not use --daemon)
exec /path/to/app/venv/bin/gunicorn ${DJANGO_WSGI_MODULE}:application \
--name $NAME \
--workers $NUM_WORKERS \
--bind=unix:$SOCKFILE  \
--log-level=debug
#  --user $USER
#  --bind=unix:$SOCKFILE


3配置nginx

3.1修改/etc/nginx/nginx.conf:

删除其中关于server的定义,保留include;

在/etc/nginx/conf.d中添加自定义文件 app_nginx.conf

upstream test_server {
server unix:/path/to/app/run/gunicorn.sock fail_timeout=10s;
}

# This is not neccessary - it's just commonly used
# it just redirects example.com -> www.example.com
# so it isn't treated as two separate websites

server {
listen 80;
server_name example.com;
return 301 $scheme://www.example.com$request_uri;
}
server {
listen 80;
server_name www.example.com;
client_max_body_size 4G;
access_log /path/to/app/logs/nginx-access.log;
error_log /path/to/app/logs/nginx-error.log warn;

location /static/ {
autoindex on;
alias   /path/to/app/static/;
}

location /media/ {
autoindex on;
alias   /path/to/app/media/;
}

location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;

if (!-f $request_filename) {
proxy_pass http://test_server; break;
}
}

#For favicon
location  /favicon.ico {
alias /path/to/app/static/img/favicon.ico;
}
#For robots.txt
location  /robots.txt {
alias /path/to/app/static/robots.txt ;
}
# Error pages
error_page 500 502 503 504 /500.html;
location = /500.html {
root /path/to/app/static/;
}
}


3.2

重新载入nginx.conf并重启服务:

nginx -s reload


service nginx restart


如果报错,错误为:

open() “/somewhere” failed (13: Permission denied)


需要检查当前SELinux mode:

getenforce


如果显示
Enforcing
,则通过输入
setenforce 0
将mode设置为Permissive

然后重新执行3.2步骤,不出意外的话则可以正常运行!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: