ThisCalculator

Everyday

nginx SSL + Varnish config test errors: fix

Updated · Standard formula, shown below

When you run nginx -t and get SSL or Varnish-related errors, the cause is almost always a missing or misconfigured SSL certificate path, a conflicting listen directive, or a broken upstream definition. This article walks through the most common nginx configuration test failures involving SSL and Varnish, explains why they happen, and gives step-by-step fixes you can apply immediately.

Why nginx -t fails with SSL and Varnish

nginx runs a configuration test every time you start or reload it. The test parses every file in the conf.d and sites-enabled directories, checks syntax, validates file paths, and verifies that directives are used in the correct context. SSL errors usually come from a certificate or key file that nginx cannot read, while Varnish errors typically involve an upstream block that points to a host or port that isn’t reachable. The test fails before nginx applies any changes, so your running server stays on the old configuration until the test passes.

Common SSL configuration test errors

These are the errors you are most likely to see and what they mean:

  • cannot load certificate – The path in ssl_certificate is wrong, the file is missing, or nginx doesn’t have read permission.
  • cannot load certificate key – The key file is missing, unreadable, or the key doesn’t match the certificate.
  • ssl_certificate directive is duplicate – You have the same certificate defined in both the http block and a server block, or in two server blocks that share the same listen address.
  • no "ssl_certificate" is defined – You set listen 443 ssl; but forgot to add the certificate directives.
  • SSL_CTX_use_PrivateKey_file failed – The key file is encrypted or in the wrong format (e.g., PKCS#8 instead of PEM).

To fix these, verify the file paths exist, check that the user nginx runs as (usually www-data or nginx) can read them, and confirm the certificate and key match. You can test the match with:

openssl x509 -noout -modulus -in cert.pem | openssl md5
openssl rsa -noout -modulus -in key.pem | openssl md5

The two hashes must be identical.

Common Varnish configuration test errors

Varnish itself has its own config test (varnishd -C -f /etc/varnish/default.vcl), but when nginx is the front-end or back-end to Varnish, the errors show up in nginx -t as upstream problems:

  • host not found in upstream – The proxy_pass or upstream block references a hostname that doesn’t resolve. Use an IP address or ensure the hostname is in /etc/hosts.
  • no resolver defined to resolve – nginx needs a resolver directive when using a hostname in proxy_pass that isn’t in the upstream block.
  • upstream prematurely closed connection – This is a runtime error, not a config test error, but it happens when Varnish isn’t listening on the expected port. Check varnishd -a :6081 and your nginx proxy_pass http://127.0.0.1:6081;.
  • invalid port in upstream – The port number is out of range or malformed. Ports must be between 1 and 65535.

For a typical setup where nginx terminates SSL and passes plain HTTP to Varnish, your nginx server block should look like:

server {
    listen 443 ssl;
    server_name example.com;
    ssl_certificate /etc/ssl/certs/example.com.crt;
    ssl_certificate_key /etc/ssl/private/example.com.key;
    location / {
        proxy_pass http://127.0.0.1:6081;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-Proto https;
    }
}

How to fix SSL and Varnish errors step by step

  1. Run the test and capture the exact error: nginx -t 2>&1 | tee /tmp/nginx-test.log. The error message includes the file name and line number.
  2. Open that file and inspect the line. For SSL, check the paths. For Varnish, check the upstream host and port.
  3. Verify file permissions: ls -l /etc/ssl/certs/your.crt. The nginx worker process must have read access. If not, chmod 644 for certificates and chmod 600 for keys.
  4. Check that Varnish is actually running: systemctl status varnish or varnishd -C -f /etc/varnish/default.vcl to test its config. If Varnish isn’t running, nginx will still pass the config test (it doesn’t check upstream availability), but you’ll get 502 errors at runtime.
  5. If using a hostname in proxy_pass, add a resolver: resolver 8.8.8.8; in the http block, or use an IP address.
  6. After fixing, run nginx -t again. If it says syntax is ok and test is successful, reload nginx with systemctl reload nginx.

How to choose between nginx in front of Varnish or Varnish in front of nginx

The most common architecture is nginx as the SSL terminator and Varnish as the cache. This works because Varnish doesn’t natively handle SSL (though Varnish Plus has h2 support). You put nginx on port 443, proxy to Varnish on 6081, and Varnish proxies to your origin server on 8080. Alternatively, you can put Varnish in front of nginx, but then you need a separate SSL terminator like hitch or use Varnish’s built-in TLS in the commercial version. For most open-source setups, nginx → Varnish → origin is simpler and avoids the need for additional software.

Common mistakes and misconceptions

  • Mixing up the order of SSL directives: ssl_certificate must come before ssl_certificate_key? Actually, order doesn’t matter, but both must be in the same server block.
  • Using the same port for both SSL and non-SSL: You cannot have two server blocks listening on the same port with different SSL settings. Use listen 443 ssl; for HTTPS and listen 80; for HTTP.
  • Forgetting to include the full chain: If your certificate is signed by an intermediate CA, you must include the intermediate certificate in the ssl_certificate file. Otherwise, some browsers will fail.
  • Assuming Varnish is the problem when it’s not: Many times the error is simply a typo in the upstream port. Double-check that Varnish is listening on the same port you put in proxy_pass.

How to test the SSL certificate before reloading

Before you reload nginx, you can verify the certificate chain with:

openssl s_client -connect localhost:443 -servername example.com

This shows the certificate chain and whether it’s valid. If you see verify error, your chain is incomplete. Also, use nginx -t to catch syntax errors, but it won’t catch certificate expiry. Check expiry with openssl x509 -enddate -noout -in cert.pem.

Frequently Asked Questions

Why does nginx -t pass but I still get 502 Bad Gateway?

The config test only checks syntax and file paths, not whether the upstream server (Varnish) is actually running. If Varnish is down or listening on a different port, nginx will still load and then return 502 when it can’t connect. Check systemctl status varnish and verify the port with ss -tlnp | grep 6081.

Can I use the same SSL certificate for multiple domains?

Yes, if the certificate covers all the domains (a SAN or wildcard certificate). You can reference the same file in multiple server blocks. For different domains with different certificates, you need separate server blocks with their own ssl_certificate directives.

What does “host not found in upstream” mean?

It means the hostname you used in proxy_pass or upstream doesn’t resolve to an IP address at the time nginx parses the config. Use an IP address like 127.0.0.1 or add a resolver directive to the http block if you must use a hostname.

How do I fix “ssl_certificate directive is duplicate”?

This happens when you define the same certificate in both the http block and a server block, or in two server blocks that listen on the same IP and port. Remove the duplicate from one of them. Usually, you only need it in the specific server block.

Should I put Varnish before or after nginx for SSL?

For open-source Varnish, put nginx in front to handle SSL, then proxy to Varnish. This avoids needing an extra SSL terminator. If you put Varnish in front, you’ll need to handle SSL separately, which adds complexity.

Why does nginx -t fail when Varnish is not running?

It shouldn’t. nginx -t doesn’t check if the upstream is reachable. If it fails, the error is likely about the upstream syntax, not the fact that Varnish is down. Fix the syntax, then start Varnish.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.