20

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)

Lekensteyn
  • 6,111
  • 6
  • 37
  • 55
  • Could you build up your string from multiple fragments interpolated? (I have no idea) – bsb Aug 23 '15 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 '15 at 16:53

1 Answers1

11

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.

Michael Hampton
  • 237,123
  • 42
  • 477
  • 940
  • 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 02 '14 at 09:20
  • It really is not the end of the world. :) – Michael Hampton Feb 02 '14 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 02 '14 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. – Roland Pihlakas Sep 29 '20 at 08:24