'Failed to load resource: the server responded with a status of 502 (Bad Gateway)

How can you solve a 502 error?

I believe whats causing the 502 error is a nested loop that contains heavy calculations. It would take up to 2 minutes for it to finish that block of code on my local server. However on a public server I just get this 502 Bad Gateway nginx/1.10.3 (Ubuntu)

for j in x:
    if j == 1:
        index_for_multi_array = 0
    else:
        index_for_multi_array = 1

    q = con[j-1] # index 0 and 7 
    q = q * 1e-6
    m = mass[j-1]
    for i in range(1,int(bands[j-1])+1):
        #read parameters
        vc         = float(_1_wnum[index_for_multi_array][i-1])
        S0         = float(_1_int[index_for_multi_array][i-1] )
        gamma_air  = float(_1_abroad[index_for_multi_array][i-1])
        gamma_self = float(_1_sbroad[index_for_multi_array][i-1])
        n          = float(_1_abcoef[index_for_multi_array][i-1] )
        #resonance shape
        alpha_1  = float( (1.0- q) * gamma_air  + q * gamma_self ) * (P/P0) * ((T/T0) ** n)
        fv       = (alpha_1 / math.pi) * np.power((np.true_divide(v,vc)),2) * np.add((np.true_divide(1, np.power(v-vc,2) + alpha_1**2)) , np.true_divide(1,np.power((v+vc),2)+alpha_1**2))
        gv       = np.true_divide(v,vc) * pre_calculation / np.tanh(h*c*vc / (2*k*T)) * fv  
        S        = S0  #line intensity
        sigmav   = gv * S0
        qq       = q * P/R/T*NA
        kv       = P/P0*T0/T*qq*sigmav
        kvt[index_for_multi_array,:] = kvt[index_for_multi_array,:] + kv

the max number of iterations of the inner loop is 209,000 the number of iterations of the outer loop is 8

My site is made with django. I am also using numpy (i installed it on the public server), js, html, and css.

Also the home page works fine. On a click of a button I get redirected to another page. That page takes a while to load because there are a lot of calculations being made in views.py, but midway it crashes with the 502 error.

Any ideas for where to start looking to solve this error?



Solution 1:[1]

Update: If your code isn't returning fast enough, I would suggest returning the blank page with no calculations returned, and have the page make an ajax call to the heavy calculations so that it can load asynchronously. Once the calculations are done, the page will load their results: http://api.jquery.com/jquery.ajax/

So what you'd do is move the heavy calculations to a view that returns your results in some format like JSON (https://docs.djangoproject.com/en/2.1/ref/request-response/#django.http.JsonResponse), and set up Javascript on the original page to call that view, wait for the JSON to be returned, and render the JSON into whatever format you wanted on the page once it is returned.

Another option is to string up Celery: http://docs.celeryproject.org/en/latest/

OLD: Try changing proxy_pass to your server IP instead of 127.0.0.1:8000 in /etc/nginx/sites-available/myproject. Also make sure that in your settings.py, your server's IP is in ALLOWED_HOSTS.

Bad gateways are not usually the result of coding problems. They usually are configuration problems.

Solution 2:[2]

Use the nginx configuration directives proxy_read_timeout and proxy_send_timeout to make nginx not terminate the proxy connection prematurely and return an error.

Solution 3:[3]

Solved with going to python2.7 directory /usr/lib/python2.7/dist-packages/gunicorn and opening the config file and finding the timeout function and changing the default timeout from 30 to 60 .

The running the command service gunicorn restart

This should fix the timout problem

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Oliver
Solution 3 John Jonson