24

I've seen people use excessive quotes:

add_header 'Access-Control-Allow-Origin' '*';

I've seen people use no quotes:

add_header Access-Control-Allow-Origin *;

Both work fine as far as I know, so when do you actually have to use quotes?

29

The exact answer is "never". You can either quote or \-escape some special characters like " " or ";" in strings (characters that would make the meaning of a statement ambiguous), so

add_header X-MyHeader "Test String;"; 

would work like

add_header X-MyHeader Test\ String\;;

In reality: Just use quotes :)


Edit: As some people love to nitpick: The not necessarily complete list of characters that can make a statement ambiguous is according to my understanding of the nginx config syntax:

<space> " ' { } ; $ \ 

and it might be necessary to escape $ and \ even in quoted strings to avoid variable expansion.

Unfortunately, I can't find a complete and authoritative list of such characters in the docs.

  • 1
    It's not always about nitpicking. Configuration file might be generated by a third-party software. Which might be provided with a template and variables to be interpolated. The software can't be sure what the variables can contain, so ideally it should know how to quote any string. – x-yuri Jan 26 at 14:16
5

Quotes are required for values which are containing space(s) and/or some other special characters, otherwise nginx will not recognize them. In your current example quotes make no difference, but anyway quoting values is a good practice/rule of thumb

  • 3
    What are the list of special characters. Is there documentation on this and how to escape them. I've seen both single and double quotes on the same lines... – Pork 'n' Bunny Sep 12 '17 at 15:23

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.