Skip to content

Commit 95907ee

Browse files
Add documentation about configuration inheritance
1 parent 7d35185 commit 95907ee

File tree

1 file changed

+104
-1
lines changed

1 file changed

+104
-1
lines changed

docs/nginx_tips.md

Lines changed: 104 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ a look on the main `nginx.conf` file from the parent image. It has a couple of
1212
standard settings included, but on the last line we can se that it opens the
1313
`/etc/nginx/conf.d/` folder and loads any file that ends with `.conf`.
1414

15-
```conf
15+
```bash
1616
user nginx;
1717
worker_processes auto;
1818

@@ -62,6 +62,106 @@ and change any of the files within it, the changes will be visible inside the
6262
container.
6363

6464

65+
## Configuration Inheritance
66+
To keep this explanation simple, yet useful, we begin by stating that the Nginx
67+
configuration is split into four blocks. Variables and settings declared in an
68+
outer block (e.g. the Global block) will be inherited by an inner block (e.g.
69+
the Server block) unless you change it inside this inner block.
70+
71+
So in the example below I have added comments with the current value of the
72+
[`keepalive_timeout`][9] setting in each block:
73+
74+
```bash
75+
# -- Global/main block --
76+
# keepalive_timeout = 60 (The default value)
77+
http {
78+
# -- HTTP block --
79+
keepalive_timeout = 30 # The value has now changed to 30
80+
server {
81+
# -- Server block --
82+
# keepalive_timeout = 30 (value inherited from http block)
83+
location /abc/ {
84+
# -- Location block nbr 1 --
85+
keepalive_timeout = 50 # The value has now changed to 50
86+
}
87+
location /xyz/ {
88+
# -- Location block nbr 2 --
89+
# keepalive_timeout = 30 (value inherited from server block)
90+
}
91+
}
92+
}
93+
```
94+
95+
This is pretty straight forward for the settings that are only one value, but
96+
the commonly used [`proxy_set_header`][10] setting can be declared multiple
97+
times in order to add multiple values to it, and [its inheritance][11] works a
98+
bit differently. The following is true of all of the settings that can be
99+
declared multiple times.
100+
101+
In the example below we want to add two headers to all requests, so we
102+
declare them in the `http` block. This builds a map/dictionary with the
103+
key-value pairs we want, and this will be inherited to all the location blocks.
104+
However, in the first location block we want to **add** another header, but
105+
doing it in this way will instead overwrite the current one with just this new
106+
header.
107+
108+
```bash
109+
http {
110+
proxy_set_header key1 value1;
111+
proxy_set_header key2 value2;
112+
server {
113+
# proxy_headers: {
114+
# "key1": "value1"
115+
# "key2": "value2"
116+
# }
117+
location /abc/ {
118+
proxy_set_header key3 value3;
119+
# proxy_headers: {
120+
# "key3": "value3"
121+
# }
122+
}
123+
location /xyz/ {
124+
# proxy_headers: {
125+
# "key1": "value1"
126+
# "key2": "value2"
127+
# }
128+
}
129+
}
130+
}
131+
```
132+
133+
The suggested solution to this problem is to create a separate file with the
134+
"common" headers, and then `include` this file where needed. So in our case we
135+
create the file `/etc/nginx/common_headers` with the following content:
136+
137+
```
138+
proxy_set_header key1 value1;
139+
proxy_set_header key2 value2;
140+
```
141+
142+
and then change the config to the following which would make the special
143+
location block have all the desired headers:
144+
145+
```bash
146+
http {
147+
include common_headers;
148+
server {
149+
location /abc/ {
150+
include common_headers;
151+
proxy_set_header key3 value3;
152+
# proxy_headers: {
153+
# "key1": "value1"
154+
# "key2": "value2"
155+
# "key3": "value3"
156+
# }
157+
}
158+
location /xyz/ {
159+
}
160+
}
161+
}
162+
```
163+
164+
65165
## Reject Unknown Server Name
66166
When setting up server blocks there exist a setting called `default_server`,
67167
which means that Nginx will use this server block in case it cannot match
@@ -139,3 +239,6 @@ minimal changes to the original.
139239
[6]: https://github.com/AxisCommunications/docker-nginx-ldap
140240
[7]: https://github.com/nginxinc/docker-nginx/tree/master/entrypoint
141241
[8]: hhttps://medium.com/@jonsbun/why-need-to-be-careful-when-mounting-single-files-into-a-docker-container-4f929340834
242+
[9]: https://nginx.org/en/docs/http/ngx_http_upstream_module.html#keepalive_timeout
243+
[10]: https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_set_header
244+
[11]: https://stackoverflow.com/a/32126596

0 commit comments

Comments
 (0)