Home > Software design >  gcloud app engine - 502 Bad Gateway server error
gcloud app engine - 502 Bad Gateway server error

Time:09-01

After I successfully deploy my gcloud app I am unable to run it in a web browser.

The error that comes up is "502 Bad Gateway nginx" when I try opening it in my browser with gcloud app browse

This is how I am navigating between pages on my app.py file:

@app.route("/", methods=['GET','POST'])
def index():
    # If you're just opening your home page
    if request.method == "GET":
       return render_template('index.html')
    # If you submitted one of the forms
    elif request.method == "POST":
       if request.values.get("action1", None) == 'Calculator':
           return render_template('/densitycalc.html')
       elif request.values.get("action2", None) == 'Linear Feet':
           return render_template('/linearFeet.html')
       elif request.values.get("action3", None) == 'Insurance':
           return render_template('insurance.html')
       elif request.values.get("action4", None) == 'Home':
           return render_template('index.html')

On each HTML page this is how I am navigating back to the index page below:

<head>
    <h1>"{name of page}"</h1>
    <form method="post" action="/" >
        <input type="submit" name="{corresponding page name}" value="{corresponding page value}">
    </form>
</head>

app.yaml

runtime: python38
entrypoint: gunicorn -b :$PORT main:app

requirements.txt

Flask==2.1.2
gunicorn==20.1.0

Am I looking in the correct places to identify the error? Or is there another file I am missing?

CodePudding user response:

The way your code is currently written, it gives the impression that the attributes - action1, action2, action3, action4 always exists but that doesn't seem to be true from your html page.

Change your code to

# If you're just opening your home page
if request.method == "GET":
    return render_template('index.html')

# If you submitted one of the forms
elif request.method == "POST":
    if request.values.get("action", None) == 'Calculator':
        return render_template('/densitycalc.html')
    elif request.values.get("action2", None) == 'Linear Feet':
        return render_template('/linearFeet.html')
    elif request.values.get("action3", None) == 'Insurance':
        return render_template('insurance.html')
    elif request.values.get("action4", None) == 'Home':
        return render_template('index.html')

By using request.values.get("action", None), you handle the situation where the attribute 'action' doesn't exist (if the attribute doesn't exist, it gets a default value of None)

Updates (because you now have a different error)

Your post talks about app.py but your app.yaml file has an entrypoint which uses main:app.

If you don't actually have a main.py file which is importing app.py and in which the app object is being created, then you should either rename app.py to main.py or you update entrypoint to refer to app:app (I wouldn't advise on this latter option cos your file now has same name as the app object)

If you're wondering why the above isn't an issue in Dev but is an issue in Production - the error will only occur in local if you run your app with dev_appserver.py which will mimic Google's production server. If you don't run in local with dev_appserver.py, then your local env is not making use of app.yaml which means it isn't using your entrypoint parameter.

  • Related