Arama Yap Mesaj Submit
Request a Callback
+90
X
X

Select Your Currency

Turkish Lira $ US Dollar Euro
X
X

Select Your Currency

Turkish Lira $ US Dollar Euro

Contact Us

Location Halkali merkez neighborhood fatih st ozgur apt no 46 , Kucukcekmece , Istanbul , 34303 , TR
NGINX Technical Guide

NGINX 502 Bad Gateway Error Solution

502 Bad Gateway means that NGINX received the client request but couldn't get a valid response from the upstream server, such as PHP-FPM, Node.js, Python, Docker, or another. This guide separates the service, port, Unix socket, proxy_pass, permissions, and security layers in the correct order.

502 Bad GatewayPHP-FPMproxy_passUnix SocketUpstream
root@server:~SSH
2026/07/17 03:14:28 [error] 2187#2187: *904 connect() to unix:/run/php/php8.3-fpm.sock failed (2: No such file or directory) while connecting to upstream
2026/07/17 03:14:28 [error] 2187#2187: *904 connect() failed (111: Connection refused) while connecting to upstream
Request flowClient, NGINX, and backend connection break point
ConfigurationValidation of active server and location blocks
Live diagnosisLog, socket, port, and service controls
Secure ApplicationTest, controlled reload and result verification
01
Technical description

NGINX 502 Bad Gateway ne anlama gelir?

NGINX produces a 502 when it cannot connect to the backend application service in front, or when the backend returns an invalid or partial response. Therefore, restarting NGINX alone is often not a permanent solution.

The first distinction, whether the error is seen in a single domain or in all applications behind NGINX. A single domain usually refers to a vhost, socket or application service; all domains point to a common PHP-FPM, NGINX, network or resource issue.

In PHP-FPM configurations using Unix sockets, the path, owner, and permissions of the socket file must match the fastcgi_pass line exactly. It is a common issue for the old socket path to remain in the vhost file when the PHP version changes.

If TCP upstream is being used, the target IP and port should actually be listened to. The port may have changed after the container, Node.js, or Python service was restarted, or the service may be connected to a different interface instead of localhost, or the firewall may be blocking the connection.

The 502 on the Cloudflare screen is not the same as the 502 produced by the origin NGINX. First, the origin IP and localhost backend should be tested directly to determine which layer the error started in.

Before increasing the timeout values in 502, it must be proven that the backend is up, on the correct port, and accessible by the NGINX worker user.

02
Log messages and their meanings

502 Bad Gateway and upstream connection error messages

Search for the phrase you see in the email, browser, or SSH log. Each card includes meaning, probable cause, and safe initial action.

8 registration
01kritik

connect() failed (111: Connection refused) while connecting to upstream

Meaning: Target IP and port connection is not being accepted.

Possible cause: Backend is closed, wrong port, or service is listening on a different interface.

ss -lntp to check the target port and the status of the related service.
02kritik

connect() to unix socket failed (2: No such file or directory)

Meaning: NGINX is using an unavailable socket path.

Possible cause: The PHP-FPM version, pool name, or socket path has changed.

Compare the listen value in the PHP-FPM pool file with the fastcgi_pass line.
03kritik

connect() to unix socket failed (13: Permission denied)

Meaning: Socket exists but NGINX worker cannot access it.

Possible cause: Socket owner, group, mode, or SELinux context is mismatched.

Examine socket permissions and NGINX/PHP-FPM user groups.
04kritik

no live upstreams while connecting to upstream

Meaning: No available backends left in the upstream group.

Possible cause: All servers are marked as failed or have a DNS/health issue.

Test upstream members directly with curl and compare error_log times.
05warning

upstream prematurely closed connection

Meaning: Backend closed the connection before completing the response.

Possible cause: Application crash, process kill, memory exhaustion or protocol mismatch.

Check the backend application and OOM logs at the same second.
06warning

recv() failed (104: Connection reset by peer)

Meaning: The counter-party has forcibly reset the connection.

