Updated: 10 November 2026 · Applies to: nginx 1.20.1 on AlmaLinux 9, nginx 1.24.0 on Ubuntu 24.04, nginx 1.26.3 on Debian 13 (tested); cPanel NGINX with reverse proxy (ea-nginx) and CPNginx (from vendor documentation)

Nginx refuses to start with open() "/var/log/nginx/error.log" failed (2: No such file or directory) when the folder that holds its log file does not exist. Recreate the folder with the right owner and permissions, run nginx -t, then restart nginx. The same error appears for any other log path in your configuration, such as a per-site log under /var/log/nginx/domains/.

What the error looks like

We removed /var/log/nginx on a test AlmaLinux 9 server and ran the configuration test:

nginx -t

Output:

nginx: [alert] could not open error log file: open() "/var/log/nginx/error.log" failed (2: No such file or directory)
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
[emerg] 68#68: open() "/var/log/nginx/error.log" failed (2: No such file or directory)
nginx: configuration file /etc/nginx/nginx.conf test failed

Note the line "syntax is ok": the configuration itself is fine, nginx just cannot create its log file. Errno 2 always means a missing file or folder. If the message says (13: Permission denied) instead, the folder exists but nginx cannot write to it.

Fix it (as root)

  1. Find the path nginx complains about. It is in the error message, and in the error_log and access_log lines of your configuration:
    grep -rn -E "error_log|access_log" /etc/nginx/
  2. Recreate the folder. On AlmaLinux, Rocky Linux and other RHEL-based systems the nginx package creates it owned by root with mode 711:
    mkdir -p /var/log/nginx
    chown root:root /var/log/nginx
    chmod 711 /var/log/nginx
    On Ubuntu 24.04 and Debian 13 the package uses root and the adm group with mode 755:
    mkdir -p /var/log/nginx
    chown root:adm /var/log/nginx
    chmod 755 /var/log/nginx
    The nginx master process runs as root and opens the log files itself, so the folder does not have to belong to the nginx or www-data user.
  3. Test the configuration again:
    nginx -t
    Output:
    nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    nginx: configuration file /etc/nginx/nginx.conf test is successful
  4. Restart nginx:
    systemctl restart nginx
    Nginx creates a fresh error.log and access.log in the folder.

A missing folder for one website

Many setups give each site its own log file. If that folder was never created, the error names it. In our test, a server block with error_log /var/log/nginx/domains/example.com.error.log; gave:

nginx: [emerg] open() "/var/log/nginx/domains/example.com.error.log" failed (2: No such file or directory)
nginx: configuration file /etc/nginx/nginx.conf test failed

Create the folder and test again:

mkdir -p /var/log/nginx/domains
nginx -t

One bad path stops the whole server, not only that site, so fix it before you restart.

Why does the folder go missing?

  • Someone cleaned up disk space with rm -rf /var/log/nginx or a script that deletes log folders instead of the files inside them.
  • A configuration copied from another server points at a log path that does not exist here.
  • A control panel or plugin was removed and took its log folders with it, while its configuration stayed.

To free space safely, empty a large log instead of deleting its folder, for example truncate -s 0 /var/log/nginx/access.log, and let logrotate handle old files.

Where the 502 Bad Gateway comes in

When nginx does not start at all, the browser shows a connection error, not a 502. A 502 Bad Gateway comes from nginx itself: it is running, but the backend it passes requests to (Apache, PHP-FPM or an application) is not answering. In our test, nginx proxied to a port where nothing was listening:

curl -sI http://127.0.0.1:8080/ | head -1

Output:

HTTP/1.1 502 Bad Gateway

And the site error log said why:

[error] 91#91: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 127.0.0.1, server: example.com, request: "HEAD / HTTP/1.1", upstream: "http://127.0.0.1:8081/", host: "127.0.0.1:8080"

So the error log matters twice: nginx needs its folder to start, and you need the log to find the cause of a 502. Read the latest lines with tail -n 50 /var/log/nginx/error.log, then start or fix the backend named after upstream:.

On cPanel servers

According to cPanel, its own NGINX with reverse proxy (the ea-nginx package, installed from WHM's NGINX Manager or EasyApache 4) moves Apache to another port, usually 81, and proxies dynamic requests to it. A 502 there usually means Apache is down. cPanel writes per-domain NGINX logs to /var/log/nginx/domains/, so if that folder is missing, recreate it as shown above. cPanel also says not to use the nginx command to stop or restart NGINX; use its script instead:

/usr/local/cpanel/scripts/restartsrv_nginx

CPNginx, the subject of the original version of this article, is a commercial third-party plugin, not part of cPanel. Its vendor still lists it, but cPanel now ships and supports its own NGINX, which is what we recommend on current cPanel and WHM servers. Do not run both at once. See Nginx on cPanel: Engintron or cPanel NGINX Reverse Proxy and How to Install CPNginx for the differences.

Related articles

On a Managed cPanel VPS our team looks after the web server for you. If nginx still will not start after these steps, open a support ticket with the output of nginx -t.

Official documentation: nginx error_log directive, cPanel: NGINX with Reverse Proxy, cPanel: default configuration of NGINX with reverse proxy.

Was this answer helpful?

Still stuck? Open a ticket