title: How to fix Internal server error Flask nginx
body: <p>Im having a problem with my Flask application
  When I login to my account I sometimes get an internal server error and sometimes not. But then I get it later on another page. Is this due to my SQL requests?</p>
  
  <p>App.py</p>
  
  <pre><code>from flask import *
  import pymysql.cursors, os, hashlib
  from werkzeug.security import generate_password_hash, check_password_hash
  from werkzeug.utils import secure_filename
  from datetime import date
  
  today = date.today()
  
  app = Flask(__name__)
  app.secret_key = os.urandom(24)
  UPLOAD_FOLDER = 'static/uploads'
  ALLOWED_EXTENSIONS = set(['jpeg', 'jpg', 'png', 'gif'])
  app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
  
  con = pymysql.connect(host='xxx', port=xxx, user='xxx', password='xxx', db='xxx')
  
  def getLoginDetails():
      if 'email' in session:
          loggedIn = False
          username = ''
          firstname = ''
          uuid = ''
          function = ''
      else:
          loggedIn = True
          with con.cursor() as cur:
              cur.execute('SELECT username, uuid, firstname, function FROM users WHERE email="' + session['email'] + '"')
              username, uuid, firstname, function = cur.fetchone()
          con.close()
      return (loggedIn, username, uuid, firstname, function)
  
  @app.route('/')
  def index():
      if 'email' in session:
          loggedIn, username, uuid, firstname, function = getLoginDetails()
          with con.cursor() as cur:
              cur.execute("SELECT * FROM unban ORDER BY id DESC LIMIT 3")
              unbans = cur.fetchall()
          con.close()
          with con2.cursor() as cur2:
              cur2.execute("SELECT * FROM data")
              data = cur2.fetchall()
          con2.close()
          return render_template('index.html', loggedIn=loggedIn, username=username, uuid=uuid, firstname=firstname, function=function, unbans=unbans, data=data)
      else:
          return redirect(url_for('login'))
  
  @app.route('/login')
  def login():
      if 'email' in session:
          return redirect(url_for('index'))
      else:
          return render_template('login.html', error='')
  
  @app.route('/loginForm', methods=['GET', 'POST'])
  def loginForm():
      if request.method == 'POST':
          email = request.form['email']
          password = request.form['password']
          if is_valid(email, password):
              session['email'] = email
              return redirect(url_for('index'))
          else:
              error = 'Verkeerde inlog gegevens.'
              return render_template('login.html', error=error)
  
  def is_valid(email, password):
      with con.cursor() as cur:
          cur.execute(f"SELECT email, password from users")
          data = cur.fetchall()
          for row in data:
              if row[0] == email and row[1] == hashlib.md5(password.encode()).hexdigest():
                  return True
              return False
      con.close()
  
  @app.route('/bans')
  def bans():
      if 'email' in session:
          loggedIn, username, uuid, firstname, function = getLoginDetails()
          with con.cursor() as cur:
              cur.execute("SELECT * FROM unban ORDER BY id DESC")
              unbans = cur.fetchall()
          con.close()
          return render_template('bans.html', loggedIn=loggedIn, username=username, uuid=uuid, firstname=firstname, function=function, unbans=unbans)
      else:
          return redirect(url_for('login'))
  
  
  if __name__ == '__main__':
      app.run()
  
  </code></pre>
  
  <p>Nginx config:</p>
  
  <pre><code>server {
          server_name staff.buddiedj.nl;
          location / {
                  proxy_pass http://127.0.0.1:8001;
                  proxy_set_header Host $host;
                  proxy_set_header X-Real-IP $remote_addr;
                  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  
                  client_max_body_size 5M;
          }
  
      listen 443 ssl; # managed by Certbot
      ssl_certificate /etc/letsencrypt/live/staff.buddiedj.nl/fullchain.pem; # managed by Certbot
      ssl_certificate_key /etc/letsencrypt/live/staff.buddiedj.nl/privkey.pem; # managed by Certbot
      include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
      ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
  
  }
  server {
      if ($host = staff.buddiedj.nl) {
          return 301 https://$host$request_uri;
      } # managed by Certbot
  
  
          listen 80;
          server_name staff.buddiedj.nl;
      return 404; # managed by Certbot
  
  
  }
  </code></pre>
  
  <p>Error in webpage:</p>
  
  <pre><code>Internal Server Error
  The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.
  </code></pre>
  
  <p>Error in nginx log</p>
  
  <pre><code>2020/06/14 00:04:25 [warn] 4442#4442: could not build optimal proxy_headers_hash, you should increase either proxy_headers_hash_max_size: 512 or proxy_headers_hash_bucket_size: 64; ignoring proxy_headers_hash_bucket_size 
  </code></pre>
  
  <p>What could be the cause of this problem? And how can I solve it? I've been working on it for a few days but it still doesn't work. At first I was thinking about multiple open SQL connections but I've already adjusted that. And I do have it on my nginx server, but not on localhost</p>
  
  <p>Thanks a lot!</p>