{"id":14962,"date":"2025-11-18T04:16:51","date_gmt":"2025-11-18T10:16:51","guid":{"rendered":"https:\/\/www.supportpro.com\/blog\/?p=14962"},"modified":"2025-11-18T04:18:20","modified_gmt":"2025-11-18T10:18:20","slug":"how-to-fix-502-bad-gateway-why-ip-works-but-domain-fails-reverse-proxy-guide","status":"publish","type":"post","link":"https:\/\/www.supportpro.com\/blog\/how-to-fix-502-bad-gateway-why-ip-works-but-domain-fails-reverse-proxy-guide\/","title":{"rendered":"How to Fix 502 Bad Gateway: Why IP Works but Domain Fails &#8211; Reverse Proxy Guide"},"content":{"rendered":"\n<p>When you see a 502 Bad Gateway error , it can be frustrating, especially when your website works perfectly fine using the server\u2019s IP address, but breaks when you access it through the domain name. It\u2019s a very common issue that happens when you\u2019re using a reverse proxy such as Nginx, Apache, or HAProxy. In this blog, we explained why it happens, and how you can fix it by following the steps one by one.\u00a0<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What Does \u201c502 Bad Gateway\u201d exactly Means?<\/strong><\/h2>\n\n\n\n<p>A 502 Bad Gateway error means that one server act as a gateway or proxy received an invalid response from another server then it come across the 502 Bad gateway. Simply saying, when your reverse proxy (like Nginx) tried to connect to the backend service or web application (like Apache, Node.js, or a <a href=\"https:\/\/www.supportpro.com\/blog\/docker-made-easy-essential-control-panels-and-ui-tools\/\" title=\"\">Docker <\/a>container), but the communication failed and it could not connect. It doesn&#8217;t always mean your backend is down. The proxy just doesn\u2019t know how to talk to it correctly due to misconfigurations.<\/p>\n\n\n\n<p><strong>So the reason why it Works on IP but Not on Domain :<\/strong><\/p>\n\n\n\n<p>If your website works using the server\u2019s IP address (like http:\/\/192.168.50.12) but not with your domain name (like https:\/\/example.com), so it usually means:<\/p>\n\n\n\n<div class=\"wp-block-group is-vertical is-layout-flex wp-container-core-group-is-layout-8cf370e7 wp-block-group-is-layout-flex\">\n<ul class=\"wp-block-list\">\n<li><strong>The reverse proxy is misconfigured <\/strong>&#8211; It\u2019s possible it\u2019s pointing to the wrong upstream host or missing a hostname.<\/li>\n\n\n\n<li><strong><a href=\"https:\/\/www.supportpro.com\/blog\/usage-of-dig-command-for-finding-the-dns-information\/\" title=\"\">DNS <\/a>or hostname resolution issue<\/strong> &#8211; Your domain may not be resolving correctly on the proxy server.<\/li>\n\n\n\n<li><strong>SSL or HTTPS configuration issue &#8211;<\/strong> The proxy might not trust the certificate or might be redirecting incorrectly .&nbsp;<\/li>\n\n\n\n<li><strong>Firewall or SELinux restrictions &#8211;<\/strong> The proxy might not be allowed to reach the domain name.<\/li>\n\n\n\n<li><strong>Loopback to itself<\/strong> &#8211; sometimes the proxy tries to connect to the same domain name it\u2019s serving, creating an infinite loop as loopback.&nbsp;<\/li>\n<\/ul>\n<\/div>\n\n\n\n<p><strong>So below are the steps to follow to fix this issue one by one.&nbsp;<\/strong><\/p>\n\n\n\n<p><strong>Step 1: Check If Domain Resolves Correctly<\/strong><\/p>\n\n\n\n<p>From your reverse proxy server, test if your domain resolves to the correct IP:<\/p>\n\n\n\n<p>ping example.com <\/p>\n\n\n\n<p>or<\/p>\n\n\n\n<p>nslookup example.com<\/p>\n\n\n\n<p>If the IP shown doesn\u2019t match your server\u2019s IP, then DNS is the issue.<\/p>\n\n\n\n<p>You need to update your DNS records to fix it (A or CNAME) or by adding a temporary entry in \/etc\/hosts like this:<\/p>\n\n\n\n<p>192.168.50.12 example.com<\/p>\n\n\n\n<p>Save and try again.&nbsp;<\/p>\n\n\n\n<p><strong>Step 2: Check Reverse Proxy Configuration<\/strong><\/p>\n\n\n\n<p>If you\u2019re using Nginx as a reverse proxy, you need to open your site configuration file (for example, \/etc\/nginx\/sites-available\/example.com):<\/p>\n\n\n\n<p>#sudo nano \/etc\/nginx\/sites-available\/example.com<\/p>\n\n\n\n<p>You will see the code like below&nbsp;<\/p>\n\n\n\n<div class=\"wp-block-group is-vertical is-layout-flex wp-container-core-group-is-layout-8cf370e7 wp-block-group-is-layout-flex\">\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><br>server {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;server_name example.com;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;location \/ {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proxy_pass http:\/\/example.com:8080;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<p><\/p>\n<\/blockquote>\n<\/div>\n\n\n\n<p>So the problem could be here, the proxy is trying to reach example.com again, which causes a loop.<\/p>\n\n\n\n<p>You should replace it with your backend IP address instead domainname like below :<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><\/p>\n\n\n\n<p>server {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;server_name example.com;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;location \/ {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proxy_pass http:\/\/127.0.0.1:8080;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proxy_set_header Host $host;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proxy_set_header X-Real-IP $remote_addr;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<p><\/p>\n<\/blockquote>\n\n\n\n<p>Save the file and test your Nginx configuration using command below :<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>#sudo nginx -t<\/strong><\/p>\n\n\n\n<p><strong>#sudo systemctl reload nginx<\/strong><\/p>\n<\/blockquote>\n\n\n\n<p>Now, try to access the domain again. If it works, the issue was a proxy loop.<\/p>\n\n\n\n<p><strong>Step 3: Check for SSL\/HTTPS Issues<\/strong><\/p>\n\n\n\n<p>If your site uses HTTPS, you need to make sure both your proxy and backend are handling SSL correctly.<\/p>\n\n\n\n<p>Sometimes, you only need SSL on the proxy, not on the backend.<\/p>\n\n\n\n<p>For example, if Nginx handles HTTPS, and your backend only runs HTTP, your configuration should be like:<\/p>\n\n\n\n<div class=\"wp-block-group is-vertical is-layout-flex wp-container-core-group-is-layout-8cf370e7 wp-block-group-is-layout-flex\">\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>server {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;listen 443 ssl;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;server_name example.com;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;ssl_certificate \/etc\/letsencrypt\/live\/example.com\/fullchain.pem;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;ssl_certificate_key \/etc\/letsencrypt\/live\/example.com\/privkey.pem;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;location \/ {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proxy_pass http:\/\/127.0.0.1:8080;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proxy_set_header Host $host;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p>}<\/p>\n<\/blockquote>\n<\/div>\n\n\n\n<p>If you mistakenly use proxy_pass https:\/\/example.com;, Nginx might fail to validate the SSL certificate, and it will cause a 502 error.<\/p>\n\n\n\n<p><strong>Step 4: Check Backend Service<\/strong><\/p>\n\n\n\n<p>In some cases , the backend service (like Apache, Gunicorn, or Node.js) isn\u2019t actually responding even though it seems to be running.<\/p>\n\n\n\n<p>You need to check and verify if it\u2019s listening on the correct port:<\/p>\n\n\n\n<p><strong>sudo netstat -tlnp | grep 8080<\/strong><\/p>\n\n\n\n<p>or<\/p>\n\n\n\n<p><strong>ss -tlnp | grep 8080<\/strong><\/p>\n\n\n\n<p>If the service isn\u2019t running, you need to restart it:<\/p>\n\n\n\n<p><strong>sudo systemctl restart apache2<\/strong><\/p>\n\n\n\n<p># or<\/p>\n\n\n\n<p><strong>sudo systemctl restart your_app<\/strong><\/p>\n\n\n\n<p><strong>Step 5: Look at Logs for More Clues and exact issue tracing<\/strong><\/p>\n\n\n\n<p>In issue investigation, the logs works like your best friends in debugging the any issues error, specially for 502 errors.&nbsp;<\/p>\n\n\n\n<p>Check Nginx error logs:<\/p>\n\n\n\n<p><strong>sudo tail -f \/var\/log\/nginx\/error.log<\/strong><\/p>\n\n\n\n<p>If it shows messages like \u201cconnect() failed\u201d or \u201cbad gateway\u201d, it usually points to a connectivity issue between the proxy and the backend.<\/p>\n\n\n\n<p>You can also check application logs to see if it\u2019s rejecting connections or crashing under load.<\/p>\n\n\n\n<p><strong>Step 6: Restart Services<\/strong><\/p>\n\n\n\n<p>Once the changes are done ,&nbsp; restart everything cleanly:<\/p>\n\n\n\n<p><strong>#sudo systemctl restart nginx<\/strong><\/p>\n\n\n\n<p><strong>#sudo systemctl restart your_app<\/strong><\/p>\n\n\n\n<p>Then, test again using using the curl command :<\/p>\n\n\n\n<p><strong>curl -I https:\/\/example.com<\/strong><\/p>\n\n\n\n<p>It it shows 200 OK response if everything is fixed.<\/p>\n\n\n\n<p>Issues like 502 Bad Gateway errors can stem from proxy, DNS, or SSL settings, and our team is available 24\u00d77 to guide you through them. Whether you need quick <a href=\"https:\/\/www.supportpro.com\/freecheckup.php\" title=\"\">troubleshooting<\/a>, configuration validation, or hands-on assistance, we\u2019re always here to help. Connect with us anytime through chat or <a href=\"https:\/\/www.supportpro.com\/emergency-new.php\" title=\"\">contact us<\/a> directly for immediate support.<br><\/p>\n\n\n\n<div class=\"wp-block-media-text alignwide has-media-on-the-right is-stacked-on-mobile is-vertically-aligned-center has-white-background-color has-background\"><div class=\"wp-block-media-text__content\">\n<p class=\"has-large-font-size\">Partner with <strong>SupportPRO<\/strong> for 24\/7 proactive cloud support that keeps your business secure, scalable, and ahead of the curve.<\/p>\n\n\n\n<!--HubSpot Call-to-Action Code --><span class=\"hs-cta-wrapper\" id=\"hs-cta-wrapper-3350a795-db50-482f-9911-301930d1b1be\"><span class=\"hs-cta-node hs-cta-3350a795-db50-482f-9911-301930d1b1be\" id=\"hs-cta-3350a795-db50-482f-9911-301930d1b1be\"><!--[if lte IE 8]><div id=\"hs-cta-ie-element\"><\/div><![endif]--><a href=\"https:\/\/cta-redirect.hubspot.com\/cta\/redirect\/2725694\/3350a795-db50-482f-9911-301930d1b1be\" ><img decoding=\"async\" class=\"hs-cta-img\" id=\"hs-cta-img-3350a795-db50-482f-9911-301930d1b1be\" style=\"border-width:0px;\" src=\"https:\/\/no-cache.hubspot.com\/cta\/default\/2725694\/3350a795-db50-482f-9911-301930d1b1be.png\"  alt=\"Contact Us today!\"\/><\/a><\/span><script charset=\"utf-8\" src=\"https:\/\/js.hscta.net\/cta\/current.js\"><\/script><script type=\"text\/javascript\"> hbspt.cta.load(2725694, '3350a795-db50-482f-9911-301930d1b1be', {\"useNewLoader\":\"true\",\"region\":\"na1\"}); <\/script><\/span><!-- end HubSpot Call-to-Action Code -->\n<\/div><figure class=\"wp-block-media-text__media\"><img fetchpriority=\"high\" decoding=\"async\" width=\"904\" height=\"931\" src=\"https:\/\/www.supportpro.com\/blog\/wp-content\/uploads\/2022\/09\/Free-server-checkup.png\" alt=\"guy server checkup\" class=\"wp-image-12943 size-full\" srcset=\"https:\/\/www.supportpro.com\/blog\/wp-content\/uploads\/2022\/09\/Free-server-checkup.png 904w, https:\/\/www.supportpro.com\/blog\/wp-content\/uploads\/2022\/09\/Free-server-checkup-291x300.png 291w, https:\/\/www.supportpro.com\/blog\/wp-content\/uploads\/2022\/09\/Free-server-checkup-768x791.png 768w, https:\/\/www.supportpro.com\/blog\/wp-content\/uploads\/2022\/09\/Free-server-checkup-585x602.png 585w\" sizes=\"(max-width: 904px) 100vw, 904px\" \/><\/figure><\/div>\n","protected":false},"excerpt":{"rendered":"<p>When you see a 502 Bad Gateway error , it can be frustrating, especially when your website works perfectly fine using the server\u2019s IP address, but breaks when you access&hellip;<\/p>\n","protected":false},"author":4,"featured_media":14965,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","footnotes":""},"categories":[207,3,332],"tags":[],"class_list":["post-14962","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cloud-support-services","category-technical-articles","category-troubleshooting"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/posts\/14962","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/comments?post=14962"}],"version-history":[{"count":5,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/posts\/14962\/revisions"}],"predecessor-version":[{"id":14969,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/posts\/14962\/revisions\/14969"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/media\/14965"}],"wp:attachment":[{"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/media?parent=14962"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/categories?post=14962"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/tags?post=14962"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}