Nginx configuration to enable ACME Challenge support on all HTTP virtual hosts

Post Date: November 10, 2020
Last Updated: November 18, 2026

Introduction

This guide explains how to enable ACME challenge support in Nginx on every HTTP virtual host. Certificate authorities such as Let’s Encrypt prove you control a domain by requesting a small file from /.well-known/acme-challenge/ on port 80. If Nginx can’t serve that path, certificate issuance and renewal fail.

The original guide added the rule to each site’s configuration, and its example placed the rule only in the port 443 block. This version moves it to the port 80 block, where the challenge arrives. It also uses one reusable snippet for all sites and adds a redirect fix, tests and troubleshooting. The original also had typos in the folder and file names (unde, sites-avaialble), which are corrected here.

Warning: If you enable ACME challenge support in the wrong server block, or place an HTTP-to-HTTPS return 301 at server level, certificate renewals can fail silently. Back up your Nginx configuration and run nginx -t before every reload.

Prerequisites

Before you enable ACME challenge support, make sure you have:

  • Root or sudo access to the server
  • Nginx installed, with your sites in /etc/nginx/sites-available/ (Debian and Ubuntu) or /etc/nginx/conf.d/ (RHEL-based systems)
  • DNS A (and AAAA, if used) records that point the domain to this server
  • Port 80 open in the firewall and in any cloud security group
  • A backup of your Nginx configuration: sudo cp -r /etc/nginx /etc/nginx.bak

Architecture

  • The certificate authority (CA) requests http://example.com/.well-known/acme-challenge/<token> on port 80
  • A shared folder, /var/www/letsencrypt, holds the token files that the client creates
  • A reusable Nginx snippet defines a location ^~ rule that serves that folder as text/plain
  • A second rule returns 404 for the bare directory, so nobody can list it
  • Every HTTP server block includes the snippet, and the rest of its traffic redirects to HTTPS or goes to your application

The image below shows the workflow. For the official references, see the Let’s Encrypt page on challenge types and the Nginx documentation on the location directive.

Steps to Enable ACME Challenge Support in Nginx

Short answer: To enable ACME challenge support on all HTTP virtual hosts, create a shared webroot with a .well-known/acme-challenge folder, save a location ^~ /.well-known/acme-challenge/ rule in an Nginx snippet, include that snippet in every port 80 server block, run nginx -t, reload Nginx, and test with curl.

1. Create the Challenge Folder and a Test File

sudo mkdir -p /var/www/letsencrypt/.well-known/acme-challenge
echo "acme-test" | sudo tee /var/www/letsencrypt/.well-known/acme-challenge/test.txt
sudo chown -R www-data:www-data /var/www/letsencrypt
sudo chmod -R 755 /var/www/letsencrypt

Note: The original guide created these folders inside each site’s document root. One shared folder is easier to manage when you have many sites. On RHEL-based systems, the Nginx user is nginx instead of www-data.

2. Create a Reusable Snippet

sudo nano /etc/nginx/snippets/acme-challenge.conf
# Serve legitimate ACME challenge requests (/.well-known/acme-challenge/xxxxxxxx)
location ^~ /.well-known/acme-challenge/ {
    default_type "text/plain";
    root /var/www/letsencrypt;
}

# Hide the directory itself and return 404
location = /.well-known/acme-challenge/ {
    return 404;
}

The ^~ modifier makes this rule win over regex locations, so your proxy or PHP rules never intercept the challenge. On RHEL-based systems, save the file in /etc/nginx/conf.d/ with a name such as acme-challenge.inc, since snippets/ may not exist.

3. Include the Snippet in Every HTTP Server Block

This is the step that lets you enable ACME challenge support on every site. Add one include line to the port 80 block of each virtual host, for example /etc/nginx/sites-available/example.com:

server {
    listen 80;
    listen [::]:80;
    server_name example.com www.example.com;

    include snippets/acme-challenge.conf;

    location / {
        return 301 https://$host$request_uri;
    }
}

Your HTTPS block stays as it is. For example, a reverse proxy to an app on port 8081:

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    server_name example.com www.example.com;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

    location / {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_pass http://localhost:8081;
    }
}

Note: The original example put the ACME rule in the 443 block. The CA connects to port 80 first, so the rule belongs in the port 80 block.

