title: Bad Request (400) and 502 error: Nginx, gunicorn, django
body: <p>I'm trying to deploy my site using nginx, gunicorn, and django. </p>
  
  <p>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. </p>
  
  <p>I've been following these steps from <a href="http://chimera.labs.oreilly.com/books/1234000000754/ch09.html#_trying_it_out" rel="nofollow">Test Driven Development</a>. </p>
  
  <p>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. </p>
  
  <p>My nginx conf file:</p>
  
  <pre><code> 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/;
       }
  
   }
  </code></pre>
  
  <p>Nginx Error log:</p>
  
  <blockquote>
    <p>2015/04/11 18:59:16 [error] 18650#0: *494 connect() to
    unix:/tmp/mysitename.socket failed (111: Connection refused) while
    connecting to upstream</p>
  </blockquote>
  
  <p>My settings.py:</p>
  
  <pre><code> DEBUG = False
  
   TEMPLATE_DEBUG = DEBUG
  
   ALLOWED_HOSTS = [mysitename]
  </code></pre>
  
  <p>When I run gunicorn:</p>
  
  <pre><code> [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
  </code></pre>
  
  <p>Things were working before I decided to switch domains.</p>
  
  <p>Edit whole nginx.conf file</p>
  
  <pre><code>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 / {
  #    }
  #}
  </code></pre>
  
  <p>The include file (from <strong>/etc/nginx/sites-enabled/mysitename</strong>):</p>
  
  <pre><code>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/;
      }
  
  }
  ~                                                                                                                               
  ~ 
  </code></pre>