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

502 bad gateway nginx fix
502 bad gateway cloudflare
502 bad gateway nginx ubuntu
502 bad gateway iis
502 bad gateway iphone
the remote server returned an error: (502) bad gateway
pro tools first 502 bad gateway
zoom 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?

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.

the server responded with a status of 502 (Bad Gateway), Update: If your code isn't returning fast enough, I would suggest returning the blank page with no calculations returned, and have the page  Dismiss Join GitHub today. GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.

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.

How to Solve 502 Bad Gateway Issues?, A 502 Bad Gateway indicates that the edge server (server acting as a If one of KeyCDN's edge servers receive a 502 Bad Gateway response from your origin server, in this file making it a valuable resource to check when you need more Knowledge Base · Network Status · Open Source · FAQ · Tools. Failed to load resource: the server responded with a status of Failed to load resource: the server responded with a io features on I get a 502 Bad Gateway

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

What is a 502 bad gateway and how do you fix it?, indicates that the edge server (server acting as a proxy) was not able to get a valid or any response from the origin server (also called upstream server). Bad gateways are not usually the result of coding problems. They usually are configuration problems. CORS is when a web application makes a cross-origin HTTP request when it requests a resource

How Can I Solve 502 Error Bad Gateway from Cloudflare , before contacting your origin server, and in that case, the error page should be like this. Failed to load resource: the server responded with a status of 504 (Gateway Timeout) ng new my-project --service-worker. Expected behavior. It should not throw any exceptions. Minimal reproduction of the problem with instructions. ng new my-project --service-worker ng build --prod cd dist http-server -p 8080 Even with IIS host i can see the

Failed to load resource: the server responded with a status , Failed to load resource: the server responded with a status of 502 (Bad Gateway) #332. Closed. mohit-jcc opened this issue on Feb 5, 2017 · 3 comments. Re: Failed to load resource: the server responded with a status of 400 (Bad Request) Kiran Pawar Mar 31, 2015 6:39 AM ( in response to thirugtrs ) Hi thirugtrs,

What is a 502 Bad Gateway Error (And How Can I Fix It)?, Failed to load resource: the server responded with a status of 502 (Bad Gateway) #42. Closed. vovanb opened this issue on Oct 29, 2018 · 16 comments. Closed  Failed to load resource: the server responded with a status of 500 (Internal Server Error) Failed to load resource: net::err_name_not_resolved Even if a specific file didn’t load, the rest of your web page will continue to load.

Comments
  • as @rchurch4 said , I'd move your code which has large calculations to be returned as an api and called via ajax. Also , If you're using web servers (nginx) please try by increasing uwsgi-timeout.
  • Okay, so I'm pretty sure that it's a configuration issue, but since I can't see your code, I can't be sure. Try commenting out the heavy calculations and returning hard code instead of doing the computations. If you still get the 502, then it is certainly your configuration.
  • If your heavy calculations code is loading data from elsewhere on the server, are you sure that your static root and media root are properly configured?
  • Everything worked when I commented out the heavy calculations so the problem is there. I will edit my question to include the code that is causing this error. Do you know why this happening ?
  • I opened csv files and stored values in a numpy matrix at the beginning of the function. Which works fine.
  • If you were able to open the CSV files without the 502, then try the asynchronous call like I described in the updated answer. Celery could also be for you.
  • I have been playing around with the nginx configs but its still crashing. I checked the nginx error log and this is what I got: upstream prematurely closed connection while reading response header from upstream.
  • I went to /etc/nginx/sites-available/django and this is the current setup location / { proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $host; proxy_redirect off; proxy_buffering off; proxy_read_timeout 900s; proxy_send_timeout 900s; proxy_pass 142.93.51.83; }
  • upstream prematurely closed connection means your worker host terminated the connection. Do you run e.g. uwsgi? That might have a timeout to restrict http requests as well.
  • I am not sure, how can I figure that out? Also after I make changes to /etc/nginx/sites-available/django do I have to run a command or something to recompile that file?
  • Do you own and run the worker host 142.93.51.83 ? Can you check what's running on that host? Also, if you try to hit a worker endpoint with e.g. curl - does it complete successfully ?