docusaurus icon indicating copy to clipboard operation
docusaurus copied to clipboard

Add deployment guide for NGINX

Open rawsashimi1604 opened this issue 11 months ago • 12 comments

Have you read the Contributing Guidelines on issues?

Description

Hey all, feel like we could enhance the Deployment docs by providing a quick getting started guide on hosting docusaurus static build files via a dockerised NGINX. Would be willing to write a quick guide!

Self-service

  • [x] I'd be willing to address this documentation request myself.

rawsashimi1604 avatar Jan 27 '25 16:01 rawsashimi1604

Hi

Honestly I'd prefer to not add this guide to our official docs, and would prefer if it was an external resource we simply link to.

The reason is: I don't have the skills to maintain it. Introducing it means that it's now my responsibility to ensure this "official" guide remains up to date over time and for all the upcoming versions of Nginx.

There's an infinite number of ways to host static websites, using various software. We can't document them all officially: we count on our community for that.

slorber avatar Jan 27 '25 16:01 slorber

The important things to host a website built by Docusaurus by Nginx/Apache are:

  • How to add/remove a/the trailing slash in the URL
  • How long and which directories should be cached

I think the former is stable, and the latter can be shared with Apache.

tats-u avatar Feb 02 '25 11:02 tats-u

The latter:

  • /static/: several weeks?
  • /assets/: maybe immutable can be added to Cache-Control. Several months–half or one year?

I think an official guideline for them would be appreciated.

tats-u avatar Feb 02 '25 11:02 tats-u

I would also appreciate official guide to caching in nginx mentioned by @tats-u , it would be immensely helpful.

my hypothesis here is this, feedback welcome, so that we can arrive at idiomatic configuration in nginx for file /etc/nginx/conf.d/default.conf is this:

server {
    listen 80;
    index index.html;
    root /var/www/html;
    
    location ~* \.html$ {
        add_header Cache-Control "no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0" always;
    }
    
    # /static/ directory - medium-term caching
    location /static/ {
        expires 4w;
        add_header Cache-Control "public, max-age=2419200";
        try_files $uri =404;
    }
    
    # All other static assets - long-term caching
    location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff2?|ttf|eot|webp)$ {
        expires 1y;
        add_header Cache-Control "public, max-age=31536000, immutable";
        try_files $uri =404;
    }

    location ~* ^(.+)/$ {
        return 301 $1;
    }

    
    location / {
        try_files $uri $uri/index.html $uri/ =404;
    }
}

jozefRudy avatar Feb 26 '25 11:02 jozefRudy

You may want to redirect from a URL with a trailing / to that without it. Also you should use the port 443 with ssl h2 instead of 80 and add the redirection server from 80 to 443 if you have a TLS certificate.

tats-u avatar Mar 09 '25 13:03 tats-u

You may want to redirect from a URL with a trailing / to that without it. Also you should use the port 443 with ssl h2 instead of 80 and add the redirection server from 80 to 443 if you have a TLS certificate.

updated, thanks

jozefRudy avatar Mar 09 '25 13:03 jozefRudy

@jozefRudy, error_page 404 404.html; seems also in deal.

And also location ~* ^(.*)/$ { return 301 $1; } gives for me recursion error.

bergentroll avatar Apr 01 '25 14:04 bergentroll

gives for me recursion error.

Only the top page? If so .+ instead of .* works?

tats-u avatar Apr 02 '25 09:04 tats-u

@jozefRudy, error_page 404 404.html; seems also in deal.

And also location ~* ^(.*)/$ { return 301 $1; } gives for me recursion error.

yes, as @tats-u mentioned, this might fix it?

@bergentroll could you confirm?

location ~* ^(.+)/$ {
    return 301 $1;
}

jozefRudy avatar Apr 02 '25 10:04 jozefRudy

You may want to redirect from a URL with a trailing / to that without it. Also you should use the port 443 with ssl h2 instead of 80 and add the redirection server from 80 to 443 if you have a TLS certificate.

sure, but that is already general config unrelated to docusaurus i think. maybe you are behind traefik which does that and this is purely docusaurus specific. (hence ommitted 443 redirection).

jozefRudy avatar Apr 02 '25 10:04 jozefRudy

@jozefRudy, confirming, .+ works.

bergentroll avatar Apr 02 '25 10:04 bergentroll

I can write a comprehensive deployment guide for hosting Docusaurus sites with dockerized NGINX. I understand the need for practical deployment documentation and will cover nginx configuration, docker setup, static file serving optimization, and best practices for production environments.

dohrisalim avatar Nov 09 '25 08:11 dohrisalim