django nginx 设置问题

django nginx setup issues

下面我尝试从 http://uwsgi-docs.readthedocs.org/en/latest/tutorials/Django_and_nginx.html

配置

以下是我的 conf 文件,位于我的项目目录中,并在 nginx 目录的 sites-enabled 中编辑了相同的文件。

现在,当我访问我的 http://127.0.0.1:8000/app1/files?path=/ 时,我得到 502 Bad Gateway 并且 nginx 错误日志显示

2015/09/21 14:07:41 [error] 8023#0: *7 connect() failed (111: Connection refused) while connecting to upstream, client: 127.0.0.1, server: 127.0.0.1, request: "GET / HTTP/1.1", upstream: "uwsgi://127.0.0.1:8001", host: "127.0.0.1:8000"

但我可以访问这个 link http://127.0.0.1:8000/media/a.jpg 我在这里做错了什么以及如何访问我的 django 应用程序

  # mysite_nginx.conf

  # the upstream component nginx needs to connect to
  upstream django {
      # server unix:///path/to/your/mysite/mysite.sock; # for a file socket
      server 127.0.0.1:8001; # for a web port socket (we'll use this first)
  }

  # configuration of the server
  server {
      # the port your site will be served on
      listen      8000;
      # the domain name it will serve for
      server_name 127.0.0.1; # substitute your machine's IP address or FQDN
      charset     utf-8;

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

      # Django media
      location /media  {
          alias /home/rajeev/django-test/mysite/media;  # your Django project's media files - amend as required
      }

      location /static {
          #alias /path/to/your/mysite/static; # your Django project's static files - amend as required
          alias /home/rajeev/django-test/mysite/static/; # your Django project's static files - amend as required
      }

      # Finally, send all non-media requests to the Django server.
      location / {
          uwsgi_pass  django;
          include /home/rajeev/django-test/mysite/conf/uwsgi_params; # the uwsgi_params file you installed
      }
  }

编辑 1:uwsgi_params 文件

uwsgi_param  QUERY_STRING       $query_string;
uwsgi_param  REQUEST_METHOD     $request_method;
uwsgi_param  CONTENT_TYPE       $content_type;
uwsgi_param  CONTENT_LENGTH     $content_length;

uwsgi_param  REQUEST_URI        $request_uri;
uwsgi_param  PATH_INFO          $document_uri;
uwsgi_param  DOCUMENT_ROOT      $document_root;
uwsgi_param  SERVER_PROTOCOL    $server_protocol;
uwsgi_param  REQUEST_SCHEME     $scheme;
uwsgi_param  HTTPS              $https if_not_empty;

uwsgi_param  REMOTE_ADDR        $remote_addr;
uwsgi_param  REMOTE_PORT        $remote_port;
uwsgi_param  SERVER_PORT        $server_port;
uwsgi_param  SERVER_NAME        $server_name;

您错过了本教程中的一个 point。 nginx 的 uWSGI 配置与在浏览器中检查的配置不同,如果 uWSGI 不仅通过使用的端口工作,而且通过协议工作。

为nginx部署uWSGI时,--http应替换为--socket。通过这个改变,nginx 将正确理解来自你的 uWSGI 服务器的输出,但你的浏览器不会。

浏览器配置(检查uWSGI是否正常工作)也适用于nginx,但在nginx中必须使用proxy_pass而不是uwsgi_pass,不推荐使用,uwsgi协议对于 web 服务器和 uWSGI 服务器之间的通信更快更好。