【问题标题】:setting up nginx with django使用 django 设置 nginx
【发布时间】:2016-09-18 08:27:42
【问题描述】:

我在 /home/ubuntu/test 目录中创建了一个名为“yo”的 django 项目。

这些是我的 nginx 文件:

nginx/sites-available/哟

    server {
        listen 80;
        server_name 52.89.220.11;

        location = /favicon.ico { access_log off; log_not_found off; }
        location /static/ {
            root /home/ubuntu/test/yo/static;
        }

        location / {
            include uwsgi_params;
            uwsgi_pass unix:/home/ubuntu/test/yo/yo.sock;
        }
    }

nginx/sites-enabled/yo_nginx.conf:

# mysite_nginx.conf

# the upstream component nginx needs to connect to
upstream django {
    # server unix:///home/Ubuntu/test/yo/yo.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      80;
    # the domain name it will serve for
    server_name 52.89.220.11; # 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 /path/to/your/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
  }

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

/home/ubuntu/test/yo/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;

有我的文件及其网址。当我转到端口 80 上的 domain_name 时,我得到 502 bad gateway 而不是 django 成功页面。请帮忙。

【问题讨论】:

  • 为什么你有 2 个不同的 nginx 配置文件?为什么你有一个不在服务器块中的位置块?
  • 我按照他们所说的做了一些链接。也许你可以帮忙。
  • 你应该使用更好的链接,sites_enabled 中的文件应该与sites_available 中的文件相同,并且位置块属于服务器块。已经修复了,如果总是有问题发布 nginx 和 uwsgi 的错误消息。
  • sites-enabled 和 sites-available 完全相同,并且 location 块仅在 server 块内。可能是发在这里的时候搞砸了。顺便说一句,我关注了这个链接uwsgi.readthedocs.io/en/latest/tutorials/Django_and_nginx.html
  • @polku 我的配置文件正确吗?

标签: python django nginx


【解决方案1】:

首先安装所需的应用程序。 sudo apt-get install nginx uwsgi uwsgi-plugin-python python-virtualenv 将使用的软件包版本:

Nginx
Uwsgi 
Virtualenv
Django 
Virtualenv.

我将我的项目存储在 ~/projects.现在我正在为我的项目创建 python 虚拟环境并安装 Django。

cd ~/projects/
virtualenv example.com
cd example.com
source bin/activate
pip install django
django-admin.py startproject project

Nginx 配置。 恕我直言,默认情况下,nginx 配置用于基本任务。我不会在此条目中更改此配置。配置文件存放在

/etc/nginx/sites-available. Go to this directory and create a new file.
cd /etc/nginx/sites-available

vim example.com 这是示例配置。

server {
    listen  80;
    server_name eample.com,www.example.com;
    access_log /var/log/nginx/eample.com_access.log;
    error_log /var/log/nginx/example.com_error.log;

    location / { 
        uwsgi_pass  unix:///tmp/example.com.sock;
        include     uwsgi_params;
    }

    location /media/  {
        alias /home/eshlox/projects/example.com/project/project/media/;
    }

    location  /static/ {
        alias  /home/eshlox/projects/example.com/project/project/static/;
    }
}

我们必须创建符号链接来启用它。

cd /etc/nginx/sites-enabled

ln -s ../sites-available/example.com .

Uwsgi。 与 Nginx 一样。配置文件存储在 /etc/uwsgi/apps-available 中。转到此目录并创建一个新文件。

cd /etc/uwsgi/apps-available
vim example.com.ini

编辑 example.com.ini [uwsgi]

vhost = true
plugins = python
socket = /tmp/example.com.sock
master = true
enable-threads = true
processes = 2
wsgi-file = /home/eshlox/projects/example.com/project/project/wsgi.py
virtualenv = /home/eshlox/projects/example.com
chdir = /home/eshlox/projects/example.com/project
touch-reload = /home/eshlox/projects/example.com/project/reload

启用此功能。

cd /etc/uwsgi/apps-enabled/
ln -s ../apps-available/example.com.ini .
That's all. Now, run this services.
sudo service nginx start
sudo service uwsgi start

【讨论】:

    【解决方案2】:
    • nginx/sites-enabled/yo_nginx.conf 中有不匹配的括号
    • nginx/sites-available/yo 不被 nginx 读取 - 只有以 .conf 结尾且位于 nginx/sites-enabled/ 的文件在默认 nginx 配置中被读取。
    • 由于你的命令行uwsgi --socket mysite.sock --module mysite.wsgi --chmod-socket=664 告诉uwsgi 监听unix 套接字,你的upstream django 块应该相应地修改。

    这应该可以消除 502 错误。

    upstream django {
        server unix:///home/ubuntu/test/yo/mysite.sock; # for a file socket, check if the path is correct
        # server 127.0.0.1:8001; # for a web port socket
    }
    
    # configuration of the server
    server {
        # the port your site will be served on
        listen      80;
        # the domain name it will serve for
        server_name 52.89.220.11; # 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 /path/to/your/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
        # }
    
    # Finally, send all non-media requests to the Django server.
        location / {
            uwsgi_pass  django;
            include     /home/ubuntu/test/yo/uwsgi_params; # the uwsgi_params file you installed
        }
    }
    

    【讨论】:

    • 我会在几分钟内通知您。
    • 这是错误日志所说的:2016/09/18 11:44:30 [error] 10853#0: *65 connect() failed (111: Connection refused) while connection to upstream ,客户端:103.61.255.51,服务器:52.89.220.11,请求:“GET /favicon.ico HTTP/1.1”,上游:“uwsgi://127.0.0.1:8001”,主机:“52.89.220.11”,推荐人: "52.89.220.11"
    • 你是不是在编辑完配置文件后重启了nginx?因为日志说它仍在尝试使用 web 端口(如您原来的yo_nginx.conf)而不是 unix 套接字进行连接。
    • 我在我的项目中更改了 .sock 文件的权限级别,并且 test.py 文件可以在 nginx 上优雅地运行,但是当我尝试使用以下命令执行它时:sudo uwsgi --socket mysite.sock - -module mysite.wsgi --chmod-socket=666,它再次给出了一个错误,说连接被拒绝,我这次确实重新启动了我的服务器,它指向了正确的位置。
    • 没关系它工作:D 这是一个权限级别的问题。谢谢:)
    猜你喜欢
    • 2013-05-03
    • 1970-01-01
    • 2023-03-06
    • 2022-01-24
    • 2020-01-25
    • 2015-12-17
    • 2015-05-05
    • 2017-01-19
    • 2017-11-19
    相关资源
    最近更新 更多