Hide
Hide

Reputation: 3317

Django with nginx connection error

I'm just making webserver with django.

Now, I want to publish Django by uwsgi+Nginx, So I read some documents(http://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html). While following that doc, I met some errors.

When I connect to mydomain.com:8000, It throws 502 Bad Gateway error. (Actually, when I worked, changed mydomain.com to real domain that I have)

After error, /var/log/nginx/error.log is in below.

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

This is my configure files.

[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
   }
}

(I made that conf file in my django project's folder and linked to /etc/nginx/sites-enabled)

How can I connect to my server?

I can't find where is error occured.

Thanks.

Upvotes: 1

Views: 2227

Answers (2)

ddiazp
ddiazp

Reputation: 647

Your Nginx configuration is correct, so let's take a look at your uwsgi configuration.

First of all, I assume you have installed uwsgi system-wide via apt-get, yum, etc.

The next thing you have to install (system-wide) is uwsgi-plugin-python3 (uwsgi-plugin-python if you are planning to execute Django with python2.7, what I don't recommend)

Then, you can create an ini file with the all the uwsgi configuration:

[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

Then, execute uwsgi:

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

If you have installed uwsgi via apt-get or yum, you have to create the ini file in /etc/uwsgi/apps-enabled/yourproject.ini and simply execute uwsgi using:

:# service uwsgi start|restart

Finally, there are a lot of options to configure uwsgi: number of processes, threads, logs, and a lot of very interesting (and bad documented) stuff.

I hope it helps ;)

Upvotes: 2

A.Raouf
A.Raouf

Reputation: 2320

at /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;
}

then at /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/;

                }

        }

then try this command

$ 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

Upvotes: 0

Related Questions