title: Nginx, Flask, Gunicorn 502 Error
body: <p>I'm still pretty new with Flask/Nginx/Gunicorn as this is only my second site using the combination. I created a website based off of Miguel Grinberg's <a href="http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world" rel="nofollow">tutorial</a> so my file structure is exactly the same as in this tutorial.</p>
  
  <p>In my previous Flask app, my app was in a single file called <code>app.py</code> so when I used Gunicorn I just called <br>
  <code>gunicorn app:app</code><br></p>
  
  <p>Now that my new application is split into multiple files I used a file <code>run.py</code> to start the app, but I'm not sure how I should call Gunicorn now. I've read other questions and tutorials but they haven't worked. When I run <code>gunicorn run:app</code> and I try to access the site I get a 502 Bad Gateway error.</p>
  
  <p>I think my problem is more Gunicorn than Nginx or Flask, since the site works if I just type <code>./run.py</code>. In any case, I've included my Nginx config and a few other files below. Thanks so much for your help!</p>
  
  <p>File: <code>run.py</code></p>
  
  <pre><code>#!flask/bin/python
  from app import app
  from werkzeug.contrib.fixers import ProxyFix
  
  app.wsgi_app = ProxyFix(app.wsgi_app)
  app.run(debug = True, port=8001)
  </code></pre>
  
  <p>File: <code>app/views.py</code></p>
  
  <pre><code>from app import app
  
  @app.route('/')
  @app.route('/index')
  def index():
      posts = Post.query.order_by(Post.id.desc()).all()
      return render_template('index.html', posts=posts)
  </code></pre>
  
  <p>File: <code>nginx.conf</code></p>
  
  <pre><code>server {
      listen 80;
      server_name example.com;
  
      root /var/www/example.com/public_html/app;
  
      access_log /var/www/example.com/logs/access.log;
      error_log /var/www/example.com/logs/error.log;
  
      client_max_body_size 2M;
  
      location / {
          try_files $uri @gunicorn_proxy;
      }
  
      location @gunicorn_proxy {
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header Host $http_host;
          proxy_redirect off;
          proxy_pass http://127.0.0.1:8001;
      }
  }
  </code></pre>