4. Keep the Redirect Inside location /

Put the HTTP-to-HTTPS redirect inside location /, as shown above. A server-level return 301 ...; runs before location matching, so it redirects the challenge request too and the check can fail.

5. Test the Configuration and Reload Nginx

sudo nginx -t
sudo systemctl reload nginx

nginx -t must report that the syntax is ok and the test is successful. Fix any error before reloading.

6. Verify That You Can Enable ACME Challenge Support Correctly

Request the test file and the bare directory over HTTP:

curl -i http://example.com/.well-known/acme-challenge/test.txt
curl -i http://example.com/.well-known/acme-challenge/

The first request should return 200 OK with Content-Type: text/plain and the text acme-test. The second should return 404. Then run a dry-run issuance with Certbot, which uses the staging server and doesn’t count against rate limits:

sudo certbot certonly --webroot -w /var/www/letsencrypt -d example.com -d www.example.com --dry-run

When it succeeds, delete the test file:

sudo rm /var/www/letsencrypt/.well-known/acme-challenge/test.txt

For the webroot options, see the Certbot documentation.

Troubleshooting

If you cannot enable ACME challenge support or the dry run fails, check these:

  • “Invalid response from http://example.com/.well-known/acme-challenge/…: 404”: The file is not where Nginx looks. Confirm the root path in the snippet matches Certbot’s -w path, and that the snippet is included in the port 80 block
  • “Timeout during connect (likely firewall problem)”: Port 80 is blocked. Open it in the server firewall and in your cloud security group
  • “Invalid response … 403 Forbidden”: Nginx can’t read the folder. Check ownership and permissions (Step 1) and SELinux contexts on RHEL-based systems
  • “nginx: [emerg] “location” directive is not allowed here”: The include line sits outside a server block. Move it inside
  • “nginx: [emerg] open() “/etc/nginx/snippets/acme-challenge.conf” failed (2: No such file or directory)”: The snippet file name or path is wrong
  • You enable ACME challenge support but the challenge still redirects: A server-level return 301 is running first. Move the redirect inside location / (Step 4)
  • The domain works on IPv4 but fails the check: An AAAA record points to another server. Fix it or remove it
  • The site sits behind a proxy or CDN: Make sure HTTP requests for /.well-known/acme-challenge/ reach your origin, or use the DNS challenge instead

Conclusion

To enable ACME challenge support in Nginx, create a shared webroot, save the location ^~ /.well-known/acme-challenge/ rule in a snippet, include it in every port 80 server block, and keep your redirect inside location /. Test with nginx -t, curl and a Certbot dry run before relying on it. Once the snippet is in place, adding a new site takes a single include line.

Frequently Asked Questions

Why do I need to enable ACME challenge support on port 80?
The HTTP-01 challenge is validated over port 80. If port 80 is closed or the path isn’t served, issuance and renewal fail.

Do I need this if I use certbot --nginx?
Not always. The Nginx plugin can edit your configuration and answer the challenge itself. The webroot method in this guide is useful when you manage Nginx files by hand or want renewals to work without changing them.

Why return 404 for the directory?
It hides the folder, so nobody can browse it. Only requests for a specific token file are served.

Can I use one shared webroot for all sites?
Yes. Every virtual host includes the same snippet, and Certbot uses the same -w path for each domain.

After you enable ACME challenge support, these guides help with web server security and SSL.

How to Install Let’s Encrypt with Apache on CentOS 7
Get a free SSL certificate with Certbot on Apache.

How to Block IPs via .htaccess File
Deny unwanted visitors on an Apache web server.

Talk to Our Technology Experts

Need help with SSL certificates, HTTPS setup, or securing your web servers? Our team can configure and maintain your certificates and server protection through our Security Services.

Connect with our technology experts.

admin

Our team has expertise across software and web development, WordPress, e-commerce, mobile applications, UI/UX, cloud and infrastructure, DevOps, CI/CD, API integration, security, testing, automation, and technical support. The team also works with AI-based software solutions, LLMs, AI workflows, AI agents, and intelligent application development to help businesses automate processes and build smarter digital solutions. We focus on developing, deploying, maintaining, and optimising secure, scalable, and reliable technology solutions while helping businesses adopt modern technologies and drive digital transformation.

Leave a Comment

Scroll to Top