【问题标题】:Django on Nginx help neededNginx 上的 Django 需要帮助
【发布时间】:2018-04-24 05:50:41
【问题描述】:

我正在尝试将我的网站部署到 aws ec2。它在 python/django 中,我想学习如何自己部署网站。我对 aws 的 EBS 有一些问题,所以首先我想知道如何手动完成。 我决定为此使用 gunicorn 和 nginx。

我可以在虚拟环境中使用 gunicorn 运行网站,并在 /home/ec2-user/gunicorn_start.bash 中创建了以下脚本:

#!/bin/bash

NAME="davidbiencom"                                  # Name of the 
application
DJANGODIR=/home/ec2-user/davidbien             # Django project directory
SOCKFILE=/home/ec2-user/virtual/run/gunicorn.sock
USER=ec2-user                                     
GROUP=ec2-user                                    
NUM_WORKERS=3                                     
DJANGO_SETTINGS_MODULE=davidbiencom.settings             
DJANGO_WSGI_MODULE=davidbiencom.wsgi                     

echo "Starting $NAME as `whoami`"

# Activate the virtual environment
cd $DJANGODIR
source /home/ec2-user/virtual/bin/activate
export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE
export PYTHONPATH=$DJANGODIR:$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$
exec gunicorn ${DJANGO_WSGI_MODULE}:application \
--name $NAME \
--workers $NUM_WORKERS \
--user=$USER --group=$GROUP \
--bind=unix:$SOCKFILE \
--log-level=debug \
--log-file=-

我相信这运行良好,因为没有错误。 接下来我安装 nginx 并启动服务。当我得到欢迎页面时,我确认它正在运行。接下来我执行以下操作:

  1. 转到 /etc/nginx/nginx.conf 并将以下内容添加到 http

    包括/etc/nginx/sites-enabled/*.conf;

然后我在 /etc/nginx/sites-available 和 sites-enabled 中创建两个文件夹。 我创建一个文件 davidbien.conf 并在里面输入以下内容(更新):

upstream app_server {
# fail_timeout=0 means we always retry an upstream even if it failed
# to return a good HTTP response

# for UNIX domain socket setups
server unix:/home/ec2-user/virtual/run/gunicorn.sock fail_timeout=0;

# for a TCP configuration
# server 192.168.0.7:8000 fail_timeout=0;
}

server {
listen 80;
server_name 35.176.185.50;

#Max upload size
client_max_body_size 75M;   # adjust to taste

location /static/ {
    root /home/ec2-user/davidbien/static;
}

location / {
  # checks for static file, if not found proxy to app
  try_files $uri @proxy_to_app;
}

location @proxy_to_app {
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  # enable this if and only if you use HTTPS
  # proxy_set_header X-Forwarded-Proto https;
  proxy_set_header Host $http_host;
  # we don't want nginx trying to do something clever with
  # redirects, we set the Host: header above already.
  proxy_redirect off;
  proxy_pass http://app_server;
  }
}

我保存此文件并运行以下命令:

ln -s /etc/nginx/sites-available/davidbiencom.conf /etc/nginx/sites-enabled/davidbiencom.conf

完成此操作后,我重新启动 nginx,当我输入 IP 地址时,我收到 502 bad gateway 错误。

这里可能有什么问题? 谢谢。

编辑: 这是来自 var/log/nginx/error.log 的错误日志

2017/11/10 22:26:27 [error] 27620#0: *1 open() "/usr/share/nginx/html/favicon.iicon.ico" failed (2: No such file or directory), client: 2.96.149.96, server: $localhost, request: "GET /favicon.ico HTTP/1.1", host: "35.176.185.50",referrer: "http://35.176.185.50/"

编辑 2: 这是 etc/nginxnginx/conf 文件:

http {
log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';

access_log  /var/log/nginx/access.log  main;

sendfile            on;
tcp_nopush          on;
tcp_nodelay         on;
keepalive_timeout   65;
types_hash_max_size 2048;

#include             /etc/nginx/mime.types;
#default_type        application/octet-stream;

# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/sites-enabled/*.conf;

index   index.html index.htm;

server {
    listen       80 default_server;
    listen       [::]:80 default_server;
    server_name  localhost;
    root         /usr/share/nginx/html;

    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;

    location / {

# redirect server error pages to the static page /40x.html
    #
    error_page 404 /404.html;
        location = /40x.html {
    }

    # redirect server error pages to the static page /50x.html
    #
error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}
 # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}
# deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

【问题讨论】:

    标签: python django amazon-web-services nginx amazon-ec2


    【解决方案1】:

    您告诉 nginx 它必须将请求转发到 127.0.0.1:3031,但是从我从脚本中看到的启动 gunicorn 的内容来看,如果您要在 127.0.0 上启动 gunicorn 工作者,gunicorn 会绑定到一个套接字。 0.1:3031 它应该可以工作

    【讨论】:

    • 现在我想起来我不需要那个部分,是吗?你觉得可以删吗?删除该行后,我得到了 403 禁止。
    • 如果你删除 gunicorn,我认为它会在 8000 端口启动
    • 但是 nginx 不应该将所有进入端口 80 的流量引导到该应用程序吗?那我应该把它设为 0.0.0.0:80 吗?我看到有些教程没有那个设置。
    • Nginx 在端口 80 上接收流量并将其转发给监听地址的 gunicorn worker。现在我再次阅读,我认为您的 Nginx 配置无论如何都不起作用。您正在运行 gunicorn 但您正在使用 nginx.conf 中的 uwsgi 配置 看看这个以使用 nginx 和 gunicorn docs.gunicorn.org/en/stable/deploy.html 运行您的应用程序
    • 仍然无法正常工作,我不确定应该在哪里更改。
    猜你喜欢
    • 1970-01-01
    • 2022-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-14
    相关资源
    最近更新 更多