In order to not pass garbage to the back-end, I have a strict regex for a location directive. It looks like this:

location ^~ "/(some|stuff|more|bar|etc(-testing)?)/[a-zA-Z0-9]+/...(more|restrict).ext {
    # other directives

I would like to fold the line at 80 chars, is there a way to split up the configuration? The following results in a syntax error, but is something I am looking for:

location ^~ "/(some|stuff|more|bar|etc(-testing)?)/[a-zA-Z0-9]+/"\
            "...(more|restrict).ext" {
# results in a literal newline (%0A) being accepted
location ^~ "/(some|stuff|more|bar|etc(-testing)?)/[a-zA-Z0-9]+/
...(more|restrict).ext" {

I could not find hints in the documentation (http://wiki.nginx.org/ConfigNotation nor http://wiki.nginx.org/HttpCoreModule#location mention anything about folding lines)

  • Could you build up your string from multiple fragments interpolated? (I have no idea)
    – bsb
    Aug 23, 2015 at 22:01
  • 2
    @bsb You cannot use variables in location expressions, so no, you cannot set variables and use string interpolation.
    – Lekensteyn
    Aug 24, 2015 at 16:53

1 Answer 1


I don't think you can do this.

nginx treats all whitespace equally, so even if you tried to split your string like that, and nginx could parse it the way you intended, you would wind up with a regex with a bunch of whitespace in it, which I'm sure isn't what you want. If nginx couldn't parse it, which is more likely, you'd just get a syntax error.

You're just going to have to live with a few long lines, or make less complex regular expressions.

  • 1
    Well, if there cannot be leading spaces, I can live with that. But a newline seems to be interpreted literally as well.
    – Lekensteyn
    Feb 2, 2014 at 9:20
  • It really is not the end of the world. :) Feb 2, 2014 at 15:48
  • 1
    For now I am stuck with a location ".../[a-zA-Z0-9_...-]+$" pattern followed by another location ~ "\.pkg\.tar\.xz$" inside. Thanks for your answer, hopefully some day a better answer shows up ;)
    – Lekensteyn
    Feb 2, 2014 at 20:02
  • 1
    Note also that the length limit for nginx configuration line seems to be about 4k characters. If you exceed that then nginx fails to parse the line and may even return some seemingly irrelevant parse error. So if you have really long lines then you may need to find a way to split them into multiple "if" conditions. Sep 29, 2020 at 8:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.