Django与nginx连接错误

时间:2018-02-20 06:15:55

标签: django nginx uwsgi

我只是用django制作网络服务器。

现在,我想通过uwsgi + Nginx发布Django,所以我读了一些文档(http://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html)。 在遵循该文档的同时,我遇到了一些错误。

当我连接到mydomain.com:8000时,它会抛出502 Bad Gateway错误。 (实际上,当我工作时,将mydomain.com更改为我拥有的真实域名)

发生错误后,/ var / log / nginx / error.log位于下方。

2018/02/20 14:56:15 [error] 7548#7548: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.30.1.254, server: mydomain.com, request: "GET / HTTP/1.1", upstream: "uwsgi://127.0.0.1:8001", host: "mydomain.com:8000" ^C

这是我的配置文件。

[project_rest.conf]

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 .mydomain.com; # 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/app/project_rest/media;
   }

   location /static {
       alias /home/app/project_rest/static;
   }

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

(我在我的django项目文件夹中创建了conf文件并链接到/ etc / nginx / sites-enabled)

如何连接我的服务器?

我无法找到错误发生的位置。

感谢。

2 个答案:

答案 0 :(得分:1)

您的Nginx配置是正确的,所以让我们来看看您的uwsgi配置。

首先,我假设您已通过uwsgiapt-get等系统安装了yum

你需要安装的下一件事(全系统)是uwsgi-plugin-python3uwsgi-plugin-python如果你打算用python2.7执行Django,我不推荐)

然后,您可以使用所有uwsgi配置创建一个ini文件:

[uwsgi]
socket = 127.0.0.1:8001

uid = execuser

; Normally nginx, www-data
gid = nginx

chdir = /absolute/path/to/your/project

; Assuming your wsgi module is in chdir/yourmainapp/wsgi.py
module = yourmainapp.wsgi

; Path to your virtualenv. If you are not using virtualenv,
; you should.
home = /absolute/path/to/your/virtualenv

; Enables plugins: python
plugins = python

; Deamonize app
master = true

; Pass django settings module as environment variable
; (it is expected by Django).
; Assuming your settings is in chdir/yourmainapp/settings.py
env = DJANGO_SETTINGS_MODULE=yourmainapp.settings

然后,执行uwsgi:

:# /path/to/uwsgi --ini /path/to/your/config.ini --daemonize /path/to/your/logs

如果您通过apt-get或yum安装了uwsgi,则必须在/etc/uwsgi/apps-enabled/yourproject.ini中创建ini文件,然后使用以下命令执行uwsgi:

:# service uwsgi start|restart

最后,配置uwsgi有很多选项:进程数,线程数,日志数以及许多非常有趣(以及记录不良)的内容。

我希望它有所帮助;)

答案 1 :(得分:0)

/etc/nginx/default.d/xxxx

upstream django {

    server 127.0.0.1:9000; # for a web port socket (we'll use this first)
    keepalive 32;
}

然后在/etc/nginx/nginx.conf

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

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;

    include /etc/nginx/default.d/*;

# Settings for a TLS enabled server.
#
    server {
    listen 80;
        listen [::]:80 default_server;
        server_name ip;
        root         /path_prj/;
        server_tokens off;

        error_log /var/log/bill_error.log;

        access_log /var/log/bill_access.log;
        resolver 8.8.8.8 8.8.4.4 valid=300s;
        resolver_timeout 5s;
        location / {
                uwsgi_read_timeout 100;

                uwsgi_pass  django;

                include     /var/www/html/uwsgi_params; # the uwsgi_params file you installed

            }
    location /media/  {
                internal;
                root /path_proj/;

            }


    location /static/ {
                root /path_proj/;

                }

        }

然后尝试此命令

$ sudo uwsgi -s :9000 -M --env DJANGO_SETTINGS_MODULE=sharing.settings --chdir /path_proj/ -w "django.core.wsgi:get_wsgi_application()" --chmod-socket=666 --enable-threads --thunder-lock --daemonize /tmp/uwsgi.log --workers 10 -b 32768

相关问题