django - 如何测试 gunicorn 是否正在工作并与 nginx 通信?

标签 django nginx gunicorn

我正在部署我第一次尝试使用 django+gunicorn+nginx。

  • 我有 django 工作(如果我运行开发服务器,curl -XGET http://127.0.0.0.1:8000 工作正常)。
  • 我让 nginx 处理静态内容(例如,我可以在浏览器中检索 http://example.com/static/my_pic.png)。
  • 我没有从我的网站获得任何 wsgi 内容,也找不到好的故障排除指南(它是否只适用于其他所有人?!)。我使用 supervisor 启动 gunicorn,它报告它确实在运行:

  • (在外壳中:)
    supervisorctl status my_app
    my_app                   RUNNING    pid 1002, uptime 0:29:51
    

    这是我用来启动它的样板脚本:
    #!/bin/bash
    #script variables
    NAME="gunicorn_myapp"                      # Name of process
    DJANGODIR=/webapps/www/my_project          # Django project directory
    SOCKFILE=/webapps/www/run/gunicorn.sock    # communicte using this socket
    USER=app_user                              # the user to run as
    GROUP=webapps                              # the group to run as
    NUM_WORKERS=3
    DJANGO_SETTINGS_MODULE=my_project.settings # settings file
    DJANGO_WSGI_MODULE=my_project.wsgi         # WSGI module name
    
    # Activate the virtual environment
    cd $DJANGODIR
    source ../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
    
    exec ../bin/gunicorn ${DJANGO_WSGI_MODULE}:application \
      --name $NAME \
      --workers $NUM_WORKERS \
      --user=$USER --group=$GROUP \
      --bind=unix:$SOCKFILE
    

    这是(压缩的)nginx配置文件:
    upstream my_server {
      server unix:/webapps/www/run/gunicorn.sock fail_timeout=10s;
    }
    server {
            listen 80;
            server_name www.example.com;
            return 301 $scheme://example.com$request_uri;
    }
    server {
        listen   80;
        server_name example.com;
        client_max_body_size 4G;
        access_log /webapps/www/logs/nginx-access.log;
        error_log /webapps/www/logs/nginx-error.log;
    
        location /favicon.ico { access_log off; log_not_found off; }
    
        location /static/ {
            autoindex on;
            alias   /webapps/www/my_project/my_app/static/;
        }
    
        location /media/ {
            autoindex on;
            alias   /webapps/www/my_project/my_app/media/;
        }
    
        location / {
            proxy_pass         http://my_server;
            proxy_redirect     off;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto https;
            proxy_set_header Host $http_host;
            proxy_redirect off;
            if (!-f $request_filename) {
                proxy_pass http://example.com;
                break;
            }
        }
    
        location  /robots.txt {
            alias /webapps/www/my_project/my_app/static/robots.txt ;
        }
        # Error pages
        error_page 500 502 503 504 /500.html;
        location = /500.html {
            root /webapps/www/my_project/my_app/static/;
        }
    }
    

    所以:gunicorn 正在运行,nginx 正在运行......我应该执行哪些测试(以及如何?)以确定 gunicorn 是否正确地执行 wsgi 的工作(以及 nginx 是否正确地代理了上述内容)?

    编辑:我已将问题缩小到 gunicorn 和 nginx 通过 unix 套接字进行的通信。如果我将 $SOCKFILE 更改为绑定(bind)到 0.0.0.0:80并停止 nginx,然后应用程序的页面从我的网站提供。坏消息是两个 conf 文件之间的套接字文件字符串完全相同,所以我不知道它们为什么不通信。我想这意味着 nginx 没有正确地获取和传递数据?

    最佳答案

    进入项目目录:

    cd projectname
    gunicorn --log-file=- projectname.wsgi:application
    


    sudo systemctl status gunicorn
    

    关于django - 如何测试 gunicorn 是否正在工作并与 nginx 通信?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36543145/

    相关文章:

    google-app-engine - nginx 代理太慢

    python - Bjoern v/s Gunicorn POST 请求

    django - docker+gunicorn+nginx需要supervisord吗?

    python - Django 请求 XML 文件

    python - Django - 设置具有许多相似字段的模型的最佳方法是什么?

    python - 通过 sendgrid-python API lib 将 django 对象上下文传递给 sendgrid 电子邮件

    python - flask +uwsgi+nginx : When is memory released?

    django - 如何在 Django 中设置 SelectDateWidget 的 MaxDate 和 MinDate?

    php - 使用 VestaCP 在 Centos 7 上将 Payeezy FirstData 自签名证书添加到服务器 Trusted Store CA

    python - 当客户端连接不稳定时,在 heroku 上运行的 django 中的工作人员卡在岗位上