wxcoder
wxcoder

Reputation: 655

Bad Request (400) and 502 error: Nginx, gunicorn, django

I'm trying to deploy my site using nginx, gunicorn, and django.

When I run gunicorn and load the page at first I was getting a 502 Bad gateway error then I switch the server name to the IP address of my server and now I get a Bad Request 400 error or the domain is unable to be found.

I've been following these steps from Test Driven Development.

I realized last night that I was using my staging server to update my live domain instead of a staging domain. So I created a staging domain as a subdomain of the live domain and created a separate directory for it, then git pulled down the work I had done previously, but it's not working.

My nginx conf file:

 server {
     listen 80;
     server_name my-server-ip-address;

     location / {
          proxy_set_header Host $host;
          proxy_pass http://unix:/tmp/mysitename.socket;
     }

      location /static {
         autoindex on;
         root /home/cmac/sites/mysitename/;
     }

 }

Nginx Error log:

2015/04/11 18:59:16 [error] 18650#0: *494 connect() to unix:/tmp/mysitename.socket failed (111: Connection refused) while connecting to upstream

My settings.py:

 DEBUG = False

 TEMPLATE_DEBUG = DEBUG

 ALLOWED_HOSTS = [mysitename]

When I run gunicorn:

 [2015-04-11 20:40:39 +0000] [4174] [INFO] Starting gunicorn 19.3.0
 [2015-04-11 20:40:39 +0000] [4174] [INFO] Listening at:      http://127.0.0.1:8000 (4174)
 [2015-04-11 20:40:39 +0000] [4174] [INFO] Using worker: sync
 [2015-04-11 20:40:39 +0000] [4177] [INFO] Booting worker with pid: 4177

Things were working before I decided to switch domains.

Edit whole nginx.conf file

user  cmac;
worker_processes  1;


error_log  /var/log/nginx/error.log;
#error_log  /var/log/nginx/error.log  notice;
#error_log  /var/log/nginx/error.log  info;

pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    include       /etc/nginx/sites-enabled/mysitename;
    default_type  application/octet-stream;

    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;

        #keepalive_timeout  0;
        keepalive_timeout  65;

        #gzip  on;

        # Load modular configuration files from the /etc/nginx/conf.d directory.
        # See http://nginx.org/en/docs/ngx_core_module.html#include
        # for more information.
        include /etc/nginx/conf.d/*.conf;

        index   index.html index.htm;

        server {
            listen       80;
            server_name  localhost;
            root         /usr/share/nginx/html;

            #charset koi8-r;

            #access_log  /var/log/nginx/host.access.log  main;

location / {
        }

        # redirect server error pages to the static page /40x.html
        #
        error_page  404              /404.html;
        location = /40x.html {
        }

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;
    #    root         html;


 #    location / {
#    }
#}


# HTTPS server
#
#server {
#    listen       443;
#    server_name  localhost;
#    root         html;

#    ssl                  on;
#    ssl_certificate      cert.pem;
#    ssl_certificate_key  cert.key;

#    ssl_session_timeout  5m;

#    ssl_protocols  SSLv2 SSLv3 TLSv1;
#    ssl_ciphers  HIGH:!aNULL:!MD5;
#    ssl_prefer_server_ciphers   on;

#    location / {
#    }
#}

The include file (from /etc/nginx/sites-enabled/mysitename):

server {
    listen 127.0.0.1;
    server_name my-server-ip-address;

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_pass http://unix:/tmp/mysitename.socket;

    }

     location /static {
        autoindex on;
        root /home/cmac/sites/mysitename/;
    }

}
~                                                                                                                               
~ 

Upvotes: 1

Views: 3401

Answers (1)

Anzel
Anzel

Reputation: 20553

In mysitename you need to listen on port 80, and server_name as your your staging domain like staging.example.com, also do not use unix sock at the moment, put http://127.0.0.1:8000 in proxy_pass as where your gunicorn serves. Try also comment out the server block in your nginx.conf, it has conflicts with your mysitename.

Also, are you sure user cmac has permissions under your directory/files? normally it runs on www-data.

Hope this helps.

Upvotes: 0

Related Questions