Nandan Pandey
Nandan Pandey

Reputation: 158

Sometimes 502 Bad Gateway , Sometimes 504 gateway timeout, sometimes website loads successfully

I have a website that is hosted on google cloud platform using NGINX server. Some hour before it was working well but suddenly an error of 502 bad gateway occured.

NGINX server is hosted on another instance and main project is another instance and following is the configuration of my server :

server {
      listen 443 ssl;  
      server_name www.domain.com;
       ssl_certificate /path-to/fullchain.pem;
      ssl_certificate_key /path-to/privkey.pem;
     
      # REST API Redirect
      location /api {
              proxy_set_header X-Real-IP  $remote_addr;
              proxy_set_header X-Forwarded-For $remote_addr;
              proxy_set_header Host $host;
              proxy_pass http:/internal-ip:3000;
      }
     
       # Server-side CMS Redirect
      location / {
              proxy_set_header X-Real-IP  $remote_addr;   
              proxy_set_header X-Forwarded-For $remote_addr;
              proxy_set_header Host $host;
              proxy_pass http://internal-ip:4400;
       }
  }

when restarted the instance of nginx server then website loaded successfully but after three or four refresh it started giving Bad Gateway and after that anytime I open it is giving bad gateway error. Sometimes, automatically it reloads well but again down.

Tried to know the error log of nginx server and following is the output of error log:

Sometimes this popular issue is logged: enter image description here

and sometimes this:enter image description here

Regarding first issue I tried some recommendations such that increase the proxy send and read time to some higher value as suggested here in server configuration and also shown in the image as follows :

enter image description here

Also backend code is working fine because I can access the deployed backend services in local during development but hosted website can not access any backend service.

But nothing worked and sadly my website is down. Please suggest any solution.

Upvotes: 1

Views: 2027

Answers (1)

Maciej Perliński
Maciej Perliński

Reputation: 412

By default nginx is having 1024 worker connections you can change it with

events {
  worker_connections  4096; 
}

Also you can try to increase amount of workers as workers*worker_connections is giving you amount of connections you can handle. All that is in the context your site is receiving a traffic and you simply running out of connections.

Upvotes: 1

Related Questions