Possible cause: Backend restart, application error, or network security layer.

Check the backend service restart count and application log.
07warning

upstream sent invalid header

Meaning: Backend did not produce HTTP/FastCGI protocol-compliant header.

Possible cause: Incorrect proxy protocol, faulty application output, or FastCGI port HTTP transmission.

Verify that the choice of proxy_pass and fastcgi_pass is appropriate for the backend type.
08bilgi

502 Bad Gateway nginx

Meaning: The general error in the browser hides the root cause.

Possible cause: Verify the service, socket, port, permission, or application layer

Find the first upstream line with the same request ID/timestamp in the domain error_log.

No records matching this expression were found.

03
Secure first review

SSH diagnostic commands and what output to look for?

Commands are for root access. Collect only status and logs first; Do not change the permanent setting without seeing the reason.

NGINX configuration test
nginx -t
nginx -T | grep -nE 'server_name|proxy_pass|fastcgi_pass'

Syntax is tested and active upstream targets are displayed.

Service and port check
systemctl status nginx php8.3-fpm --no-pager
ss -lntp
ss -lxnp | grep -E 'php|fpm'

NGINX, PHP-FPM, TCP ports and Unix sockets are verified.

Error logs.
tail -n 150 /var/log/nginx/error.log
journalctl -u nginx -u php8.3-fpm -n 150 --no-pager

NGINX and backend service errors are compared within the same time frame.

Backend direct test
curl -sv http://127.0.0.1:3000/ -o /dev/null
curl -skI https://example.com/

Reverse proxy behind it tests the service independent of NGINX.

Socket permission control
namei -l /run/php/php8.3-fpm.sock
stat /run/php/php8.3-fpm.sock
ps -eo user,group,cmd | grep '[n]ginx: worker'

Shows access to all folders in the socket path and the worker user.

Secure Reboot
nginx -t && systemctl reload nginx
systemctl restart php8.3-fpm

NGINX loads continuously; only the backend is restarted when necessary.

04
By hosting environment

Ubuntu/Debian, AlmaLinux/CloudLinux and Plesk controls

Although the root cause of the NGINX error is the same, package paths, service names, security layers and virtual host management vary depending on the Linux distribution or Plesk infrastructure used.

Ubuntu / Debian

NGINX 502 Bad Gateway for systemd, package path, and journald controls.

  • First, verify the active NGINX configuration and service status.
  • Compare the domain error log with the systemd log in the same time range.
  • Run nginx -t before the change, then perform a graceful reload.
systemctl status nginx php8.3-fpm --no-pager nginx -t tail -n 100 /var/log/nginx/error.log

AlmaLinux / CloudLinux

NGINX 502 Bad Gateway for SELinux, PHP-FPM pool, and RHEL-based service controls.

  • Check the active status of NGINX and related backend services.
  • Review SELinux AVC records and security contexts.
  • Do not restart the service without passing the configuration test.
systemctl status nginx php-fpm --no-pager nginx -t tail -n 100 /var/log/nginx/error.log

Plesk Obsidian

NGINX virtual host files generated by Plesk: nginx 502 bad gateway control.

  • Review the additional directives in the Domains > Apache & nginx Settings section.
  • Separate manually created and Plesk generated files.
  • If necessary, recreate the web configuration for the relevant domain only.
plesk repair web example.com -y systemctl status nginx sw-engine --no-pager nginx -t
05
Safe solution order

NGINX 502 Bad Gateway Secure Solution Order

Instead of randomly restarting the service, proceed by proving that the error occurred at the NGINX, socket, backend, or security layer.

1

Determine the affected scope

Determine whether a single domain, a single PHP version, or all upstream applications are affected.

for u in https://example.com https://diger.example.com; do curl -sk -o /dev/null -w "$u %{http_code}\n" "$u"; done
2

Find the same error_log line at the same time

Classify the root cause based on the upstream error line, not the browser message.

tail -f /var/log/nginx/error.log
3

Verify that the backend service is listening

