0

When I first visit the website, it loads correctly. but on refresh, it gives me the error 502 Bad Gateway Nginx/1.14.0 (Ubuntu). I am deploying a simple e-commerce site with nodejs. I am using vps which is running ubuntu-18.04-x86_64.

Below is what i get when i run error logs.

    tail -f /var/log/nginx/error.log
    
2022/07/19 01:35:17 [error] 3810#3810: *197 no live upstreams while connecting to upstream, 
client: 111.90.141.20, server: www.example.com, request: "GET /mysqladmin/ HTTP/1.1", 
upstream: "http://localhost/mysqladmin/", host: "vps_ip_address", referrer: 
"http://vps_ip_address/"

2022/07/19 01:35:17 [error] 3810#3810: *198 no live upstreams while connecting to upstream, 
client: 111.90.141.20, server: www.example.com, request: "GET /mysql-admin/ HTTP/1.1", 
upstream: "http://localhost/mysql-admin/", host: "vps_ip_address", referrer: 
"http://vps_ip_address/"

2022/07/19 01:39:11 [error] 3810#3810: *204 connect() failed (111: Connection refused) while 
connecting to upstream, client: 197.232.142.53, server: www.example.com, request: "GET /blog 
HTTP/1.1", upstream: "http://127.0.0.1:8000/blog", host: "www.example.com", referrer: 
"http://www.example.com/"

2022/07/19 01:39:11 [error] 3810#3810: *204 connect() failed (111: Connection refused) while 
connecting to upstream, client: 197.232.142.53, server: www.example.com, request: "GET /blog 
HTTP/1.1", upstream: "http://[::1]:8000/blog", host: "www.example.com", referrer: 
"http://www.example.com/"

and below is my Nginx config file (i am new to nginx and i really want this done).

    server {
        listen 80;
        listen [::]:80 ipv6only=on default_server;
server_name www.example.com example.com;
proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;


location / {
    proxy_set_header    X-Forwarded-For $remote_addr;
    proxy_set_header    Host $http_host;
    proxy_pass          http://127.0.0.1:8000/;
    proxy_http_version  1.1;
    proxy_set_header    "Connection" "";
    proxy_set_header    Upgrade $http_upgrade;
    client_max_body_size 20m;
    proxy_read_timeout 600s;
    
}

location ^~ /css {
    autoindex on;
    alias /home/saidjillo/webcitey/public/css;
}


location ^~ /media_query {
    autoindex on;
    alias /home/saidjillo/webcitey/public/css/media_query;
}

location ^~ /js {
    autoindex on;
    alias /home/saidjillo/webcitey/public/js;
}

location /images {
    alias /home/saidjillo/webcitey/images;
    autoindex on;
}
    }

Someone to help with this problem. I am working from a very remote area where getting technical assistance like this one is a tricky business.

I am using pm2 to serve my nodejs.

Seyyid Said
  • 475
  • 2
  • 6
  • 16
  • I have gone through a lot of tutorials on this issue but am still unable to solve it. – Seyyid Said Jul 19 '22 at 08:58
  • The website is working fine but when you do a quick refresh, it gives you the `502 Bad Gateway` error – Seyyid Said Jul 19 '22 at 09:03
  • 1
    It might be related to the invalid server name configuration, you are setting the host to "example.com", try changing it to "localhost" or "_" and see if that works, also try to remove the "ipv6only=on" part, as it will force the server to only take IPv6 requests (which I guess you don't need) – Dyn4sty Jul 19 '22 at 09:19
  • The ` example.com` its just there on purpose, and its not what i am using. I am using my reall domain. – Seyyid Said Jul 19 '22 at 09:20
  • I have edited my config file and changed `listen [::]:80 ipv6only=on default_server;` to `listen [::]:80 default_server;` and still the error comes on refresh. – Seyyid Said Jul 19 '22 at 09:27
  • 1
    1st the error does not have anything to do with the server_name. 2nd PLEASE UPDATE YORU NGINX VERSION! 1.14 is old and you should go for the latest version. Will check your issue now. The issue is because the second request goes to an IPv6 binding of port `8000` which will likely not be present. Plese check `netstat -tulpn |grep "LISTEN"` – Timo Stark Jul 19 '22 at 10:15
  • I have done the nginx upgrade to nginx/1.18.0 (Ubuntu) but still it gives me the same error on quick refresh – Seyyid Said Jul 19 '22 at 18:49

0 Answers0