Best way to serve your Jekyll blog out of an S3 bucket, via Nginx:

        server {
                  listen  80;
                  server_name sitename.com;
        
                  access_log /var/dir/access.log;
                  error_log /var/dir/error.log;
                  rewrite_log on;
                  error_page 404 = @fallback-2;
                  error_page 500 https://jekyllhub.com/errors/error_500;
        
                  location / {
                    # match anything with an extension
                    rewrite /[^/]*\.\w+$  "/sitename${uri}?" break;        
                    
                    # ending in normal word
                    rewrite /\w+$      "/sitename${uri}/index.html?" break;
                    
                    # ending in /
                    rewrite /$         "/sitename${uri}index.html?" break;
                    
                    # catch all
                    rewrite ^          "/sitename${uri}?" break;
        
                    proxy_pass http://bucket.s3.amazonaws.com;
                    proxy_intercept_errors on;
                    index index.html;
                  }

                  location @fallback-2 {
                     access_log /var/dir/fallback.log;
                     error_page 404 https://jekyllhub.com/errors/error_404;
                     
                     # no dir with this name, try file
                     # note your req path here is WHAT YOU REWROTE IT TO in the first req
                     rewrite ^/sitename/(.*)/index.html$  "/sitename/$1.html" break;
                     proxy_intercept_errors on;
                     proxy_pass http://bucket.s3.amazonaws.com;
                  }
        }

Setting up my blog with Jekyll, I wanted to proxy it from S3 through Nginx. However, it took me a while to get the urls right - I wanted /page2 to work even though page2 is just a directory with a /index.html , and I wanted /about to point to about.html in the root directory.

My solution was to try the directory version first, catch the 404, and try to rewrite again on fallback. One thing the Nginx docs didn’t make clear is that when you call rewrite and catch 404 with a fallback, the uri / path is no longer what the original request was, but is now what you rewrote it to. So if you want to rewrite again, you have to account for that.

Hope this helps someone.