大网数据广告活动
服务器运维问答

Can multiple nginx statements be on the same line in a config file?

· 52CE 我爱测

The Problem I'm creating a docker image which dynamically creates nginx config based on environment variables. I would like to minimise the number of environment variables required. I have a config template file that looks someth…

问题原文

原帖链接 · Max Colledge · CC BY-SA 4.0

The Problem

I'm creating a docker image which dynamically creates nginx config based on environment variables. I would like to minimise the number of environment variables required.

I have a config template file that looks something like this:

server {
...
  $ALLOW_DENY
...
}

And I'd like to configure the template like this

export ALLOW_DENY="allow 11.11.11.11; deny all;"
envsubst '\$ALLOW_DENY' < sites-available/site.template > sites-available/my-site

My problem is that I don't want to create multiple environment variables for each allow or deny statement (some configurations of this file might require none, where others might require 5 or 6) but I can't find a way to make envsubst accept newline characters, and I'm not sure if multiple nginx commands are allowed on a single line.

For reference, I'm going to be loading all my environment variables using aws ssm and I don't think it accepts multi-line environment variables like this:

export $ALLOW_DENY="  allow 11.11.11.11;
  deny all;"

tl;dr

Is this valid?

server {
...
  allow 11.11.11.11; deny all;
...
}

已采纳解答

原帖链接 · anx · CC BY-SA 4.0

Yes. Though I highly recommend making liberal use of whitespaces, line breaks and include statements to increase readability - ultimately its up to you to skip line breaks where convenient - nginx only treats line breaks special to be able to point out the line where configuration parser errors occurred.

nginx configuration files are nothing more than semicolon-separated directives in curly-brace-delimited blocks - line breaks are irrelevant other than being parsed similar to spaces and tabs:

A simple directive consists of the name and parameters separated by spaces and ends with a semicolon (;). A block directive has the same structure as a simple directive, but instead of the semicolon it ends with a set of additional instructions surrounded by braces ({ and }).

The only reason you do not want to too much of your configuration in a single line is readability - and comments are only possible on a line basis:

The rest of a line after the # sign is considered a comment.

Nginx Beginner’s Guide

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2019-08-08)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。