rewrite rule with consistent document structure

xrd nginx-forum at
Thu Jan 16 16:47:17 UTC 2020

I have a directory containing several directories with the exact same
structure. I'll be adding more and more directories with the exact same
structure. I want to make an NGINX rewrite rule that makes the URL as simple
as possible. 

For example, here is two of those directories. Notice each has a directory
called "public" and then has a bunch of files and other directories.

* directoryA (dir)
*** public (dir)
***** index.html (file)
***** images (dir)
******* image.png (file)
******* image.jpg (file)
* directoryB (dir)
*** public (dir)
***** index.html (file)
***** readme.html (file)
******* a (dir) 
********* b (dir)
*********** c (dir)
************* somefile.html
************* nested.jpg

I want to make it so that NGINX serves up the HTML and images from the
parent directory and omits the public directory from the URI. 

More explicitly, if the server is I would like someone
to be able to get https://example/directoryB/readme.html. And, if
readme.html has an <img src="a/b/c/nested.jpg"> tag inside it, that that
gets loaded correctly as well.

I've been trying various combinations of rewrite plus try_files but I'm
getting confused by the last, break, etc. And, I think a key distinction is
that I don't want to "rewrite" the URI. If I understand things correctly, I
don't want to have to
rewrite to (I would prefer not to
have the public directory available as a possible URI, but I suppose it
wouldn't hurt).

I'm struggling with this because I don't know how to "test" my rules without
tweaking the configuration, then restarting, tailing the log files, etc.
It's been very slow and I would love a better way, so if someone has a
suggestion on how , I'm happy to hear it.

Posted at Nginx Forum:,286755,286755#msg-286755

More information about the nginx mailing list