Verify that the socket or TCP port target is actually open.

ss -lntp; ss -lxnp | grep -E 'php|fpm'
4

Compare the vhost target with the service settings.

Match the fastcgi_pass or proxy_pass value with the actual server IP address.

nginx -T | grep -nE 'fastcgi_pass|proxy_pass'
5

Check the permission and security layer.

Check SELinux and firewall blocks for Socket access.

namei -l /run/php/php8.3-fpm.sock; getenforce 2>/dev/null
6

Test and apply controlled reload

If syntax is successful, reload and measure both origin and backend again.

nginx -t && systemctl reload nginx curl -skI https://example.com/
06
Distinction by symptom

Special scenarios and decision trees

Started after PHP update.

Old PHP-FPM socket path may be left in the vhost file.

grep -R 'fastcgi_pass' /etc/nginx /etc/plesk 2>/dev/null
Occurred after Docker deployment

Container port, network name, or health status may have changed

docker ps --format 'table {{.Names}}\t{{.Status}}\t{{.Ports}}'
502 Error in Node.js Application

The PM2/systemd process is stopped or the application is bound to a different port.

systemctl status application --no-pager; ss -lntp
Permission denied is displayed.

Examine socket group/mode or SELinux context mismatch.

stat /run/php/php8.3-fpm.sock; ausearch -m AVC -ts recent | tail
Only 502 from Cloudflare

Distinguish the Cloudflare layer with direct access to Origin.

curl -skI --resolve example.com:443:ORIGIN_IP https://example.com/
Only one domain is affected in Plesk.

Rebuild the generated NGINX/PHP handler configuration for the domain.

plesk repair web example.com -y

Absolutely don't

  • Do not restart all services at the same time without looking at the error log.
  • Do not try to solve the socket permission issue by setting it to 777.
  • Do not write a random port instead of the PHP-FPM socket.
  • Do not just increase proxy timeout values when the backend crashes.
  • Do not edit the main vhost file generated by Plesk directly and permanently.

Post-solution verification

  • NGINX configuration test is successful.
  • Backend socket or TCP port is listening steadily.
  • The Origin URL is returning the expected 2xx/3xx HTTP code.
  • There is no connection refused or permission denied in the new error_log records.
  • Services are not starting up in short intervals and are not producing OOM records.
07
Official technical resources

Official documentation of NGINX and related components

08
Internal SEO content set

Related NGINX error solutions

09
Frequently asked questions

NGINX 502 Bad Gateway Curiosities about

NGINX 502 Bad Gateway why?

The most common reason for not being able to connect to NGINX's PHP-FPM or reverse proxy backend is a wrong socket/port, a stopped service, permission issues, or the application closing the connection prematurely.

Does restarting NGINX solve the 502 error?

If there is an NGINX process issue, it may resolve temporarily or ineffectively if the backend is closed or the fastcgi_pass is incorrect.

What is the difference between 502 and 504?

502 usually indicates that a connection couldn't be established or an invalid backend response was received; 504 indicates that a connection was established but no response was received in time.

How do I find the PHP-FPM socket path?

Check the listen value in the PHP-FPM pool files and the output of ss -lxnp. This value should match the NGINX fastcgi_pass line.

What does Connection refused mean?

Indicates that the target IP/port is not accepting the connection or has been actively blocked. The service, port, and bind address should be checked.

What is the safe operation for 502 in Plesk?

First, check the domain logs and handler settings; then, if necessary, recreate the web configuration for the relevant domain only with plesk repair web domainname -y.

Is the Cloudflare 502 error origin-related?

Not always. You can also directly send requests to the origin IP using --resolve and separate Cloudflare and origin NGINX layers

EKA SOFTWARE AND INFORMATION SYSTEMS

Let's fix the error on your server permanently

By analyzing cPanel, WHM, CloudLinux, LiteSpeed, MariaDB, Exim and security layers together, we fix the root cause of the failure instead of just removing the service.

Get Server Support WhatsApp
Top