【问题标题】:django running with gunicorn and nginx: 400 bad request使用 gunicorn 和 nginx 运行的 django:400 错误请求
【发布时间】:2014-10-18 12:36:57
【问题描述】:

我正在尝试使用 nginx 和 gunicorn 部署我的 django 1.6 版项目。在我的服务器中,我添加了我的项目 nginx 文件:

error_log /var/log/nginx/myproject-error.log;
access_log /var/log/nginx/myproject-access.log;
server {
       listen 80;
       server_name <domain_name>;
       root <path_to_my_root_project>;

       location /static/ {
           root <path_to_my_root_project>;
           expires max;
           add_header Pragma public;
          add_header Cache-Control "public, must-revalidate, proxy-revalidate";
      }

      location / {
          proxy_pass_header Server;
          proxy_set_header Host $http_host;
          proxy_redirect off;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Schema $scheme;
          proxy_connect_timeout 10;
          proxy_read_timeout 10;

          proxy_pass http://localhost:8333/;
      }
      error_page 500 502 503 504 /static/50x.html;
}

在我的 django 设置文件中,我设置了静态路径:

# Static
STATIC_ROOT = BASE_DIR + '/static'
STATIC_URL = '/static/'

我运行 gunicorn 如下:

$ gunicorn_django -b localhost:8333
!!!
!!! WARNING: This command is deprecated.
!!!
!!!     You should now run your application with the WSGI interface
!!!     installed with your project. Ex.:
!!!
!!!         gunicorn myproject.wsgi:application
!!!
!!!     See https://docs.djangoproject.com/en/1.5/howto/deployment/wsgi/gunicorn/
!!!     for more info.
!!!

所以当我在我的服务器 curl localhost 上运行 curl 时,我得到了 &lt;h1&gt;Bad Request (400)&lt;/h1&gt; 并且日志没有比 127.0.0.1 - - [18/Oct/2014:14:38:57 +0200] "GET / HTTP/1.1" 400 37 "-" "curl/7.26.0" 更进一步

gunicorn 保持不动,好像没有任何请求发送给它。

有什么想法吗?

更新: 我运行 gunicor:gunicorn myproject.wsgi -b localhost:8333 curl localhost 返回我的 static/50x.html 错误页面。我在 nginx 日志中得到了这个:

2014/10/18 15:06:31 [error] 16037#0: *32 connect() failed (111: Connection refused) while connecting to upstream, client: 127.0.0.1, server: servername.local.fr, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8333/", host: "localhost"

【问题讨论】:

    标签: python django curl nginx gunicorn


    【解决方案1】:

    这个告诉你该怎么做。

    !!!     You should now run your application with the WSGI interface
    !!!     installed with your project. Ex.:
    !!!
    !!!         gunicorn myproject.wsgi:application
    

    NGINX 工作正常,gunicorn 只是没有收到您的请求。

    只需到您的项目目录并使用适当的参数运行 gunicorn,如下所述:http://docs.gunicorn.org/en/latest/run.html#gunicorn-django

    【讨论】:

    • WARNING: This command is deprecated. 我认为它只是被弃用了,因此可以工作。否则会出现错误消息
    • @smarber 你应该尝试使用 siply gunicorn 来运行它。
    • @smarber 你确定 gunicorn 正在运行吗?试试这个:ps aux | grep -i gunicorn ...然后这个:sudo netstat -lputn
    • ps 返回:username 16231 0.0 0.5 50856 12140 pts/0 S+ 15:20 0:00 /home/username/Documents/projects/env/bin/python /home/username/Documents/projects/env/bin/gunicorn myproject.wsgi -b localhost:8333 username 16236 0.0 0.8 59000 16580 pts/0 S+ 15:20 0:00 /home/username/Documents/projects/env/bin/python /home/bilou/Documents/django/projects/env/bin/gunicorn myproject.wsgi -b localhost:8333 username 16319 0.0 0.0 7848 904 pts/8 S+ 15:23 0:00 grep --color=auto -i gunicorn
    • netstat -lputn | grep -i gunicorn 不返回任何内容
    猜你喜欢
    • 2015-06-17
    • 1970-01-01
    • 1970-01-01
    • 2014-02-19
    • 2015-03-31
    • 2021-03-30
    • 1970-01-01
    • 2014-08-03
    • 2015-07-29
    相关资源
    最近更新 更多