You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

475 lines
20 KiB

nginx-util: use UCI for server configuration **tl;dr:** The functions `{add,del}_ssl` modify a server section of the UCI config if there is no `.conf` file with the same name in `/etc/nginx/conf.d/`. Then `init_lan` creates `/var/lib/nginx/uci.conf` files by copying the `/etc/nginx/uci.conf.template` and standard options from the UCI config; additionally the special path `logd` can be used in `{access,error}_log`. The init does not change the configuration beside re-creating self-signed certificates when needed. This is also the only purpose of the new `check_ssl`, which is installed as yearly cron job. **Initialization:** Invoking `nginx-util init_lan` parses the UCI configuration for package `nginx`. It creates a server part in `/var/lib/nginx/uci.conf` for each `section server '$name'` by copying all UCI options but the following: * `option uci_manage_ssl` is skipped. It is set to 'self-signed' by `nginx-util add_ssl $name`, removed by `nginx-util del_ssl $name` and used by `nginx-util check_ssl` (see below). * `logd` as path in `error_log` or `access_log` writes them to STDERR respective STDOUT, which are fowarded by Nginx's init to the log daemon. Specifically: `option error_log 'logd'` becomes `error_log stderr;` and `option access_log 'logd openwrt'` becomes `access_log /proc/self/fd/1 openwrt;` Other `[option|list] key 'value'` entries just become `key value;` directives. The init.d calls internally also `check_ssl` for rebuilding self-signed SSL certificates if needed (see below). And it still sets up `/var/lib/nginx/lan{,_ssl}.listen` files as it is doing in the current version (so they stay available). **Defaults:** The package installs the file `/etc/nginx/restrict_locally` containing allow/deny directives for restricting the access to LAN addresses by including it into a server part. The default server '_lan' includes this file and listens on all IPs (instead of only the local IPs as it did before; other servers do not need to listen explicitly on the local IPs anymore). The default server is contained together with a server that redirects HTTP requests for inexistent URLs to HTTPS in the UCI configuration file `/etc/config/nginx`. Furthermore, the packages installs a `/etc/nginx/uci.conf.template` containing the current setup and a marker, which will be replaced by the created UCI servers when calling `init_lan`. **Other:** If there is a file named `/etc/nginx/conf.d/$name.conf` the functions `init_lan`, `add_ssl $name` and `del_ssl $name` will use that file instead of a UCI server section (this is similar to the current version). Else it selects the UCI `section server $name`, or, when there is no such section, it searches for the first one having `option server_name '… $name …'`. For this section: * `nginx-util add_ssl $name` will add to it: `option uci_manage_ssl 'self-signed'` `option ssl_certificate '/etc/nginx/conf.d/$name.crt'` `option ssl_certificate_key '/etc/nginx/conf.d/$name.key'` `option ssl_session_cache 'shared:SSL:32k'` `option ssl_session_timeout '64m'` If these options are already present, they will stay the same; just the first option `uci_manage_ssl` will always be changed to 'self-signed'. The command also changes all `listen` list items to use port 443 and ssl instead of port 80 (without ssl). If they stated another port than 80 before, they are kept the same. Furthermore, it creates a self-signed SSL certificate if necessary, i.e., if there is no *valid* certificate and key at the locations given by the options `ssl_certificate` and `ssl_certificate_key`. * `nginx-util del_ssl $name` checks if `uci_manage_ssl` is set 'self-signed' in the corresponding UCI section. Only then it removes all of the above options regardless of the value looking just at the key name. Then, it also changes all `listen` list items to use port 80 (without ssl) instead of port 443 with ssl. If stating another port than 443, they are kept the same. Furthermore, it removes the SSL certificate and key that were indicated by `ssl_certificate{,_key}`. * `nginx-util check_ssl` looks through all server sections of the UCI config for `uci_manage_ssl 'self-signed'`. On every hit it checks if the SSL certificate-key-pair indicated by the options `ssl_certificate{,_key}` is expired. Then it re-creates a self-signed certificate. If there exists at least one `section server` with `uci_manage_ssl 'self-signed'`, it will try to install itself as cron job. If there are no such sections, it removes that cron job if possible. For installing a ssl certificate and key managed by another app, you can call: `nginx-util add_ssl $name $manager $crtpath $keypath` Hereby `$name` is as above, `$manager` is an arbitrary string, and the the ssl certificate and its key are indicated by their absolute path. If you want to remove the directives again, then you can use: `nginx-util del_ssl $name $manager` Signed-off-by: Peter Stadler <peter.stadler@student.uibk.ac.at>
4 years ago
  1. #!/bin/sh
  2. # This is a template copy it by: ./README.sh | xclip -selection c
  3. # to https://openwrt.org/docs/guide-user/services/webserver/nginx#configuration
  4. NGINX_UTIL="/usr/bin/nginx-util"
  5. EXAMPLE_COM="example.com"
  6. MSG="
  7. /* Created by the following bash script that includes the source of some files:
  8. * https://github.com/openwrt/packages/net/nginx-util/files/README.sh
  9. */"
  10. eval $("${NGINX_UTIL}" get_env)
  11. code() {
  12. local file
  13. [ $# -gt 1 ] && file="$2" || file="$(basename "$1")"
  14. printf "<file nginx %s>\n%s</file>" "$1" "$(cat "${file}")";
  15. }
  16. ifConfEcho() {
  17. sed -nE "s/^\s*$1=\s*(\S*)\s*\\\\$/\n$2 \"\1\";/p" ../../nginx/Makefile;
  18. }
  19. cat <<EOF
  20. ===== Configuration =====${MSG}
  21. The official Documentation contains a
  22. [[https://docs.nginx.com/nginx/admin-guide/|Admin Guide]].
  23. Here we will look at some often used configuration parts and how we handle them
  24. at OpenWrt.
  25. At different places there are references to the official
  26. [[https://docs.nginx.com/nginx/technical-specs/|Technical Specs]]
  27. for further reading.
  28. **tl;dr:** When starting Nginx by ''/etc/init.d/nginx'', it creates its main
  29. configuration dynamically based on a minimal template and the
  30. [[docs:guide-user:base-system:uci|🡒UCI]] configuration.
  31. The UCI ''/etc/config/nginx'' contains initially:
  32. | ''config server '${LAN_NAME}''' | \
  33. Default server for the LAN, which includes all ''${CONF_DIR}*.locations''. |
  34. | ''config server '_redirect2ssl''' | \
  35. Redirects inexistent URLs to HTTPS. |
  36. It enables also the ''${CONF_DIR}'' directory for further configuration:
  37. | ''${CONF_DIR}\$NAME.conf'' | \
  38. Is included in the main configuration. \
  39. It is prioritized over a UCI ''config server '\$NAME' ''. |
  40. | ''${CONF_DIR}\$NAME.locations'' | \
  41. Is include in the ''${LAN_NAME}'' server and can be re-used for others, too. |
  42. | ''$(dirname "${CONF_DIR}")/restrict_locally'' | \
  43. Is include in the ''${LAN_NAME}'' server and allows only accesses from LAN. |
  44. Setup configuration (for a server ''\$NAME''):
  45. | ''$(basename ${NGINX_UTIL}) [${ADD_SSL_FCT}|del_ssl] \$NAME'' | \
  46. Add/remove a self-signed certificate and corresponding directives. |
  47. | ''uci set nginx.\$NAME.access_log='logd openwrt''' | \
  48. Writes accesses to Openwrt’s \
  49. [[docs:guide-user:base-system:log.essentials|🡒logd]]. |
  50. | ''uci set nginx.\$NAME.error_log='logd' '' | \
  51. Writes errors to Openwrt’s \
  52. [[docs:guide-user:base-system:log.essentials|🡒logd]]. |
  53. | ''uci [set|add_list] nginx.\$NAME.key='value' '' | \
  54. Becomes a ''key value;'' directive if the //key// does not start with //uci_//. |
  55. | ''uci set nginx.\$NAME=[disable|server]'' |\
  56. Disable/enable inclusion in the dynamic conf.|
  57. | ''uci set nginx.global.uci_enable=false'' | \
  58. Use a custom ''${NGINX_CONF}'' rather than a dynamic conf. |
  59. ==== Basic ====${MSG}
  60. We modify the configuration by changing servers saved in the UCI configuration
  61. at ''/etc/config/nginx'' and/or by creating different configuration files in the
  62. ''${CONF_DIR}'' directory.
  63. These files use the file extensions ''.locations'' and ''.conf'' plus ''.crt''
  64. and ''.key'' for SSL certificates and keys.((
  65. We can disable a single configuration file in ''${CONF_DIR}'' by giving it
  66. another extension, e.g., by adding ''.disabled''.))
  67. For the new configuration to take effect, we must reload it by:
  68. <code bash>service nginx reload</code>
  69. For OpenWrt we use a special initial configuration, which is explained in the
  70. section [[#openwrt_s_defaults|🡓OpenWrt’s Defaults]].
  71. So, we can make a site available at a specific URL in the **LAN** by creating a
  72. ''.locations'' file in the directory ''${CONF_DIR}''.
  73. Such a file consists just of some
  74. [[https://nginx.org/en/docs/http/ngx_http_core_module.html#location|
  75. location blocks]].
  76. Under the latter link, you can find also the official documentation for all
  77. available directives of the HTTP core of Nginx.
  78. Look for //location// in the Context list.
  79. The following example provides a simple template, see at the end for
  80. different [[#locations_for_apps|🡓Locations for Apps]]((look for
  81. [[https://github.com/search?utf8=%E2%9C%93&q=repo%3Aopenwrt%2Fpackages
  82. +extension%3Alocations&type=Code&ref=advsearch&l=&l=|
  83. other packages using a .locations file]], too.)):
  84. <code nginx ${CONF_DIR}example.locations>
  85. location /ex/am/ple {
  86. access_log off; # default: not logging accesses.
  87. # access_log /proc/self/fd/1 openwrt; # use logd (init forwards stdout).
  88. # error_log stderr; # default: logging to logd (init forwards stderr).
  89. error_log /dev/null; # disable error logging after config file is read.
  90. # (state path of a file for access_log/error_log to the file instead.)
  91. index index.html;
  92. }
  93. # location /eg/static { … }
  94. </code>
  95. All location blocks in all ''.locations'' files must use different URLs,
  96. since they are all included in the ''${LAN_NAME}'' server that is part of the
  97. [[#openwrt_s_defaults|🡓OpenWrt’s Defaults]].((
  98. We reserve the ''location /'' for making LuCI available under the root URL,
  99. e.g. [[https://192.168.1.1/|192.168.1.1/]].
  100. All other sites shouldn’t use the root ''location /'' without suffix.))
  101. We should use the root URL for other sites than LuCI only on **other** domain
  102. names, e.g., we could make a site available at https://${EXAMPLE_COM}/.
  103. In order to do that, we create [[#new_server_parts|🡓New Server Parts]] for all
  104. domain names.
  105. We can also activate SSL thereby, see
  106. [[#ssl_server_parts|🡓SSL Server Parts]].
  107. We use such server parts also for publishing sites to the internet (WAN)
  108. instead of making them available just locally (in the LAN).
  109. Via ''${CONF_DIR}*.conf'' files we can add directives to the //http// part of
  110. the configuration.
  111. If you would change the configuration ''$(basename "${UCI_CONF}").template''
  112. instead, it is not updated to new package's versions anymore.
  113. Although it is not recommended, you can also disable the whole UCI config and
  114. create your own ''${NGINX_CONF}''; then invoke:
  115. <code bash>uci set nginx.global.uci_enable=false</code>
  116. ==== New Server Parts ====${MSG}
  117. For making the router reachable from the WAN at a registered domain name,
  118. it is not enough letting the
  119. [[docs:guide-user:firewall:firewall_configuration|🡒firewall]] accept requests
  120. (typically on ports 80 and 443) and giving the name server the internet IP
  121. address of the router (maybe updated automatically by a
  122. [[docs:guide-user:services:ddns:client|🡒DDNS Client]]).
  123. We also need to set up virtual hosting for this domain name by creating an
  124. appropriate server section in ''/etc/config/nginx''
  125. (or in a ''${CONF_DIR}*.conf'' file, which cannot be changed using UCI).
  126. All such parts are included in the main configuration of OpenWrt
  127. ([[#openwrt_s_defaults|🡓OpenWrt’s Defaults]]).
  128. In the server part, we state the domain as
  129. [[https://nginx.org/en/docs/http/ngx_http_core_module.html#server_name|
  130. server_name]].
  131. The link points to the same document as for the location blocks in the
  132. [[#basic|🡑Basic Configuration]]: the official documentation for all available
  133. directives of the HTTP core of Nginx.
  134. This time look for //server// in the Context list, too.
  135. The server part should also contain similar location blocks as
  136. ++before.|
  137. We can re-include a ''.locations'' file that is included in the server part for
  138. the LAN by default.
  139. Then the site is reachable under the same path at both domains, e.g. by
  140. https://192.168.1.1/ex/am/ple as well as by https://${EXAMPLE_COM}/ex/am/ple.
  141. ++
  142. We can add directives to a server in the UCI configuration by invoking
  143. ''uci [set|add_list] nginx.${EXAMPLE_COM//./_}.key=value''.
  144. If the //key// is not starting with //uci_//, it becomes a ''key value;''
  145. ++directive.|
  146. Although the UCI config does not support nesting like Nginx, we can add a whole
  147. block as //value//.
  148. ++
  149. We cannot use dots in a //key// name other than in the //value//.
  150. In the following example we replace the dot in //${EXAMPLE_COM}// by an
  151. underscore for the UCI name of the server, but not for Nginx's //server_name//:
  152. <code bash>
  153. uci add nginx server &&
  154. uci rename nginx.@server[-1]=${EXAMPLE_COM//./_} &&
  155. uci add_list nginx.${EXAMPLE_COM//./_}.listen='80' &&
  156. uci add_list nginx.${EXAMPLE_COM//./_}.listen='[::]:80' &&
  157. uci set nginx.${EXAMPLE_COM//./_}.server_name='${EXAMPLE_COM}' &&
  158. uci add_list nginx.${EXAMPLE_COM//./_}.include=\
  159. '$(basename ${CONF_DIR})/${EXAMPLE_COM}.locations'
  160. # uci add_list nginx.${EXAMPLE_COM//./_}.location='/ { … }' \
  161. # root location for this server.
  162. </code>
  163. We can disable respective re-enable this server again by:
  164. <code bash>
  165. uci set nginx.${EXAMPLE_COM//./_}=disable # respective: \
  166. uci set nginx.${EXAMPLE_COM//./_}=server
  167. </code>
  168. These changes are made in the RAM (and can be used until a reboot), we can save
  169. them permanently by:
  170. <code bash>uci commit nginx</code>
  171. For creating a similar ''${CONF_DIR}${EXAMPLE_COM}.conf'', we can adopt the
  172. following:
  173. <code nginx ${CONF_DIR}${EXAMPLE_COM}.conf>
  174. server {
  175. listen 80;
  176. listen [::]:80;
  177. server_name ${EXAMPLE_COM};
  178. include '$(basename ${CONF_DIR})/${EXAMPLE_COM}.locations';
  179. # location / { … } # root location for this server.
  180. }
  181. </code>
  182. [[#openwrt_s_defaults|🡓OpenWrt’s Defaults]] include the UCI server
  183. ''config server '_redirect2ssl' ''.
  184. It acts as //default_server// for HTTP and redirects requests for inexistent
  185. URLs to HTTPS.
  186. For making another domain name accessible to all addresses, the corresponding
  187. server part should listen on port //80// and contain the FQDN as
  188. //server_name//, cf. the official documentation on
  189. [[https://nginx.org/en/docs/http/request_processing.html|request_processing]].
  190. Furthermore, there is a UCI server named ''${LAN_NAME}''.
  191. It is the //default_server// for HTTPS and allows connections from LAN only.
  192. It includes the file ''$(dirname "${CONF_DIR}")/restrict_locally'' with
  193. appropriate //allow/deny// directives, cf. the official documentation on
  194. [[https://nginx.org/en/docs/http/ngx_http_access_module.html|limiting access]].
  195. ==== SSL Server Parts ====${MSG}
  196. For enabling HTTPS for a domain we need a SSL certificate as well as its key and
  197. add them by the directives //ssl_certificate// respective
  198. //ssl_certificate_key// to the server part of the domain
  199. ([[https://nginx.org/en/docs/http/configuring_https_servers.html#sni|TLS SNI]]
  200. is supported by default).
  201. The rest of the configuration is similar as for general
  202. [[#new_server_parts|🡑New Server Parts]].
  203. We only have to adjust the listen directives by adding the //ssl// parameter and
  204. changing the port from //80// to //443//.
  205. The official documentation of the SSL module contains an
  206. [[https://nginx.org/en/docs/http/ngx_http_ssl_module.html#example|
  207. example]] with some optimizations.
  208. We can extend an existing UCI server section similarly, e.g., for the above
  209. ''config server '${EXAMPLE_COM//./_}' '' we invoke:
  210. <code bash>
  211. # Instead of 'del_list' the listen* entries, we could use '443 ssl' beforehand.
  212. uci del_list nginx.${EXAMPLE_COM//./_}.listen='80' &&
  213. uci del_list nginx.${EXAMPLE_COM//./_}.listen='[::]:80' &&
  214. uci add_list nginx.${EXAMPLE_COM//./_}.listen='443 ssl' &&
  215. uci add_list nginx.${EXAMPLE_COM//./_}.listen='[::]:443 ssl' &&
  216. uci set nginx.${EXAMPLE_COM//./_}.ssl_certificate=\
  217. '${CONF_DIR}${EXAMPLE_COM}.crt' &&
  218. uci set nginx.${EXAMPLE_COM//./_}.ssl_certificate_key=\
  219. '${CONF_DIR}${EXAMPLE_COM}.key' &&
  220. uci set nginx.${EXAMPLE_COM//./_}.ssl_session_cache=\
  221. '${SSL_SESSION_CACHE_ARG}' &&
  222. uci set nginx.${EXAMPLE_COM//./_}.ssl_session_timeout=\
  223. '${SSL_SESSION_TIMEOUT_ARG}' &&
  224. uci commit nginx
  225. </code>
  226. For making the server in ''${CONF_DIR}${EXAMPLE_COM}.conf'' available
  227. via SSL, we can make similar changes there.
  228. The following command creates a **self-signed** SSL certificate and changes the
  229. corresponding configuration:
  230. <code bash>$(basename "${NGINX_UTIL}") ${ADD_SSL_FCT} ${EXAMPLE_COM}</code>
  231. - If a ''$(basename "${CONF_DIR}")/${EXAMPLE_COM}.conf'' file exists, it\
  232. adds //ssl_*// directives and changes the //listen// directives there.\
  233. Else it does that similarly to the example above for a ++selected UCI\
  234. server.| Hereby it searches the UCI config first for a server with the\
  235. given name and then for a server whose //server_name// contains the name.\
  236. For //${EXAMPLE_COM}// it is the latter as a UCI key cannot have dots.++
  237. - It checks if there is a certificate with key for '${EXAMPLE_COM}' that is\
  238. valid for at least 13 months or tries to create a self-signed one.
  239. - When cron is activated, it installs a cron job for renewing the self-signed\
  240. certificate every year if needed, too. We can activate cron by: \
  241. <code bash>service cron enable && service cron start</code>
  242. This can be undone by invoking:
  243. <code bash>$(basename "${NGINX_UTIL}") del_ssl ${EXAMPLE_COM}</code>
  244. For using an SSL certificate and key that are managed otherwise, there is:
  245. <code bash>$(basename "${NGINX_UTIL}") add_ssl ${EXAMPLE_COM} "\$MANAGER" \
  246. "/absolute/path/to/crt" "/absolute/path/to/key"</code>
  247. It only adds //ssl_*// directives and changes the //listen// directives in
  248. the appropriate configuration, but does not create or change the certificate
  249. or its key. This can be reverted by:
  250. <code bash>$(basename "${NGINX_UTIL}") del_ssl ${EXAMPLE_COM} "\$MANAGER"</code>
  251. For example [[https://github.com/ndilieto/uacme|uacme]] or
  252. [[https://github.com/Neilpang/acme.sh|acme.sh]] can be used for creating an SSL
  253. certificate signed by Let’s Encrypt and changing the config
  254. ++accordingly.|
  255. They call ''$(basename "${NGINX_UTIL}") add_ssl \$FQDN acme \$CRT \$KEY''
  256. internally.++
  257. We can install them by:
  258. <code bash>
  259. opkg update && opkg install uacme #or: acme #and for LuCI: luci-app-acme
  260. </code>
  261. [[#openwrt_s_defaults|🡓OpenWrt’s Defaults]] include a UCI server for the LAN:
  262. ''config server '${LAN_NAME}' ''.
  263. It has //ssl_*// directives prepared for a self-signed((Let’s Encrypt (and other
  264. CAs) cannot sign certificates of a **local** server.))
  265. SSL certificate, which is created on the first start of Nginx.
  266. The server listens on all addresses, is the //default_server// for HTTPS and
  267. allows connections from LAN only (by including the file ''restrict_locally''
  268. with //allow/deny// directives, cf. the official documentation on
  269. [[https://nginx.org/en/docs/http/ngx_http_access_module.html|limiting access]]).
  270. For making another domain name accessible to all addresses, the corresponding
  271. SSL server part should listen on port //443// and contain the FQDN as
  272. //server_name//, cf. the official documentation on
  273. [[https://nginx.org/en/docs/http/request_processing.html|request_processing]].
  274. Furthermore, there is also a UCI server named ''_redirect2ssl'', which listens
  275. on all addresses, acts as //default_server// for HTTP and redirects requests for
  276. inexistent URLs to HTTPS.
  277. ==== OpenWrt’s Defaults ====${MSG}
  278. Since Nginx is compiled with these presets, we can pretend that the main
  279. configuration will always contain the following directives
  280. (though we can overwrite them):
  281. <code nginx>$(ifConfEcho --pid-path pid)\
  282. $(ifConfEcho --lock-path lock_file)\
  283. $(ifConfEcho --error-log-path error_log)\
  284. $(false && ifConfEcho --http-log-path access_log)\
  285. $(ifConfEcho --http-proxy-temp-path proxy_temp_path)\
  286. $(ifConfEcho --http-client-body-temp-path client_body_temp_path)\
  287. $(ifConfEcho --http-fastcgi-temp-path fastcgi_temp_path)\
  288. </code>
  289. When starting or reloading the Nginx service, the ''/etc/init.d/nginx'' script
  290. sets also the following directives
  291. (so we cannot change them in the used configuration file):
  292. <code nginx>
  293. daemon off; # procd expects services to run in the foreground
  294. </code>
  295. Then, the init sript creates the main configuration
  296. ''$(basename "${UCI_CONF}")'' dynamically from the template:
  297. $(code "${UCI_CONF}.template")
  298. So, the access log is turned off by default and we can look at the error log
  299. by ''logread'', as init.d script forwards stderr and stdout to the
  300. [[docs:guide-user:base-system:log.essentials|🡒runtime log]].
  301. We can set the //error_log// and //access_log// to files, where the log
  302. messages are forwarded to instead (after the configuration is read).
  303. And for redirecting the access log of a //server// or //location// to the logd,
  304. too, we insert the following directive in the corresponding block:
  305. <code nginx> access_log /proc/self/fd/1 openwrt;</code>
  306. If we setup a server through UCI, we can use the options //error_log// and/or
  307. //access_log// also with the special path
  308. ++'logd'.|
  309. When initializing the Nginx service, this special path is replaced by //stderr//
  310. respective ///proc/self/fd/1// (which are forwarded to the runtime log).
  311. ++
  312. For creating the configuration from the template shown above, Nginx’s init
  313. script replaces the comment ''#UCI_HTTP_CONFIG'' by all UCI servers.
  314. For each server section in the the UCI configuration, it basically copies all
  315. options into a Nginx //server {}// part, in detail:
  316. * Options starting with ''uci_'' are skipped. Currently there is only\
  317. the ''option ${MANAGE_SSL}='' in ++usage.| It is set to\
  318. //'self-signed'// when invoking\
  319. ''$(basename ${NGINX_UTIL}) ${ADD_SSL_FCT} \$NAME''.\
  320. Then the corresponding certificate is re-newed if it is about to expire.\
  321. All those certificates are checked on the initialization of the Nginx service\
  322. and if Cron is available, it is deployed for checking them annually, too.++
  323. * All other lists or options of the form ''key='value' '' are written\
  324. one-to-one as ''key value;'' directives to the configuration file.\
  325. Just the path //logd// has a special meaning for the logging directives\
  326. (described in the previous paragraph).
  327. The init.d script of Nginx uses the //$(basename ${NGINX_UTIL})// for creating
  328. the configuration file
  329. ++in RAM.|
  330. The main configuration ''${UCI_CONF}'' is a symbolic link to this place
  331. (it is a dead link if the Nginx service is not running).
  332. ++
  333. We could use a custom configuration created at ''${NGINX_CONF}'' instead of the
  334. dynamic configuration, too.((
  335. For using a custom configuration at ''${NGINX_CONF}'', we execute
  336. <code bash>uci set nginx.global.uci_enable='false' </code>
  337. Then the rest of the UCI config is ignored and //init.d// will not create the
  338. main configuration dynamically from the template anymore.
  339. Invoking ''$(basename ${NGINX_UTIL}) [${ADD_SSL_FCT}|del_ssl] \$FQDN''
  340. will still try to change a server in ''$(basename "${CONF_DIR}")/\$FQDN.conf''
  341. (this is less reliable than for a UCI config as it uses regular expressions, not
  342. a complete parser for the Nginx configuration).))
  343. This is not encouraged since you cannot setup servers using UCI anymore.
  344. Rather, we can put custom configuration parts to ''.conf'' files in the
  345. ''${CONF_DIR}'' directory.
  346. The main configuration pulls in all ''$(basename "${CONF_DIR}")/*.conf'' files
  347. into the //http {}// block behind the created UCI servers.
  348. The initial UCI config is enabled and contains two server section:
  349. $(code "/etc/config/nginx" "nginx.config")
  350. While the LAN server is the //default_server// for HTTPS, the server
  351. redirecting requests for an inexistent ''server_name'' from HTTP to HTTPS acts
  352. as //default_server// if there is ++no other|;
  353. it uses an invalid name for that, more in the official documentation on
  354. [[https://nginx.org/en/docs/http/request_processing.html|request_processing]]
  355. ++.
  356. The LAN server pulls in all ''.locations'' files from the directory
  357. ''${CONF_DIR}''.
  358. We can install the location parts of different sites there (see
  359. [[#basic|🡑Basic Configuration]]) and re-include them into other servers.
  360. This is needed especially for making them available to the WAN
  361. ([[#new_server_parts|🡑New Server Parts]]).
  362. The LAN server listens for all addresses on port //443// and restricts the
  363. access to local addresses by including:
  364. $(code "$(dirname "${CONF_DIR}")/restrict_locally")
  365. When starting or reloading the Nginx service, the init.d looks which UCI servers
  366. have set ''option ${MANAGE_SSL} 'self-signed' '', e.g. the LAN server.
  367. For all those servers it checks if there is a certificate that is still valid
  368. for 13 months or (re-)creates a self-signed one.
  369. If there is any such server, it installs also a cron job that checks the
  370. corresponding certificates once a year.
  371. The option ''${MANAGE_SSL}'' is set to //'self-signed'// respectively removed
  372. from a UCI server named ''${EXAMPLE_COM//./_}'' by the following
  373. (see [[#ssl_server_parts|🡑SSL Server Parts]], too):
  374. <code bash>
  375. $(basename ${NGINX_UTIL}) ${ADD_SSL_FCT} ${EXAMPLE_COM//./_} \
  376. # respectively: \
  377. $(basename ${NGINX_UTIL}) del_ssl ${EXAMPLE_COM//./_}
  378. </code>
  379. EOF