Splitting long lines in a nginx configuration file

Lekensteyn asked:

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)

My answer:


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.


View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.