Configuration guides

Remove Nginx comments and keep the hashes that are data

·

A hash has different effects depending on where it occurs in an Nginx configuration. Compare the recognized comments with three literal hash examples before sharing a cleaned copy.

Nginx Config Comment Remover

Compare comments and literal hashes in the example

Select Load example and Remove comments. The report removes # Field app, # reviewed separately and # response label. The first line becomes blank, so the original line positions are still useful.

The text "report #1; {draft}", blue#green and the escaped hash before internal stay in the output. The tokenizer sees those hashes within argument tokens.

Compare comments and literal hashes in the example
Source textTreatment
# response label after the semicolonRemoved through the end of that line
"report #1; {draft}"Retained as a quoted argument
blue#greenRetained as a single unquoted token

Nginx configuration syntaxNginx 1.28 configuration tokenizer

Use the file picker to keep original line endings

Open a UTF-8 copy to preserve its supplied line endings. Run the remover without editing the source field, then download nginx-clean.conf. Reopen it and compare the line count and the include directive.

Leading and trailing spaces outside a comment span are preserved. You may see spaces followed by a line break where an inline comment originally appeared.

Nginx 1.28 configuration tokenizer

Correct an unclosed quote before stripping comments

Remove the closing quote after {draft} in the example and check again. The remover reports the opening quote's line and column because subsequent hashes could belong to that argument.

Restore the closing quote and rerun the remover. Use Nginx Validator for block and terminator checks on the cleaned text.

Nginx 1.28 configuration tokenizer

Common questions

What if the same line contains a value hash and a trailing comment?

The value hash stays with the argument. The separate comment begins at a token boundary and is removed through the end of the line.

Will removing comments tidy the indentation too?

The remover leaves indentation and other surrounding text in place. Use the formatter if you want to reindent the cleaned configuration.

Sources

Put it into practice.

Related guides