├── .gitignore ├── LICENSE ├── README.md ├── amazon └── no-nap │ ├── Dockerfile │ ├── entrypoint.sh │ └── nginx-plus-api.conf ├── centos ├── examples │ ├── agent-layer │ │ └── Dockerfile │ └── nginx-plus │ │ ├── Dockerfile │ │ └── entrypoint.sh ├── nap │ ├── Dockerfile │ ├── custom_log_format.json │ ├── entrypoint.sh │ └── nginx-plus-api.conf └── no-nap │ ├── Dockerfile │ ├── entrypoint.sh │ └── nginx-plus-api.conf ├── debian ├── examples │ ├── agent-layer │ │ └── Dockerfile │ └── nginx-plus │ │ ├── Dockerfile │ │ └── entrypoint.sh ├── nap │ ├── Dockerfile │ ├── custom_log_format.json │ ├── entrypoint.sh │ └── nginx-plus-api.conf └── no-nap │ ├── Dockerfile │ ├── entrypoint.sh │ └── nginx-plus-api.conf ├── ubuntu ├── examples │ ├── agent-layer │ │ └── Dockerfile │ └── nginx-plus │ │ ├── Dockerfile │ │ └── entrypoint.sh ├── nap │ ├── Dockerfile │ ├── custom_log_format.json │ ├── entrypoint.sh │ └── nginx-plus-api.conf └── no-nap │ ├── Dockerfile │ ├── entrypoint.sh │ └── nginx-plus-api.conf └── unprivileged └── examples ├── ubuntu-nap ├── Dockerfile ├── custom_log_format.json ├── entrypoint.sh └── nginx-plus-api.conf └── ubuntu-no-nap ├── Dockerfile ├── entrypoint.sh └── nginx-plus-api.conf /.gitignore: -------------------------------------------------------------------------------- 1 | *.crt 2 | *.key 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | **Table of Contents** *generated with [DocToc](https://github.com/thlorenz/doctoc)* 4 | 5 | - [1. Overview](#1-overview) 6 | - [1.1. Current Scenarios](#11-current-scenarios) 7 | - [1.2 Before You Begin](#12-before-you-begin) 8 | - [2. How to Build and Run an NGINX Controller-Enabled NGINX Plus Image](#2-how-to-build-and-run-an-nginx-controller-enabled-nginx-plus-image) 9 | - [2.1. Building an NGINX Controller-Enabled Image with NGINX Plus](#21-building-an-nginx-controller-enabled-image-with-nginx-plus) 10 | - [2.2. Building a NAP-Enabled NGINX Docker Container](#22-building-a-nap-enabled-nginx-docker-container) 11 | - [2.3. Running an NGINX Controller-Enabled NGINX Docker Container](#23-running-an-nginx-controller-enabled-nginx-docker-container) 12 | - [3.0 Adding a Controller Agent Layer to an Existing Container or Image](#30-adding-a-controller-agent-layer-to-an-existing-container-or-image) 13 | - [3.1 At Run Time](#31-at-run-time) 14 | - [3.2 As an Image Layer](#32-as-an-image-layer) 15 | - [4.0 Build Time and Run Time Options](#40-build-time-and-run-time-options) 16 | - [4.1 Default Naming Behavior](#41-default-naming-behavior) 17 | - [4.2 Persisting an Instance Identity through Stops and Starts](#42-persisting-an-instance-identity-through-stops-and-starts) 18 | - [4.3 Applying a Unique Location to a Container at Run Time](#43-applying-a-unique-location-to-a-container-at-run-time) 19 | - [4.4 Overriding Agent NGINX Controller configuration](#44-overriding-agent-nginx-controller-configuration) 20 | - [4.5 Supported run-time variables](#45-supported-run-time-variables) 21 | - [4.6 Overriding NGINX Plus version](#46-overriding-nginx-plus-version) 22 | - [5.0 Build Unprivileged Docker Image](#50-build-unprivileged-docker-image) 23 | - [5.1 Required changes in Dockerfile](#51-required-changes-in-dockerfile) 24 | - [5.2 Required changes in entrypoint.sh](#52-required-changes-in-entrypointsh) 25 | - [5.3 New build arguments](#53-new-build-arguments) 26 | - [6.0 Support](#60-support) 27 | 28 | 29 | 30 | We are actively working on improving support for containers with NGINX Controller. 31 | 32 | The following is a set of guidelines that you can use today as we enhance the experience. 33 | 34 | ## 1. Overview 35 | 36 | [NGINX Controller](https://www.nginx.com/products/nginx-controller/) is a centralized monitoring and management control-plane solution for the NGINX Plus data plane. NGINX Controller is developed and maintained by NGINX -- the people behind NGINX software. 37 | 38 | With NGINX Controller, it is possible to collect and aggregate metrics across NGINX Plus instances, your applications, environments, and locations however they run -- presenting a coherent set of visualizations of the critical NGINX Plus performance data, such as active connections or requests per second. It is also easy to quickly check for any performance degradations and traffic anomalies and to get a more in-depth insight into the NGINX configuration in general. 39 | 40 | A small agent (NGINX Controller Agent) is necessary inside the container alongside NGINX Plus to use NGINX Controller to monitor and/or manage your fleet of NGINX Plus instances. 41 | 42 | For security protection of your web application, a web application firewall NGINX App Protect could be installed alongside NGINX Plus. 43 | The official documentation for NGINX App Protect is available [here](https://docs.nginx.com/nginx-app-protect/). 44 | 45 | The official documentation for NGINX Controller is available [here](https://docs.nginx.com/nginx-controller/). 46 | 47 | Guidance around NGINX Plus is available [here](https://www.nginx.com/blog/deploying-nginx-nginx-plus-docker/). 48 | 49 | The Dockerfiles in this repository are supported by and tested against NGINX Controller v3.10 and later. 50 | 51 | ### 1.1. Current Scenarios 52 | 53 | The following list summarizes known container scenarios with NGINX Controller: 54 | 55 | - The NGINX [Controller Agent](https://docs.nginx.com/nginx-controller/admin-guides/install/install-nginx-controller-agent/) manages/monitors NGINX Plus from **inside the container**. It is not possible to run the Controller Agent in a separate container and monitor the neighboring containers running NGINX Plus, nor is it possible to install the Controller Agent on the container host and monitor/manage NGINX Plus running in containers. 56 | 57 | ### 1.2 Before You Begin 58 | 59 | Before proceeding, first complete the following required steps: 60 | 61 | 1. [Install NGINX Controller](https://docs.nginx.com/nginx-controller/admin-guides/install/). 62 | 2. [Download your NGINX Plus certificate and key](https://docs.nginx.com/nginx-controller/admin-guides/install/get-n-plus-cert-and-key/) (that is, `nginx-repo.crt` and `nginx-repo.key`). 63 | 3. [Obtain the API key for your NGINX Controller instance](https://docs.nginx.com/nginx-controller/admin-guides/install/install-nginx-controller-agent/). 64 | 65 | ## 2. How to Build and Run an NGINX Controller-Enabled NGINX Plus Image 66 | 67 | ### 2.1. Building an NGINX Controller-Enabled Image with NGINX Plus 68 | 69 | > **Note**: If you are new to Docker or the Dockerfile-based image building process, refer to the [Install Docker Engine](https://docs.docker.com/engine/install/) guide for installation instructions. See also the [Get started guide](https://docs.docker.com/get-started/) for instructions on obtaining and building images. 70 | 71 | Here's how to build the container image with the Controller Agent inside, based on the official NGINX image: 72 | 73 | 1. Clone this repository: 74 | 75 | ```bash 76 | git clone https://github.com/nginxinc/docker-nginx-controller.git 77 | ``` 78 | ```bash 79 | cd docker-nginx-controller//no-nap 80 | ``` 81 | 82 | 2. Copy your NGINX Plus repository certificate and key to the folder of the Dockerfile you will be using for your Linux distribution. 83 | 84 | 3. Edit the Dockerfile with your API_KEY and CONTROLLER_URL. 85 | 86 | > Note: Use the `CONTROLLER_URL` format that's supported by your version of NGINX Controller: 87 | > 88 | > NGINX Controller v3.10 or older: `CONTROLLER_URL=https://:8443/1.4/install/controller` 89 | > 90 | > NGINX Controller v3.11 or newer: `CONTROLLER_URL=https:///install/controller-agent` 91 | 92 | For example: 93 | 94 | ```bash 95 | sudo DOCKER_BUILDKIT=1 docker build --build-arg CONTROLLER_URL=https:///install/controller-agent --build-arg API_KEY='abcdefxxxxxx' --secret id=nginx-crt,src=nginx-repo.crt --secret id=nginx-key,src=nginx-repo.key -t nginx-agent . 96 | ``` 97 | 98 | The `DOCKER_BUILDKIT=1` enables `docker build` to recognize the `--secret` flag which allows the user to pass secret information to be used in the Dockerfile for building docker images in a safe way that will not end up stored in the final image. This is a recommended practice for the handling of the certificate and private key for NGINX repository access (`nginx-repo.crt` and `nginx-repo.key` files). More information [here](https://docs.docker.com/develop/develop-images/build_enhancements/#new-docker-build-secret-information). 99 | 100 | > **Note**: If you are using an older version of Docker (less than version 20), you may need to insert the following line at the top of your Dockerfile before building your image: `syntax=docker/dockerfile:experimental` 101 | 102 | 4. After the image is built, view the list of Docker images: 103 | 104 | ```bash 105 | sudo docker images 106 | ``` 107 | 108 | The output looks similar to the following example: 109 | 110 | ```bash 111 | REPOSITORY TAG IMAGE ID CREATED SIZE 112 | nginx-agent latest d039b39d2987 3 minutes ago 241.6 MB 113 | ``` 114 | 115 | ### 2.2. Building a NAP-Enabled NGINX Docker Container 116 | 117 | If you want your Docker image to include a web application firewall, in addition to the Controller Agent, 118 | use a Dockerfile with NGINX App Protect included. The file is located at 119 | `docker-nginx-controller//nap`: 120 | 121 | ```bash 122 | cd docker-nginx-controller//nap 123 | ``` 124 | 125 | ### 2.3. Running an NGINX Controller-Enabled NGINX Docker Container 126 | 127 | Take the following steps to run an NGINX Controller-enabled NGINX Docker Container: 128 | 129 | 1. To start a container from the new image, run the following command: 130 | 131 | ```bash 132 | docker run --name mynginx1 -d nginx-agent 133 | ``` 134 | 135 | 2. After the container has started, check its status with `docker ps`: 136 | 137 | ```bash 138 | docker ps 139 | ``` 140 | 141 | The output looks similar to the following example: 142 | 143 | ```bash 144 | CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 145 | 7d7b47ba4c72 nginx-agent "/entrypoint.sh" 3 seconds ago Up 2 seconds 80/tcp, 443/tcp mynginx1 146 | ``` 147 | 148 | 3. You can also check the `docker logs` for the container status: 149 | 150 | ```bash 151 | docker logs 7d7b47ba4c72 152 | ``` 153 | 154 | The output looks similar to the following example: 155 | 156 | ```bash 157 | starting nginx ... 158 | updating /etc/controller-agent/agent.conf ... 159 | ---> using api_key = 1234567890 160 | starting controller-agent ... 161 | ``` 162 | 163 | 4. Check which processes have started: 164 | 165 | ```bash 166 | docker exec 7d7b47ba4c72 ps axu 167 | ``` 168 | 169 | If you see the **controller-agent** process, the setup went smoothly, and you should see the new container in the NGINX Controller interface after approximately one minute. 170 | 171 | The output looks similar to the following example: 172 | 173 | ```bash 174 | USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND 175 | root 1 0.0 0.1 4328 676 ? Ss 19:33 0:00 /bin/sh /entrypoint.sh 176 | root 5 0.0 0.5 31596 2832 ? S 19:33 0:00 nginx: master process nginx -g daemon off; 177 | nginx 11 0.0 0.3 31988 1968 ? S 19:33 0:00 nginx: worker process 178 | nginx 65 0.6 9.1 111584 45884 ? S 19:33 0:06 controller-agent 179 | ``` 180 | 181 | If your container includes NAP (NGINX App Protect) then you should also see NAP-specific processes: 182 | 183 | ```bash 184 | nginx 10 0.0 2.5 129684 52320 ? S 11:14 0:05 /usr/bin/perl /opt/app_protect/bin/bd_agent 185 | nginx 14 2.4 12.7 1057612 260260 ? Sl 11:14 5:54 /usr/share/ts/bin/bd-socket-plugin tmm_count 4 proc_cpuinfo_cpu_m 186 | ``` 187 | 188 | 5. To view the Controller Agent log, run the following command: 189 | 190 | ```bash 191 | docker exec 7d7b47ba4c72 tail /var/log/nginx-controller/agent.log 192 | ``` 193 | 194 | The output looks similar to the following example: 195 | 196 | ```bash 197 | 2016-08-05 19:49:39,001 [65] supervisor agent started, version=0.37-1 pid=65 uuid=<..> 198 | 2016-08-05 19:49:39,047 [65] nginx_config running nginx -t -c /etc/nginx/nginx.conf 199 | 2016-08-05 19:49:40,047 [65] supervisor post https://:8443/<..>/ffeedd0102030405060708/agent/ 200 85 4 0.096 200 | 2016-08-05 19:50:24,674 [65] bridge_manager post https://:8443/<..>/ffeedd0102030405060708/update/ 202 2370 0 0.084 201 | ``` 202 | 203 | When you're done with the container, run the following command to stop it: 204 | 205 | ```bash 206 | docker stop 7d7b47ba4c72 207 | ``` 208 | 209 | To check the status of all the containers that are running and stopped, run the following command: 210 | 211 | ```bash 212 | docker ps -a 213 | ``` 214 | 215 | The output looks similar to the following example: 216 | 217 | ```bash 218 | CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 219 | 7d7b47ba4c72 nginx-agent "/entrypoint.sh" 22 minutes ago Exited (137) 19 seconds ago mynginx1 220 | ``` 221 | 222 | ## 3.0 Adding a Controller Agent Layer to an Existing Container or Image 223 | 224 | An alternate way to handle the Controller Agent within containers is to include the necessary Controller Agent commands in the run command for an existing image. This way, you don't have to build the Controller Agent into your container before running, and you might find it handy for a Proof of Concept. 225 | 226 | ### 3.1 At Run Time 227 | 228 | To add a Controller Agent layer to an existing container or image at run time, take the following steps: 229 | 230 | ```bash 231 | # nginx-plus is an example base image located in debian/examples/nginx-plus 232 | FROM nginx-plus 233 | 234 | # Start a container with environment variables for CONTROLLER_URL and API_KEY 235 | # docker run --name api-gw --hostname api-gw -e CONTROLLER_URL=https:///install/controller-agent -e API_KEY=deadbeef -d -P nginx-ctrl 236 | 237 | # Install everything needed to install the Controller Agent so that the container can start quickly 238 | RUN apt-get update &&\ 239 | apt install -y \ 240 | curl procps sudo 241 | 242 | EXPOSE 80 443 8080 243 | STOPSIGNAL SIGTERM 244 | 245 | WORKDIR /controller 246 | 247 | RUN printf "curl -skSL \$CONTROLLER_URL | bash -s - -y\n exec nginx -g 'daemon off;'" > start 248 | 249 | CMD ["sh", "/controller/start"] 250 | ``` 251 | 252 | It takes 1-2 minutes to start the container. After `docker run `, use `docker logs --follow CONTAINER` to watch the installation and startup progress. 253 | 254 | ### 3.2 As an Image Layer 255 | 256 | For your convenience, Dockerfiles that define a Controller Agent image layer are provided for you. These Dockerfiles are built following the pattern for [running multiple services in a Docker image](https://docs.docker.com/config/containers/multi-service_container) and can be found under each distribution in the following location: 257 | 258 | ```bash 259 | cd docker-nginx-controller//examples/agent-layer 260 | ``` 261 | 262 | The build process is the same as above, referencing your custom image as the source. 263 | 264 | ## 4.0 Build Time and Run Time Options 265 | 266 | ### 4.1 Default Naming Behavior 267 | 268 | By default, the Controller Agent determines the OS hostname during installation using the `hostname -f` command. The hostname value is then assigned to the `instance_name` key in the Controller Agent configuration file (`agent.conf`) and is used to generate a UUID. The UUID and instance name together provide a means of uniquely identifying the NGINX instance in NGINX Controller. When the Agent is run inside a container, the default hostname is a shortened Docker Container ID on the host. You can override the automatically assigned `instance_name` at run time by setting the `ENV_CONTROLLER_INSTANCE_NAME` environment variable to the desired value. 269 | 270 | Providing the `ENV_CONTROLLER_INSTANCE_NAME` variable for the container sets the container's name that is displayed in NGINX Controller for the `displayName` of the instance. This also sets the instance object name, which is used in configuration references. 271 | 272 | If you do not override the default instance name, the `containerID` is registered as the instance name and `displayName` in NGINX Controller. 273 | 274 | ### 4.2 Persisting an Instance Identity through Stops and Starts 275 | 276 | Using the optional build-time setting of `STORE_UUID=True` ensures that the dynamically generated UUID persists in the Controller Agent configuration. This, together with the `instance_name`, allows the container instance to be stopped and started or persist if the container host is rebooted. 277 | 278 | Each new container started from an NGINX Controller-enabled Docker image is reported as a unique system in the NGINX Controller console. This is the recommended configuration. NGINX Controller aggregates metrics across your instances based on the application, application component, location, environment, and so forth. 279 | 280 | `VAR STORE_UUID=True` can be set during the image build process and applies to all containers derived from the image. 281 | 282 | ```bash 283 | sudo DOCKER_BUILDKIT=1 docker build --secret id=nginx-crt,src=nginx-repo.crt --secret id=nginx-key,src=nginx-repo.key --build-arg CONTROLLER_URL=https:///install/controller-agent --build-arg API_KEY='abcdefxxxxxx' --build-arg STORE_UUID=True -t nginx-agent . 284 | ``` 285 | 286 | ### 4.3 Applying a Unique Location to a Container at Run Time 287 | 288 | By default, new instances are placed in the NGINX Controller location named `unspecified`. There are situations where instances should be associated with specific locations. This can be defined at build time to apply to all containers derived from the image or during run time and apply to a subset of containers. 289 | 290 | Using the optional run time setting of `ENV_CONTROLLER_LOCATION`, when your container instance reports to NGINX Controller, the new instance will automatically register itself with a specific location already present in NGINX Controller. 291 | 292 | ```bash 293 | docker run --name mynginx-east-1 -e ENV_CONTROLLER_LOCATION=east -d nginx-agent 294 | ``` 295 | 296 | The location will not be automatically created in NGINX Controller and needs to be defined separately. 297 | 298 | ### 4.4 Overriding Agent NGINX Controller configuration 299 | 300 | Container could be build using configuration of one instance of NGINX Controller and run using another instance of NGINX Controller. 301 | `ENV_CONTROLLER_API_KEY` and `ENV_CONTROLLER_API_URL` variable could be used to override during run-time NGINX Controller configuration. 302 | 303 | ```bash 304 | docker run --name mynginx1 -d -e ENV_CONTROLLER_API_URL=:443 -e ENV_CONTROLLER_API_KEY=deadbeef nginx-agent 305 | ``` 306 | 307 | ### 4.5 Supported run-time variables 308 | 309 | You can use the following run-time variables to override the default NGINX configuration values: 310 | 311 | | Variable | Description | 312 | |-|-| 313 | | `ENV_CONTROLLER_API_URL` | The NGINX Controller API URL. The new instance will use the specified API URL to locate the NGINX Controller host. | 314 | | `ENV_CONTROLLER_API_KEY` | The NGINX Controller API key. The new instance will use the specified API key to authenticate and communicate with NGINX Controller. | 315 | | `ENV_CONTROLLER_INSTANCE_NAME` | The name to use for the instance. The new instance will use the specified name when registering itself with NGINX Controller. | 316 | | `ENV_CONTROLLER_LOCATION` | The location for the instance. The new instance will use the specified location when registering itself with NGINX Controller.
**Note**: The specified location must already exist in NGINX Controller. | 317 | | `ENV_CONTROLLER_INSTANCE_GROUP` | The instance group the instance belongs to. The new instance will join the specified instance group when registering itself with NGINX Controller.
**Note**: The specified instance group will be created if it doesn't already exist. | 318 | 319 | ### 4.6 Overriding NGINX Plus version 320 | 321 | Version of NGINX Plus installed inside docker image could changed using `NGINX_PLUS_VERSION` build time argument. 322 | `NGINX_PLUS_VERSION` should be set to release number of NGINX Plus e.g. `25` 323 | 324 | ```bash 325 | DOCKER_BUILDKIT=1 docker build --secret id=nginx-crt,src=nginx-repo.crt --secret id=nginx-key,src=nginx-repo.key --build-arg CONTROLLER_URL=https:///install/controller-agent --build-arg API_KEY='abcdefxxxxxx' --build-arg NGINX_PLUS_VERSION=22 -t nginx-agent . 326 | ``` 327 | 328 | For NAP-Enabled NGINX Docker Containers, information about App Security requirements can be found [here](https://docs.nginx.com/nginx-controller/admin-guides/install/try-nginx-controller-app-sec/) 329 | 330 | ### 4.7 Configuring and updating agent 331 | 332 | When manually editing the `agent.conf` file or upgrading the NGINX Agent service running inside the container, use `--restart always` with `docker run`, as restarting the NGINX Agent service will trigger the container to exit. 333 | 334 | ```bash 335 | docker run --restart always --name mynginx -d nginx-agent 336 | ``` 337 | 338 | ## 5.0 Build Unprivileged Docker Image 339 | 340 | ### 5.1 Required changes in Dockerfile 341 | Add the following snippet under the `ARG NGINX_PLUS_VERSION=nn` line: 342 | ```bash 343 | ARG EXPOSE_PORT=8080 344 | ENV NGINX_EXPOSE_PORT=$EXPOSE_PORT 345 | 346 | ARG NON_ROOT_USER=nginx 347 | ENV CONTROLLER_USER=$NON_ROOT_USER 348 | 349 | ARG NON_ROOT_GROUP=nginx 350 | ENV CONTROLLER_GROUP=$NON_ROOT_GROUP 351 | 352 | ARG NAP_SYSLOG_PORT=5114 353 | ENV LISTENERS_NAP_SYSLOG_PORT=$NAP_SYSLOG_PORT 354 | ``` 355 | Replace the `EXPOSE 80` line with the following snippet: 356 | ```bash 357 | # Update ownership for the necessary filesystem objects for running under non-root user 358 | RUN chown -R $CONTROLLER_USER:$CONTROLLER_GROUP /var/run/ \ 359 | && chown -R $CONTROLLER_USER:$CONTROLLER_GROUP /var/log/nginx/ \ 360 | && chown -R $CONTROLLER_USER:$CONTROLLER_GROUP /var/cache/nginx/ \ 361 | && chown -R $CONTROLLER_USER:$CONTROLLER_GROUP /etc/controller-agent/ \ 362 | && chown -R $CONTROLLER_USER:$CONTROLLER_GROUP /etc/nginx/ \ 363 | && chown -R $CONTROLLER_USER:$CONTROLLER_GROUP /var/log/app_protect/ || true \ 364 | && chown -R $CONTROLLER_USER:$CONTROLLER_GROUP /opt/app_protect/ || true \ 365 | && sed -i "s,listen 80 default_server;,listen $NGINX_EXPOSE_PORT default_server;," /etc/nginx/conf.d/default.conf \ 366 | && sed -i '/user nginx;/d' /etc/nginx/nginx.conf 367 | 368 | USER $CONTROLLER_USER 369 | 370 | EXPOSE $NGINX_EXPOSE_PORT 371 | ``` 372 | Examples of unprivileged docker files can be found in `unprivileged/examples` directory. 373 | ### 5.2 Required changes in entrypoint.sh 374 | Only applicable to images running NGINX App Protect - remove prefixes `/bin/su -s` under `# Launch NAP` (two occurrences). 375 | 376 | The result should look similar to the following snippet: 377 | ```bash 378 | # Launch NAP 379 | /bin/bash -c '/opt/app_protect/bin/bd_agent &' nginx 380 | bd_agent_pid=$(pgrep bd_agent) 381 | /bin/bash -c "/usr/share/ts/bin/bd-socket-plugin tmm_count 4 proc_cpuinfo_cpu_mhz 2000000 total_xml_memory 307200000 total_umu_max_size 3129344 sys_max_account_id 1024 no_static_config 2>&1 > /var/log/app_protect/bd-socket-plugin.log &" nginx 382 | bd_socket_pid=$(pgrep bd-socket) 383 | ``` 384 | ### 5.3 New build arguments 385 | Here is the list of new build arguments introduced by the changes above: 386 | | Argument | Description | 387 | |-|-| 388 | | `EXPOSE_PORT` | The port number to expose and listen on for NGINX. (Default `8080`) | 389 | | `NON_ROOT_USER` | The name of unprivileged user. (Default `nginx`) | 390 | | `NON_ROOT_GROUP` | The name of unprivileged group. (Default `nginx`) | 391 | | `NAP_SYSLOG_PORT` | The port number for syslog listener of NGINX App Protect events. (Default `5114`) | 392 | 393 | The build and run instructions are the same. Please refer to [2. How to Build and Run an NGINX Controller-Enabled NGINX Plus Image](#2-how-to-build-and-run-an-nginx-controller-enabled-nginx-plus-image). 394 | ## 6.0 Support 395 | 396 | This project is supported and has been validated with Controller Agent v3.10 and later. 397 | -------------------------------------------------------------------------------- /amazon/no-nap/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM amazonlinux:2 2 | 3 | LABEL maintainer="NGINX Controller Engineering" 4 | 5 | # e.g '1234567890' 6 | ARG API_KEY 7 | ENV ENV_CONTROLLER_API_KEY=$API_KEY 8 | 9 | # e.g https:///install/controller-agent 10 | ARG CONTROLLER_URL 11 | ENV ENV_CONTROLLER_URL=$CONTROLLER_URL 12 | 13 | # e.g True or False 14 | ARG STORE_UUID=False 15 | ENV ENV_CONTROLLER_STORE_UUID=$STORE_UUID 16 | 17 | # e.g Instance location already defined in Controller 18 | ARG LOCATION 19 | ENV ENV_CONTROLLER_LOCATION=$LOCATION 20 | 21 | # e.g Instance group already defined in Controller 22 | ARG INSTANCE_GROUP 23 | ENV ENV_CONTROLLER_INSTANCE_GROUP=$INSTANCE_GROUP 24 | 25 | # NGXIN Plus release e.g 25 26 | ARG NGINX_PLUS_VERSION=25 27 | 28 | COPY nginx-plus-api.conf /etc/nginx/conf.d/ 29 | RUN mkdir "/etc/ssl/nginx/" 30 | RUN chmod +r /etc/ssl/nginx/ 31 | 32 | # Install NGINX Plus 33 | RUN --mount=type=secret,id=nginx-crt,dst=/etc/ssl/nginx/nginx-repo.crt,mode=0644 \ 34 | --mount=type=secret,id=nginx-key,dst=/etc/ssl/nginx/nginx-repo.key,mode=0644 \ 35 | set -ex \ 36 | && yum -y update && yum -y upgrade \ 37 | && yum -y groupinstall 'Development Tools' \ 38 | && yum -y install \ 39 | ca-certificates \ 40 | curl \ 41 | initscripts \ 42 | sudo \ 43 | jq \ 44 | net-tools \ 45 | redhat-lsb \ 46 | tar \ 47 | wget \ 48 | && \ 49 | NGINX_GPGKEY=573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62; \ 50 | found=''; \ 51 | for server in \ 52 | ha.pool.sks-keyservers.net \ 53 | hkp://keyserver.ubuntu.com:80 \ 54 | hkp://p80.pool.sks-keyservers.net:80 \ 55 | pgp.mit.edu \ 56 | ; do \ 57 | echo "Fetching GPG key $NGINX_GPGKEY from $server"; \ 58 | gpg --keyserver "$server" --recv-keys "0x$NGINX_GPGKEY" && found=yes && break; \ 59 | done; \ 60 | test -z "$found" && echo >&2 "error: failed to fetch GPG key $NGINX_GPGKEY" && exit 1; \ 61 | wget -P /etc/yum.repos.d https://cs.nginx.com/static/files/nginx-plus-amazon2.repo \ 62 | # NGINX Javascript module needed for APIM 63 | && yum update && yum -y install nginx-plus-${NGINX_PLUS_VERSION}* nginx-plus-module-njs-${NGINX_PLUS_VERSION}* \ 64 | # Install Controller Agent 65 | && curl -k -sS -L ${CONTROLLER_URL} > install.sh \ 66 | && sed -i 's/^assume_yes=""/assume_yes="-y"/' install.sh \ 67 | && sed -i 's,-n "${NGINX_GPGKEY}",true,' install.sh \ 68 | && sh ./install.sh -y \ 69 | # cleanup sensitive nginx-plus data 70 | && rm /etc/yum.repos.d/nginx-plus-amazon2.repo \ 71 | && gpg --batch --delete-keys "$NGINX_GPGKEY" 72 | 73 | # Forward request logs to Docker log collector 74 | RUN ln -sf /proc/1/fd/1 /var/log/nginx-controller/agent.log \ 75 | && ln -sf /proc/1/fd/2 /var/log/nginx/error.log 76 | 77 | COPY entrypoint.sh / 78 | 79 | EXPOSE 80 80 | 81 | STOPSIGNAL SIGTERM 82 | 83 | ENTRYPOINT ["sh", "/entrypoint.sh"] 84 | -------------------------------------------------------------------------------- /amazon/no-nap/entrypoint.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 3 | # This script launches nginx and the NGINX Controller Agent. 4 | # 5 | 6 | # Variables 7 | agent_conf_file="/etc/controller-agent/agent.conf" 8 | agent_log_file="/var/log/nginx-controller/agent.log" 9 | nginx_status_conf="/etc/nginx/conf.d/stub_status.conf" 10 | api_key="" 11 | instance_name="$(hostname -f)" 12 | controller_api_url="" 13 | location="" 14 | 15 | handle_term() 16 | { 17 | echo "received TERM signal" 18 | echo "stopping controller-agent ..." 19 | kill -TERM "${agent_pid}" 2>/dev/null 20 | echo "stopping nginx ..." 21 | kill -TERM "${nginx_pid}" 2>/dev/null 22 | } 23 | 24 | trap 'handle_term' TERM 25 | 26 | # Launch nginx 27 | echo "starting nginx ..." 28 | nginx -g "daemon off;" & 29 | 30 | nginx_pid=$! 31 | 32 | wait_workers() 33 | { 34 | while ! pgrep -f 'nginx: worker process' >/dev/null 2>&1; do 35 | echo "waiting for nginx workers ..." 36 | sleep 2 37 | done 38 | } 39 | 40 | wait_workers 41 | 42 | test -n "${ENV_CONTROLLER_API_KEY}" && \ 43 | api_key=${ENV_CONTROLLER_API_KEY} 44 | 45 | # if instance_name is defined in the env vars, use it 46 | test -n "${ENV_CONTROLLER_INSTANCE_NAME}" && \ 47 | instance_name=${ENV_CONTROLLER_INSTANCE_NAME} 48 | 49 | test -n "${ENV_CONTROLLER_API_URL}" && \ 50 | controller_api_url=${ENV_CONTROLLER_API_URL} 51 | 52 | test -n "${ENV_CONTROLLER_LOCATION}" && \ 53 | location=${ENV_CONTROLLER_LOCATION} 54 | 55 | test -n "${ENV_CONTROLLER_INSTANCE_GROUP}" && \ 56 | instance_group=${ENV_CONTROLLER_INSTANCE_GROUP} 57 | 58 | if [ -n "${api_key}" -o -n "${instance_name}" -o -n "${controller_api_url}" -o -n "${location}" -o -n "${instance_group}" ]; then 59 | echo "updating ${agent_conf_file} ..." 60 | 61 | if [ ! -f "${agent_conf_file}" ]; then 62 | test -f "${agent_conf_file}.default" && \ 63 | cp -p "${agent_conf_file}.default" "${agent_conf_file}" || \ 64 | { echo "no ${agent_conf_file}.default found! exiting."; exit 1; } 65 | fi 66 | 67 | test -n "${api_key}" && \ 68 | echo " ---> using api_key = ${api_key}" && \ 69 | sh -c "sed -i.old -e 's/api_key.*$/api_key = $api_key/' \ 70 | ${agent_conf_file}" 71 | 72 | test -n "${controller_api_url}" && \ 73 | echo " ---> using controller api url = ${controller_api_url}" && \ 74 | sh -c "sed -i.old -e 's@^api_url.*@api_url = $controller_api_url@' \ 75 | ${agent_conf_file}" 76 | 77 | test -n "${instance_name}" && \ 78 | echo " ---> using instance_name = ${instance_name}" && \ 79 | sh -c "sed -i.old -e 's/instance_name.*$/instance_name = $instance_name/' \ 80 | ${agent_conf_file}" 81 | 82 | test -n "${location}" && \ 83 | echo " ---> using location = ${location}" && \ 84 | sh -c "sed -i.old -e 's/location_name.*$/location_name = $location/' \ 85 | ${agent_conf_file}" 86 | 87 | test -n "${instance_group}" && \ 88 | echo " ---> using instance group = ${instance_group}" && \ 89 | sh -c "sed -i.old -e 's/instance_group.*$/instance_group = $instance_group/' \ 90 | ${agent_conf_file}" 91 | 92 | test -f "${agent_conf_file}" && \ 93 | chmod 644 ${agent_conf_file} && \ 94 | chown nginx ${agent_conf_file} > /dev/null 2>&1 95 | 96 | test -f "${nginx_status_conf}" && \ 97 | chmod 644 ${nginx_status_conf} && \ 98 | chown nginx ${nginx_status_conf} > /dev/null 2>&1 99 | fi 100 | 101 | if ! grep '^api_key.*=[ ]*[[:alnum:]].*' ${agent_conf_file} > /dev/null 2>&1; then 102 | echo "no api_key found in ${agent_conf_file}! exiting." 103 | exit 1 104 | fi 105 | 106 | echo "starting controller-agent ..." 107 | /usr/bin/nginx-controller-agent > /dev/null 2>&1 < /dev/null & 108 | 109 | agent_pid=$! 110 | 111 | if [ $? != 0 ]; then 112 | echo "couldn't start the agent, please check ${agent_log_file}" 113 | exit 1 114 | fi 115 | 116 | wait_term() 117 | { 118 | wait ${agent_pid} 119 | trap - TERM 120 | kill -QUIT "${nginx_pid}" 2>/dev/null 121 | echo "waiting for nginx to stop..." 122 | wait ${nginx_pid} 123 | } 124 | 125 | wait_term 126 | 127 | echo "controller-agent process has stopped, exiting." 128 | -------------------------------------------------------------------------------- /amazon/no-nap/nginx-plus-api.conf: -------------------------------------------------------------------------------- 1 | # This sample NGINX Plus configuration enables the NGINX Plus API, for live 2 | # activity monitoring and the built-in dashboard, dynamic configuration of 3 | # upstream groups, and key-value stores. Keep in mind that any features 4 | # added to the API in future NGINX Plus releases will be enabled 5 | # automatically by this file. 6 | # Created in May 2018 by NGINX, Inc. for NGINX Plus R14 and later. 7 | 8 | # Documentation: 9 | # https://docs.nginx.com/nginx/admin-guide/monitoring/live-activity-monitoring/ 10 | # https://www.nginx.com/blog/live-activity-monitoring-nginx-plus-3-simple-steps 11 | 12 | # To conform with the conventional configuration scheme, place this file in 13 | # the /etc/nginx/conf.d directory and add an 'include' directive that 14 | # references it in the main configuration file, /etc/nginx/nginx.conf, 15 | # either by name or with a wildcard expression. Then validate and reload 16 | # the configuration, for example with this command: 17 | # 18 | # nginx -t && nginx -s reload 19 | 20 | # Note that additional directives are required in other parts of the 21 | # configuration: 22 | # 23 | # For metrics to be gathered for an HTTP or TCP/UDP virtual server, you must 24 | # include the 'status_zone' directive in its 'server' block. See: 25 | # http://nginx.org/r/status_zone 26 | # 27 | # Similarly, for metrics to be gathered for an upstream server group, you 28 | # must include the 'zone' directive in the 'upstream' block. See: 29 | # http://nginx.org/r/zone 30 | # 31 | # For more information and instructions, see: 32 | # https://docs.nginx.com/nginx/admin-guide/monitoring/live-activity-monitoring#status_data 33 | 34 | # We strongly recommend that you restrict access to the NGINX Plus API so 35 | # that only authorized users can view metrics and configuration, change 36 | # configuration, or both. Here are a few options: 37 | # 38 | # (1) Configure your firewall to limit access to port 8080. 39 | # 40 | # (2) Use SSL/TLS client certificates. See: 41 | # https://docs.nginx.com/nginx/admin-guide/security-controls/terminating-ssl-http/ 42 | # 43 | # (3) Enable HTTP Basic authentication (RFC 7617) by uncommenting the 44 | # 'auth_basic*' directives in the 'server' block below. You can add users 45 | # with an htpasswd generator, which is readily available, or reuse an 46 | # existing htpasswd file (from an Apache HTTP Server, for example). See: 47 | # http://nginx.org/en/docs/http/ngx_http_auth_basic_module.html 48 | # 49 | # (4) Enable access from a defined network and disable it from all others, 50 | # by uncommenting the 'allow' and 'deny' directives in the 'server' block 51 | # below and specifying the appropriate network ranges. See: 52 | # http://nginx.org/en/docs/http/ngx_http_access_module.html 53 | # 54 | # You can create further restrictions on write operations, to distinguish 55 | # between users with read permission and those who can change configuration. 56 | # Uncomment the sample 'limit_except' directive in the 'location api' 57 | # block below. In addition to the HTTP Basic authentication shown, other 58 | # authentication schemes are supported. See: 59 | # http://nginx.org/r/limit_except 60 | 61 | server { 62 | # Conventional port for the NGINX Plus API is 8080 63 | listen 8080; 64 | 65 | # Uncomment to use HTTP Basic authentication; see (3) above 66 | #auth_basic "NGINX Plus API"; 67 | #auth_basic_user_file /etc/nginx/users; 68 | 69 | # Uncomment to use permissions based on IP address; see (4) above 70 | #allow 10.0.0.0/8; 71 | #deny all; 72 | 73 | # Conventional location for accessing the NGINX Plus API 74 | location /api/ { 75 | # Enable in read-write mode 76 | api write=on; 77 | 78 | # Uncomment to further restrict write permissions; see note above 79 | #limit_except GET { 80 | #auth_basic "NGINX Plus API"; 81 | #auth_basic_user_file /etc/nginx/admins; 82 | #} 83 | } 84 | 85 | # Conventional location of the NGINX Plus dashboard 86 | location = /dashboard.html { 87 | root /usr/share/nginx/html; 88 | } 89 | 90 | # Redirect requests for "/" to "/dashboard.html" 91 | location / { 92 | return 301 /dashboard.html; 93 | } 94 | 95 | # Redirect requests for pre-R14 dashboard 96 | location /status.html { 97 | return 301 /dashboard.html; 98 | } 99 | } 100 | 101 | # vim: syntax=nginx -------------------------------------------------------------------------------- /centos/examples/agent-layer/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM nginx-plus 2 | 3 | # Install everything we will need to install the Controller Agent so that the container can start quickly 4 | RUN yum update && yum -y install sudo procps ca-certificates gnupg wget binutils net-tools 5 | 6 | EXPOSE 80 443 8080 7 | STOPSIGNAL SIGTERM 8 | 9 | WORKDIR /controller 10 | # This script will download, install and start the agent using `/usr/bin/nginx-controller-agent`. 11 | # Graceful shutdown of agent is not done in this example as this solution 12 | # strictly depends on the form of cmd/entrypoint of the base image. 13 | # Example solution for multiple service management in docker image could be found in official docker 14 | # documentation: 15 | # https://docs.docker.com/config/containers/multi-service_container 16 | # TODO: remove `sed` once launching agent using `service` has been handled in the install script 17 | RUN printf "curl -skSL \$CONTROLLER_URL | sed '/^# Unconditionally stop the agent service/,\$d' | bash -s - -y\nexec nginx -g 'daemon off;' &\n/usr/bin/nginx-controller-agent" > start 18 | CMD ["sh", "/controller/start"] 19 | -------------------------------------------------------------------------------- /centos/examples/nginx-plus/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM centos:7 2 | 3 | LABEL maintainer="NGINX Controller Engineering" 4 | 5 | # Install NGINX Plus 6 | RUN --mount=type=secret,id=nginx-crt,dst=/etc/ssl/nginx/nginx-repo.crt,mode=0644 \ 7 | --mount=type=secret,id=nginx-key,dst=/etc/ssl/nginx/nginx-repo.key,mode=0644 \ 8 | set -ex \ 9 | && yum -y update && yum -y upgrade \ 10 | && yum -y install sudo procps ca-certificates gnupg wget binutils net-tools \ 11 | && \ 12 | NGINX_GPGKEY=573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62; \ 13 | found=''; \ 14 | for server in \ 15 | ha.pool.sks-keyservers.net \ 16 | hkp://keyserver.ubuntu.com:80 \ 17 | hkp://p80.pool.sks-keyservers.net:80 \ 18 | pgp.mit.edu \ 19 | ; do \ 20 | echo "Fetching GPG key $NGINX_GPGKEY from $server"; \ 21 | gpg --keyserver "$server" --recv-keys "0x$NGINX_GPGKEY" && found=yes && break; \ 22 | done; \ 23 | test -z "$found" && echo >&2 "error: failed to fetch GPG key $NGINX_GPGKEY" && exit 1; \ 24 | wget -P /etc/yum.repos.d https://cs.nginx.com/static/files/nginx-plus-7.4.repo \ 25 | # NGINX Javascript module needed for APIM 26 | && yum update && yum -y install nginx-plus nginx-plus-module-njs 27 | 28 | # Forward request logs to Docker log collector 29 | RUN ln -sf /dev/stdout /var/log/nginx/access.log \ 30 | && ln -sf /dev/stderr /var/log/nginx/error.log 31 | 32 | EXPOSE 80 33 | STOPSIGNAL SIGTERM 34 | 35 | COPY ./entrypoint.sh /entrypoint.sh 36 | ENTRYPOINT ["/entrypoint.sh"] 37 | CMD ["nginx", "-g", "daemon off;"] 38 | -------------------------------------------------------------------------------- /centos/examples/nginx-plus/entrypoint.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | exec "$@" -------------------------------------------------------------------------------- /centos/nap/Dockerfile: -------------------------------------------------------------------------------- 1 | ARG STORE_UUID=False 2 | FROM centos:7 as nginx-installer 3 | 4 | LABEL maintainer="NGINX Controller Engineering" 5 | 6 | # NGXIN Plus release e.g 25 7 | ARG NGINX_PLUS_VERSION=25 8 | 9 | COPY nginx-plus-api.conf /etc/nginx/conf.d/ 10 | COPY entrypoint.sh / 11 | 12 | ENV NGINX_GPGKEY=573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62 13 | 14 | # Install NGINX Plus 15 | RUN --mount=type=secret,id=nginx-crt,dst=/etc/ssl/nginx/nginx-repo.crt,mode=0644 \ 16 | --mount=type=secret,id=nginx-key,dst=/etc/ssl/nginx/nginx-repo.key,mode=0644 \ 17 | set -ex \ 18 | && yum -y update && yum -y upgrade \ 19 | && yum -y install epel-release \ 20 | && yum -y install wget ca-certificates epel-release curl sudo vim procps gnupg binutils net-tools \ 21 | && \ 22 | found=''; \ 23 | for server in \ 24 | ha.pool.sks-keyservers.net \ 25 | hkp://keyserver.ubuntu.com:80 \ 26 | hkp://p80.pool.sks-keyservers.net:80 \ 27 | pgp.mit.edu \ 28 | ; do \ 29 | echo "Fetching GPG key $NGINX_GPGKEY from $server"; \ 30 | gpg --keyserver "$server" --recv-keys "0x$NGINX_GPGKEY" && found=yes && break; \ 31 | done; \ 32 | test -z "$found" && echo >&2 "error: failed to fetch GPG key $NGINX_GPGKEY" && exit 1; \ 33 | wget -P /etc/yum.repos.d https://cs.nginx.com/static/files/nginx-plus-7.4.repo \ 34 | && wget -P /etc/yum.repos.d https://cs.nginx.com/static/files/app-protect-7.repo \ 35 | # NGINX Javascript module needed for APIM 36 | && yum update && yum -y install nginx-plus-${NGINX_PLUS_VERSION}* nginx-plus-module-njs-${NGINX_PLUS_VERSION}* 37 | 38 | FROM nginx-installer as agent-installer 39 | # Install Controller Agent 40 | ARG CONTROLLER_URL 41 | ARG API_KEY 42 | ENV ENV_CONTROLLER_API_KEY=$API_KEY 43 | ARG STORE_UUID 44 | RUN --mount=type=secret,id=nginx-crt,dst=/etc/ssl/nginx/nginx-repo.crt,mode=0644 \ 45 | --mount=type=secret,id=nginx-key,dst=/etc/ssl/nginx/nginx-repo.key,mode=0644 \ 46 | curl -k -sS -L ${CONTROLLER_URL} > install.sh \ 47 | && sed -i 's/^assume_yes=""/assume_yes="-y"/' install.sh \ 48 | && sed -i 's,-n "${NGINX_GPGKEY}",true,' install.sh \ 49 | && sh ./install.sh -y 50 | 51 | FROM agent-installer as nap-installer 52 | # Install nginx-app-protect 53 | ARG NGINX_PLUS_VERSION=25 54 | RUN --mount=type=secret,id=nginx-crt,dst=/etc/ssl/nginx/nginx-repo.crt,mode=0644 \ 55 | --mount=type=secret,id=nginx-key,dst=/etc/ssl/nginx/nginx-repo.key,mode=0644 \ 56 | if [ "$NGINX_PLUS_VERSION" -lt 22 ] ; then \ 57 | echo "NGINX Plus version ${NGINX_PLUS_VERSION} is not supported by NAP" \ 58 | && exit 1 ; \ 59 | elif [ "$NGINX_PLUS_VERSION" -eq 22 ] ; \ 60 | then \ 61 | yum -y install app-protect-22+3.243.1 ; \ 62 | else \ 63 | yum -y install app-protect ; \ 64 | fi 65 | RUN sed -i "6 a load_module modules/ngx_http_app_protect_module.so;" /etc/nginx/nginx.conf 66 | 67 | 68 | FROM nap-installer as cleaner 69 | # cleanup sensitive nginx-plus data 70 | RUN gpg --list-keys 71 | RUN rm /etc/yum.repos.d/nginx-plus-7.4.repo \ 72 | && gpg --batch --delete-keys $NGINX_GPGKEY 73 | 74 | FROM cleaner as runner 75 | ## e.g '1234567890' 76 | ARG API_KEY 77 | ENV ENV_CONTROLLER_API_KEY=$API_KEY 78 | 79 | # e.g https:///install/controller-agent 80 | ARG CONTROLLER_URL 81 | ENV ENV_CONTROLLER_URL=$CONTROLLER_URL 82 | 83 | # e.g True or False 84 | ARG STORE_UUID 85 | ENV ENV_CONTROLLER_STORE_UUID=$STORE_UUID 86 | 87 | # e.g Instance location already defined in Controller 88 | ARG LOCATION 89 | ENV ENV_CONTROLLER_LOCATION=$LOCATION 90 | 91 | # e.g Instance group already defined in Controller 92 | ARG INSTANCE_GROUP 93 | ENV ENV_CONTROLLER_INSTANCE_GROUP=$INSTANCE_GROUP 94 | 95 | # Forward request logs to Docker log collector 96 | RUN ln -sf /proc/1/fd/1 /var/log/nginx-controller/agent.log \ 97 | && ln -sf /proc/1/fd/2 /var/log/nginx/error.log 98 | 99 | COPY custom_log_format.json /etc/nginx/ 100 | 101 | EXPOSE 80 102 | 103 | STOPSIGNAL SIGTERM 104 | 105 | ENTRYPOINT ["sh", "/entrypoint.sh"] 106 | -------------------------------------------------------------------------------- /centos/nap/custom_log_format.json: -------------------------------------------------------------------------------- 1 | { 2 | "filter": { 3 | "request_type": "all" 4 | }, 5 | "content": { 6 | "format": "splunk", 7 | "max_request_size": "any", 8 | "max_message_size": "10k" 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /centos/nap/entrypoint.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 3 | # This script launches nginx and the NGINX Controller Agent. 4 | # 5 | 6 | # Variables 7 | agent_conf_file="/etc/controller-agent/agent.conf" 8 | agent_log_file="/var/log/nginx-controller/agent.log" 9 | nginx_status_conf="/etc/nginx/conf.d/stub_status.conf" 10 | api_key="" 11 | instance_name="$(hostname -f)" 12 | controller_api_url="" 13 | location="" 14 | 15 | handle_term() 16 | { 17 | echo "received TERM signal" 18 | echo "stopping bd-socket-plugin" 19 | kill -TERM "${bd_socket_pid}" 2>/dev/null 20 | echo "stopping bd-agent" 21 | kill -TERM "${bd_agent_pid}" 2>/dev/null 22 | echo "stopping controller-agent ..." 23 | kill -TERM "${agent_pid}" 2>/dev/null 24 | echo "stopping nginx ..." 25 | kill -TERM "${nginx_pid}" 2>/dev/null 26 | } 27 | 28 | trap 'handle_term' TERM 29 | 30 | # Launch NAP 31 | /bin/su -s /bin/bash -c '/opt/app_protect/bin/bd_agent &' nginx 32 | bd_agent_pid=$(pgrep bd_agent) 33 | /bin/su -s /bin/bash -c "/usr/share/ts/bin/bd-socket-plugin tmm_count 4 proc_cpuinfo_cpu_mhz 2000000 total_xml_memory 307200000 total_umu_max_size 3129344 sys_max_account_id 1024 no_static_config 2>&1 > /var/log/app_protect/bd-socket-plugin.log &" nginx 34 | bd_socket_pid=$(pgrep bd-socket) 35 | # Launch nginx 36 | echo "starting nginx ..." 37 | nginx -g "daemon off;" & 38 | 39 | nginx_pid=$! 40 | 41 | wait_workers() 42 | { 43 | while ! pgrep -f 'nginx: worker process' >/dev/null 2>&1; do 44 | echo "waiting for nginx workers ..." 45 | sleep 2 46 | done 47 | } 48 | 49 | wait_workers 50 | 51 | test -n "${ENV_CONTROLLER_API_KEY}" && \ 52 | api_key=${ENV_CONTROLLER_API_KEY} 53 | 54 | # if instance_name is defined in the env vars, use it 55 | test -n "${ENV_CONTROLLER_INSTANCE_NAME}" && \ 56 | instance_name=${ENV_CONTROLLER_INSTANCE_NAME} 57 | 58 | test -n "${ENV_CONTROLLER_API_URL}" && \ 59 | controller_api_url=${ENV_CONTROLLER_API_URL} 60 | 61 | test -n "${ENV_CONTROLLER_LOCATION}" && \ 62 | location=${ENV_CONTROLLER_LOCATION} 63 | 64 | test -n "${ENV_CONTROLLER_INSTANCE_GROUP}" && \ 65 | instance_group=${ENV_CONTROLLER_INSTANCE_GROUP} 66 | 67 | if [ -n "${api_key}" -o -n "${instance_name}" -o -n "${controller_api_url}" -o -n "${location}" -o -n "${instance_group}" ]; then 68 | echo "updating ${agent_conf_file} ..." 69 | 70 | if [ ! -f "${agent_conf_file}" ]; then 71 | test -f "${agent_conf_file}.default" && \ 72 | cp -p "${agent_conf_file}.default" "${agent_conf_file}" || \ 73 | { echo "no ${agent_conf_file}.default found! exiting."; exit 1; } 74 | fi 75 | 76 | test -n "${api_key}" && \ 77 | echo " ---> using api_key = ${api_key}" && \ 78 | sh -c "sed -i.old -e 's/api_key.*$/api_key = $api_key/' \ 79 | ${agent_conf_file}" 80 | 81 | test -n "${controller_api_url}" && \ 82 | echo " ---> using controller api url = ${controller_api_url}" && \ 83 | sh -c "sed -i.old -e 's@^api_url.*@api_url = $controller_api_url@' \ 84 | ${agent_conf_file}" 85 | 86 | test -n "${instance_name}" && \ 87 | echo " ---> using instance_name = ${instance_name}" && \ 88 | sh -c "sed -i.old -e 's/instance_name.*$/instance_name = $instance_name/' \ 89 | ${agent_conf_file}" 90 | 91 | test -n "${location}" && \ 92 | echo " ---> using location = ${location}" && \ 93 | sh -c "sed -i.old -e 's/location_name.*$/location_name = $location/' \ 94 | ${agent_conf_file}" 95 | 96 | test -n "${instance_group}" && \ 97 | echo " ---> using instance group = ${instance_group}" && \ 98 | sh -c "sed -i.old -e 's/instance_group.*$/instance_group = $instance_group/' \ 99 | ${agent_conf_file}" 100 | 101 | if ! grep -Fq "security = " ${agent_conf_file}; then 102 | sh -c "sed -i.old -e 's/\[extensions\]/&\nsecurity = True/g' \ 103 | ${agent_conf_file}" 104 | fi 105 | 106 | test -f "${agent_conf_file}" && \ 107 | chmod 644 ${agent_conf_file} && \ 108 | chown nginx ${agent_conf_file} > /dev/null 2>&1 109 | 110 | test -f "${nginx_status_conf}" && \ 111 | chmod 644 ${nginx_status_conf} && \ 112 | chown nginx ${nginx_status_conf} > /dev/null 2>&1 113 | fi 114 | 115 | if ! grep '^api_key.*=[ ]*[[:alnum:]].*' ${agent_conf_file} > /dev/null 2>&1; then 116 | echo "no api_key found in ${agent_conf_file}! exiting." 117 | exit 1 118 | fi 119 | 120 | echo "starting controller-agent ..." 121 | /usr/bin/nginx-controller-agent > /dev/null 2>&1 < /dev/null & 122 | 123 | agent_pid=$! 124 | 125 | if [ $? != 0 ]; then 126 | echo "couldn't start the agent, please check ${agent_log_file}" 127 | exit 1 128 | fi 129 | 130 | wait_term() 131 | { 132 | wait ${agent_pid} 133 | trap - TERM 134 | kill -QUIT "${nginx_pid}" 2>/dev/null 135 | echo "waiting for nginx to stop..." 136 | wait ${nginx_pid} 137 | } 138 | 139 | wait_term 140 | 141 | echo "controller-agent process has stopped, exiting." 142 | -------------------------------------------------------------------------------- /centos/nap/nginx-plus-api.conf: -------------------------------------------------------------------------------- 1 | # This sample NGINX Plus configuration enables the NGINX Plus API, for live 2 | # activity monitoring and the built-in dashboard, dynamic configuration of 3 | # upstream groups, and key-value stores. Keep in mind that any features 4 | # added to the API in future NGINX Plus releases will be enabled 5 | # automatically by this file. 6 | # Created in May 2018 by NGINX, Inc. for NGINX Plus R14 and later. 7 | 8 | # Documentation: 9 | # https://docs.nginx.com/nginx/admin-guide/monitoring/live-activity-monitoring/ 10 | # https://www.nginx.com/blog/live-activity-monitoring-nginx-plus-3-simple-steps 11 | 12 | # To conform with the conventional configuration scheme, place this file in 13 | # the /etc/nginx/conf.d directory and add an 'include' directive that 14 | # references it in the main configuration file, /etc/nginx/nginx.conf, 15 | # either by name or with a wildcard expression. Then validate and reload 16 | # the configuration, for example with this command: 17 | # 18 | # nginx -t && nginx -s reload 19 | 20 | # Note that additional directives are required in other parts of the 21 | # configuration: 22 | # 23 | # For metrics to be gathered for an HTTP or TCP/UDP virtual server, you must 24 | # include the 'status_zone' directive in its 'server' block. See: 25 | # http://nginx.org/r/status_zone 26 | # 27 | # Similarly, for metrics to be gathered for an upstream server group, you 28 | # must include the 'zone' directive in the 'upstream' block. See: 29 | # http://nginx.org/r/zone 30 | # 31 | # For more information and instructions, see: 32 | # https://docs.nginx.com/nginx/admin-guide/monitoring/live-activity-monitoring#status_data 33 | 34 | # We strongly recommend that you restrict access to the NGINX Plus API so 35 | # that only authorized users can view metrics and configuration, change 36 | # configuration, or both. Here are a few options: 37 | # 38 | # (1) Configure your firewall to limit access to port 8080. 39 | # 40 | # (2) Use SSL/TLS client certificates. See: 41 | # https://docs.nginx.com/nginx/admin-guide/security-controls/terminating-ssl-http/ 42 | # 43 | # (3) Enable HTTP Basic authentication (RFC 7617) by uncommenting the 44 | # 'auth_basic*' directives in the 'server' block below. You can add users 45 | # with an htpasswd generator, which is readily available, or reuse an 46 | # existing htpasswd file (from an Apache HTTP Server, for example). See: 47 | # http://nginx.org/en/docs/http/ngx_http_auth_basic_module.html 48 | # 49 | # (4) Enable access from a defined network and disable it from all others, 50 | # by uncommenting the 'allow' and 'deny' directives in the 'server' block 51 | # below and specifying the appropriate network ranges. See: 52 | # http://nginx.org/en/docs/http/ngx_http_access_module.html 53 | # 54 | # You can create further restrictions on write operations, to distinguish 55 | # between users with read permission and those who can change configuration. 56 | # Uncomment the sample 'limit_except' directive in the 'location api' 57 | # block below. In addition to the HTTP Basic authentication shown, other 58 | # authentication schemes are supported. See: 59 | # http://nginx.org/r/limit_except 60 | server { 61 | # Conventional port for the NGINX Plus API is 8080 62 | listen 8080; 63 | 64 | # Uncomment to use HTTP Basic authentication; see (3) above 65 | #auth_basic "NGINX Plus API"; 66 | #auth_basic_user_file /etc/nginx/users; 67 | 68 | # Uncomment to use permissions based on IP address; see (4) above 69 | #allow 10.0.0.0/8; 70 | #deny all; 71 | 72 | # Conventional location for accessing the NGINX Plus API 73 | app_protect_enable on; 74 | app_protect_security_log_enable on; 75 | app_protect_security_log "/etc/nginx/custom_log_format.json" stderr; 76 | location /api/ { 77 | # Enable in read-write mode 78 | api write=on; 79 | # Uncomment to further restrict write permissions; see note above 80 | #limit_except GET { 81 | #auth_basic "NGINX Plus API"; 82 | #auth_basic_user_file /etc/nginx/admins; 83 | #} 84 | } 85 | 86 | # Conventional location of the NGINX Plus dashboard 87 | location = /dashboard.html { 88 | root /usr/share/nginx/html; 89 | } 90 | 91 | # Redirect requests for "/" to "/dashboard.html" 92 | location / { 93 | return 301 /dashboard.html; 94 | } 95 | 96 | # Redirect requests for pre-R14 dashboard 97 | location /status.html { 98 | return 301 /dashboard.html; 99 | } 100 | } 101 | 102 | # vim: syntax=nginx 103 | -------------------------------------------------------------------------------- /centos/no-nap/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM centos:7 2 | 3 | LABEL maintainer="NGINX Controller Engineering" 4 | 5 | # e.g '1234567890' 6 | ARG API_KEY 7 | ENV ENV_CONTROLLER_API_KEY=$API_KEY 8 | 9 | # e.g https:///install/controller-agent 10 | ARG CONTROLLER_URL 11 | ENV ENV_CONTROLLER_URL=$CONTROLLER_URL 12 | 13 | # e.g True or False 14 | ARG STORE_UUID=False 15 | ENV ENV_CONTROLLER_STORE_UUID=$STORE_UUID 16 | 17 | # e.g Instance location already defined in Controller 18 | ARG LOCATION 19 | ENV ENV_CONTROLLER_LOCATION=$LOCATION 20 | 21 | # e.g Instance group already defined in Controller 22 | ARG INSTANCE_GROUP 23 | ENV ENV_CONTROLLER_INSTANCE_GROUP=$INSTANCE_GROUP 24 | 25 | # NGXIN Plus release e.g 25 26 | ARG NGINX_PLUS_VERSION=25 27 | 28 | COPY nginx-plus-api.conf /etc/nginx/conf.d/ 29 | 30 | # Install NGINX Plus 31 | RUN --mount=type=secret,id=nginx-crt,dst=/etc/ssl/nginx/nginx-repo.crt,mode=0644 \ 32 | --mount=type=secret,id=nginx-key,dst=/etc/ssl/nginx/nginx-repo.key,mode=0644 \ 33 | set -ex \ 34 | && yum -y update && yum -y upgrade \ 35 | && yum -y install curl sudo vim procps ca-certificates gnupg wget binutils net-tools \ 36 | && \ 37 | NGINX_GPGKEY=573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62; \ 38 | found=''; \ 39 | for server in \ 40 | ha.pool.sks-keyservers.net \ 41 | hkp://keyserver.ubuntu.com:80 \ 42 | hkp://p80.pool.sks-keyservers.net:80 \ 43 | pgp.mit.edu \ 44 | ; do \ 45 | echo "Fetching GPG key $NGINX_GPGKEY from $server"; \ 46 | gpg --keyserver "$server" --recv-keys "0x$NGINX_GPGKEY" && found=yes && break; \ 47 | done; \ 48 | test -z "$found" && echo >&2 "error: failed to fetch GPG key $NGINX_GPGKEY" && exit 1; \ 49 | wget -P /etc/yum.repos.d https://cs.nginx.com/static/files/nginx-plus-7.4.repo \ 50 | # NGINX Javascript module needed for APIM 51 | && yum update && yum -y install nginx-plus-${NGINX_PLUS_VERSION}* nginx-plus-module-njs-${NGINX_PLUS_VERSION}* \ 52 | # Install Controller Agent 53 | && curl -k -sS -L ${CONTROLLER_URL} > install.sh \ 54 | && sed -i 's/^assume_yes=""/assume_yes="-y"/' install.sh \ 55 | && sed -i 's,-n "${NGINX_GPGKEY}",true,' install.sh \ 56 | && sh ./install.sh -y \ 57 | # cleanup sensitive nginx-plus data 58 | && rm /etc/yum.repos.d/nginx-plus-7.4.repo \ 59 | && gpg --batch --delete-keys "$NGINX_GPGKEY" 60 | 61 | # Forward request logs to Docker log collector 62 | RUN ln -sf /proc/1/fd/1 /var/log/nginx-controller/agent.log \ 63 | && ln -sf /proc/1/fd/2 /var/log/nginx/error.log 64 | 65 | COPY entrypoint.sh / 66 | 67 | EXPOSE 80 68 | 69 | STOPSIGNAL SIGTERM 70 | 71 | ENTRYPOINT ["sh", "/entrypoint.sh"] 72 | -------------------------------------------------------------------------------- /centos/no-nap/entrypoint.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 3 | # This script launches nginx and the NGINX Controller Agent. 4 | # 5 | 6 | # Variables 7 | agent_conf_file="/etc/controller-agent/agent.conf" 8 | agent_log_file="/var/log/nginx-controller/agent.log" 9 | nginx_status_conf="/etc/nginx/conf.d/stub_status.conf" 10 | api_key="" 11 | instance_name="$(hostname -f)" 12 | controller_api_url="" 13 | location="" 14 | 15 | handle_term() 16 | { 17 | echo "received TERM signal" 18 | echo "stopping controller-agent ..." 19 | kill -TERM "${agent_pid}" 2>/dev/null 20 | echo "stopping nginx ..." 21 | kill -TERM "${nginx_pid}" 2>/dev/null 22 | } 23 | 24 | trap 'handle_term' TERM 25 | 26 | # Launch nginx 27 | echo "starting nginx ..." 28 | nginx -g "daemon off;" & 29 | 30 | nginx_pid=$! 31 | 32 | wait_workers() 33 | { 34 | while ! pgrep -f 'nginx: worker process' >/dev/null 2>&1; do 35 | echo "waiting for nginx workers ..." 36 | sleep 2 37 | done 38 | } 39 | 40 | wait_workers 41 | 42 | test -n "${ENV_CONTROLLER_API_KEY}" && \ 43 | api_key=${ENV_CONTROLLER_API_KEY} 44 | 45 | # if instance_name is defined in the env vars, use it 46 | test -n "${ENV_CONTROLLER_INSTANCE_NAME}" && \ 47 | instance_name=${ENV_CONTROLLER_INSTANCE_NAME} 48 | 49 | test -n "${ENV_CONTROLLER_API_URL}" && \ 50 | controller_api_url=${ENV_CONTROLLER_API_URL} 51 | 52 | test -n "${ENV_CONTROLLER_LOCATION}" && \ 53 | location=${ENV_CONTROLLER_LOCATION} 54 | 55 | test -n "${ENV_CONTROLLER_INSTANCE_GROUP}" && \ 56 | instance_group=${ENV_CONTROLLER_INSTANCE_GROUP} 57 | 58 | if [ -n "${api_key}" -o -n "${instance_name}" -o -n "${controller_api_url}" -o -n "${location}" -o -n "${instance_group}" ]; then 59 | echo "updating ${agent_conf_file} ..." 60 | 61 | if [ ! -f "${agent_conf_file}" ]; then 62 | test -f "${agent_conf_file}.default" && \ 63 | cp -p "${agent_conf_file}.default" "${agent_conf_file}" || \ 64 | { echo "no ${agent_conf_file}.default found! exiting."; exit 1; } 65 | fi 66 | 67 | test -n "${api_key}" && \ 68 | echo " ---> using api_key = ${api_key}" && \ 69 | sh -c "sed -i.old -e 's/api_key.*$/api_key = $api_key/' \ 70 | ${agent_conf_file}" 71 | 72 | test -n "${controller_api_url}" && \ 73 | echo " ---> using controller api url = ${controller_api_url}" && \ 74 | sh -c "sed -i.old -e 's@^api_url.*@api_url = $controller_api_url@' \ 75 | ${agent_conf_file}" 76 | 77 | test -n "${instance_name}" && \ 78 | echo " ---> using instance_name = ${instance_name}" && \ 79 | sh -c "sed -i.old -e 's/instance_name.*$/instance_name = $instance_name/' \ 80 | ${agent_conf_file}" 81 | 82 | test -n "${location}" && \ 83 | echo " ---> using location = ${location}" && \ 84 | sh -c "sed -i.old -e 's/location_name.*$/location_name = $location/' \ 85 | ${agent_conf_file}" 86 | 87 | test -n "${instance_group}" && \ 88 | echo " ---> using instance group = ${instance_group}" && \ 89 | sh -c "sed -i.old -e 's/instance_group.*$/instance_group = $instance_group/' \ 90 | ${agent_conf_file}" 91 | 92 | test -f "${agent_conf_file}" && \ 93 | chmod 644 ${agent_conf_file} && \ 94 | chown nginx ${agent_conf_file} > /dev/null 2>&1 95 | 96 | test -f "${nginx_status_conf}" && \ 97 | chmod 644 ${nginx_status_conf} && \ 98 | chown nginx ${nginx_status_conf} > /dev/null 2>&1 99 | fi 100 | 101 | if ! grep '^api_key.*=[ ]*[[:alnum:]].*' ${agent_conf_file} > /dev/null 2>&1; then 102 | echo "no api_key found in ${agent_conf_file}! exiting." 103 | exit 1 104 | fi 105 | 106 | echo "starting controller-agent ..." 107 | /usr/bin/nginx-controller-agent > /dev/null 2>&1 < /dev/null & 108 | 109 | agent_pid=$! 110 | 111 | if [ $? != 0 ]; then 112 | echo "couldn't start the agent, please check ${agent_log_file}" 113 | exit 1 114 | fi 115 | 116 | wait_term() 117 | { 118 | wait ${agent_pid} 119 | trap - TERM 120 | kill -QUIT "${nginx_pid}" 2>/dev/null 121 | echo "waiting for nginx to stop..." 122 | wait ${nginx_pid} 123 | } 124 | 125 | wait_term 126 | 127 | echo "controller-agent process has stopped, exiting." 128 | -------------------------------------------------------------------------------- /centos/no-nap/nginx-plus-api.conf: -------------------------------------------------------------------------------- 1 | # This sample NGINX Plus configuration enables the NGINX Plus API, for live 2 | # activity monitoring and the built-in dashboard, dynamic configuration of 3 | # upstream groups, and key-value stores. Keep in mind that any features 4 | # added to the API in future NGINX Plus releases will be enabled 5 | # automatically by this file. 6 | # Created in May 2018 by NGINX, Inc. for NGINX Plus R14 and later. 7 | 8 | # Documentation: 9 | # https://docs.nginx.com/nginx/admin-guide/monitoring/live-activity-monitoring/ 10 | # https://www.nginx.com/blog/live-activity-monitoring-nginx-plus-3-simple-steps 11 | 12 | # To conform with the conventional configuration scheme, place this file in 13 | # the /etc/nginx/conf.d directory and add an 'include' directive that 14 | # references it in the main configuration file, /etc/nginx/nginx.conf, 15 | # either by name or with a wildcard expression. Then validate and reload 16 | # the configuration, for example with this command: 17 | # 18 | # nginx -t && nginx -s reload 19 | 20 | # Note that additional directives are required in other parts of the 21 | # configuration: 22 | # 23 | # For metrics to be gathered for an HTTP or TCP/UDP virtual server, you must 24 | # include the 'status_zone' directive in its 'server' block. See: 25 | # http://nginx.org/r/status_zone 26 | # 27 | # Similarly, for metrics to be gathered for an upstream server group, you 28 | # must include the 'zone' directive in the 'upstream' block. See: 29 | # http://nginx.org/r/zone 30 | # 31 | # For more information and instructions, see: 32 | # https://docs.nginx.com/nginx/admin-guide/monitoring/live-activity-monitoring#status_data 33 | 34 | # We strongly recommend that you restrict access to the NGINX Plus API so 35 | # that only authorized users can view metrics and configuration, change 36 | # configuration, or both. Here are a few options: 37 | # 38 | # (1) Configure your firewall to limit access to port 8080. 39 | # 40 | # (2) Use SSL/TLS client certificates. See: 41 | # https://docs.nginx.com/nginx/admin-guide/security-controls/terminating-ssl-http/ 42 | # 43 | # (3) Enable HTTP Basic authentication (RFC 7617) by uncommenting the 44 | # 'auth_basic*' directives in the 'server' block below. You can add users 45 | # with an htpasswd generator, which is readily available, or reuse an 46 | # existing htpasswd file (from an Apache HTTP Server, for example). See: 47 | # http://nginx.org/en/docs/http/ngx_http_auth_basic_module.html 48 | # 49 | # (4) Enable access from a defined network and disable it from all others, 50 | # by uncommenting the 'allow' and 'deny' directives in the 'server' block 51 | # below and specifying the appropriate network ranges. See: 52 | # http://nginx.org/en/docs/http/ngx_http_access_module.html 53 | # 54 | # You can create further restrictions on write operations, to distinguish 55 | # between users with read permission and those who can change configuration. 56 | # Uncomment the sample 'limit_except' directive in the 'location api' 57 | # block below. In addition to the HTTP Basic authentication shown, other 58 | # authentication schemes are supported. See: 59 | # http://nginx.org/r/limit_except 60 | 61 | server { 62 | # Conventional port for the NGINX Plus API is 8080 63 | listen 8080; 64 | 65 | # Uncomment to use HTTP Basic authentication; see (3) above 66 | #auth_basic "NGINX Plus API"; 67 | #auth_basic_user_file /etc/nginx/users; 68 | 69 | # Uncomment to use permissions based on IP address; see (4) above 70 | #allow 10.0.0.0/8; 71 | #deny all; 72 | 73 | # Conventional location for accessing the NGINX Plus API 74 | location /api/ { 75 | # Enable in read-write mode 76 | api write=on; 77 | 78 | # Uncomment to further restrict write permissions; see note above 79 | #limit_except GET { 80 | #auth_basic "NGINX Plus API"; 81 | #auth_basic_user_file /etc/nginx/admins; 82 | #} 83 | } 84 | 85 | # Conventional location of the NGINX Plus dashboard 86 | location = /dashboard.html { 87 | root /usr/share/nginx/html; 88 | } 89 | 90 | # Redirect requests for "/" to "/dashboard.html" 91 | location / { 92 | return 301 /dashboard.html; 93 | } 94 | 95 | # Redirect requests for pre-R14 dashboard 96 | location /status.html { 97 | return 301 /dashboard.html; 98 | } 99 | } 100 | 101 | # vim: syntax=nginx -------------------------------------------------------------------------------- /debian/examples/agent-layer/Dockerfile: -------------------------------------------------------------------------------- 1 | # nginx-plus is an example base image located in debian/examples/nginx-plus 2 | FROM nginx-plus 3 | 4 | # Start container with environment variables for CONTROLLER_URL and API_KEY 5 | # docker run --name apigw --hostname apigw -e CONTROLLER_URL=https:///install/controller-agent -e API_KEY=deadbeef -d -P nginx-ctrl 6 | 7 | # Install everything we will need to install the Controller Agent so that the container can start quickly 8 | RUN apt-get update &&\ 9 | apt install -y \ 10 | curl \ 11 | gnupg2 \ 12 | procps \ 13 | distro-info-data \ 14 | libmpdec2 \ 15 | lsb-release sudo 16 | 17 | EXPOSE 80 443 8080 18 | STOPSIGNAL SIGTERM 19 | 20 | WORKDIR /controller 21 | 22 | # This script will download install and start the agent using `service controller-agent start` command. 23 | # Agent service should be stopped gracefully using `service controller-agent stop` command, which 24 | # is not done in this example as a solution strictly depends on the form of cmd/entrypoint of the base image. 25 | # Example solution for multiple service management in docker image could be found in official docker 26 | # documentation: 27 | # https://docs.docker.com/config/containers/multi-service_container 28 | RUN printf "curl -skSL \$CONTROLLER_URL | bash -s - -y\n exec nginx -g 'daemon off;'" > start 29 | 30 | CMD ["sh", "/controller/start"] 31 | 32 | -------------------------------------------------------------------------------- /debian/examples/nginx-plus/Dockerfile: -------------------------------------------------------------------------------- 1 | # Example nginx-plus only Dockerfile 2 | FROM debian:stretch-slim 3 | 4 | LABEL maintainer="NGINX Docker Maintainers " 5 | 6 | # Install NGINX Plus 7 | RUN --mount=type=secret,id=nginx-crt,dst=/etc/ssl/nginx/nginx-repo.crt,mode=0644 \ 8 | --mount=type=secret,id=nginx-key,dst=/etc/ssl/nginx/nginx-repo.key,mode=0644 \ 9 | set -x \ 10 | && apt-get update && apt-get upgrade -y \ 11 | && apt-get install --no-install-recommends --no-install-suggests -y apt-transport-https ca-certificates gnupg1 procps wget curl\ 12 | && \ 13 | NGINX_GPGKEY=573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62; \ 14 | found=''; \ 15 | for server in \ 16 | ha.pool.sks-keyservers.net \ 17 | hkp://keyserver.ubuntu.com:80 \ 18 | hkp://p80.pool.sks-keyservers.net:80 \ 19 | pgp.mit.edu \ 20 | ; do \ 21 | echo "Fetching GPG key $NGINX_GPGKEY from $server"; \ 22 | apt-key adv --keyserver "$server" --keyserver-options timeout=10 --recv-keys "$NGINX_GPGKEY" && found=yes && break; \ 23 | done; \ 24 | test -z "$found" && echo >&2 "error: failed to fetch GPG key $NGINX_GPGKEY" && exit 1; \ 25 | echo "Acquire::https::plus-pkgs.nginx.com::Verify-Peer \"true\";" >> /etc/apt/apt.conf.d/90nginx \ 26 | && echo "Acquire::https::plus-pkgs.nginx.com::Verify-Host \"true\";" >> /etc/apt/apt.conf.d/90nginx \ 27 | && echo "Acquire::https::plus-pkgs.nginx.com::SslCert \"/etc/ssl/nginx/nginx-repo.crt\";" >> /etc/apt/apt.conf.d/90nginx \ 28 | && echo "Acquire::https::plus-pkgs.nginx.com::SslKey \"/etc/ssl/nginx/nginx-repo.key\";" >> /etc/apt/apt.conf.d/90nginx \ 29 | && printf "deb https://plus-pkgs.nginx.com/debian stretch nginx-plus\n" > /etc/apt/sources.list.d/nginx-plus.list \ 30 | && apt-get update && apt-get install -y nginx-plus \ 31 | && apt-get remove --purge --auto-remove -y gnupg1 \ 32 | && rm -rf /var/lib/apt/lists/* 33 | 34 | # Forward request logs to Docker log collector 35 | RUN ln -sf /dev/stdout /var/log/nginx/access.log \ 36 | && ln -sf /dev/stderr /var/log/nginx/error.log 37 | 38 | EXPOSE 80 39 | 40 | STOPSIGNAL SIGTERM 41 | 42 | COPY ./entrypoint.sh /entrypoint.sh 43 | 44 | ENTRYPOINT ["/entrypoint.sh"] 45 | CMD ["nginx", "-g", "daemon off;"] 46 | -------------------------------------------------------------------------------- /debian/examples/nginx-plus/entrypoint.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | exec "$@" 4 | -------------------------------------------------------------------------------- /debian/nap/Dockerfile: -------------------------------------------------------------------------------- 1 | ARG STORE_UUID=False 2 | FROM debian:buster-slim as nginx-installer 3 | 4 | LABEL maintainer="NGINX Controller Engineering" 5 | 6 | # NGXIN Plus release e.g 24 7 | ARG NGINX_PLUS_VERSION=25 8 | 9 | COPY nginx-plus-api.conf /etc/nginx/conf.d/ 10 | COPY entrypoint.sh / 11 | 12 | ENV NGINX_GPGKEY=573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62 13 | 14 | # Install NGINX Plus 15 | RUN --mount=type=secret,id=nginx-crt,dst=/etc/ssl/nginx/nginx-repo.crt,mode=0644 \ 16 | --mount=type=secret,id=nginx-key,dst=/etc/ssl/nginx/nginx-repo.key,mode=0644 \ 17 | set -ex \ 18 | && apt-get update && apt-get upgrade -y \ 19 | && apt-get install --no-install-recommends --no-install-suggests -y curl sudo procps apt-utils apt-transport-https ca-certificates gnupg1 distro-info-data libmpdec2 \ 20 | lsb-release python python-minimal binutils net-tools \ 21 | && \ 22 | found=''; \ 23 | for server in \ 24 | ha.pool.sks-keyservers.net \ 25 | hkp://keyserver.ubuntu.com:80 \ 26 | hkp://p80.pool.sks-keyservers.net:80 \ 27 | pgp.mit.edu \ 28 | ; do \ 29 | echo "Fetching GPG key $NGINX_GPGKEY from $server"; \ 30 | apt-key adv --keyserver "$server" --keyserver-options timeout=10 --recv-keys "$NGINX_GPGKEY" && found=yes && break; \ 31 | done; \ 32 | test -z "$found" && echo >&2 "error: failed to fetch GPG key $NGINX_GPGKEY" && exit 1; \ 33 | echo "Acquire::https::pkgs.nginx.com::Verify-Peer \"true\";" >> /etc/apt/apt.conf.d/90pkgs-nginx \ 34 | && echo "Acquire::https::pkgs.nginx.com::Verify-Host \"true\";" >> /etc/apt/apt.conf.d/90pkgs-nginx \ 35 | && echo "Acquire::https::pkgs.nginx.com::SslCert \"/etc/ssl/nginx/nginx-repo.crt\";" >> /etc/apt/apt.conf.d/90pkgs-nginx \ 36 | && echo "Acquire::https::pkgs.nginx.com::SslKey \"/etc/ssl/nginx/nginx-repo.key\";" >> /etc/apt/apt.conf.d/90pkgs-nginx \ 37 | && printf "deb https://pkgs.nginx.com/plus/debian buster nginx-plus\n" > /etc/apt/sources.list.d/nginx-plus.list \ 38 | && printf "deb https://pkgs.nginx.com/app-protect/debian buster nginx-plus\n" > /etc/apt/sources.list.d/nginx-app-protect.list \ 39 | # NGINX Javascript module needed for APIM 40 | && apt-get update && apt-get install -y nginx-plus=${NGINX_PLUS_VERSION}* nginx-plus-module-njs=${NGINX_PLUS_VERSION}* 41 | 42 | FROM nginx-installer as agent-installer 43 | # Install Controller Agent 44 | ARG CONTROLLER_URL 45 | ARG API_KEY 46 | ENV ENV_CONTROLLER_API_KEY=$API_KEY 47 | ARG STORE_UUID 48 | RUN --mount=type=secret,id=nginx-crt,dst=/etc/ssl/nginx/nginx-repo.crt,mode=0644 \ 49 | --mount=type=secret,id=nginx-key,dst=/etc/ssl/nginx/nginx-repo.key,mode=0644 \ 50 | curl -k -sS -L ${CONTROLLER_URL} > install.sh \ 51 | && sed -i 's/^assume_yes=""/assume_yes="-y"/' install.sh \ 52 | && sed -i 's,-n "${NGINX_GPGKEY}",true,' install.sh \ 53 | && sh ./install.sh -y 54 | 55 | FROM agent-installer as nap-installer 56 | # Install nginx-app-protect 57 | ARG NGINX_PLUS_VERSION=25 58 | RUN --mount=type=secret,id=nginx-crt,dst=/etc/ssl/nginx/nginx-repo.crt,mode=0644 \ 59 | --mount=type=secret,id=nginx-key,dst=/etc/ssl/nginx/nginx-repo.key,mode=0644 \ 60 | if [ "$NGINX_PLUS_VERSION" -lt 22 ] ; \ 61 | then \ 62 | echo "NGINX Plus version ${NGINX_PLUS_VERSION} is not supported by NAP" \ 63 | && exit 1 ; \ 64 | elif [ "$NGINX_PLUS_VERSION" -eq 22 ] ; then \ 65 | apt-get install -y app-protect-compiler=5.1.0-1~buster \ 66 | app-protect-engine=5.1.0-1~buster \ 67 | app-protect-plugin=3.243.1-1~buster \ 68 | nginx-plus-module-appprotect=22+3.243.1-1~buster \ 69 | app-protect=22+3.243.1-1~buster ; \ 70 | else \ 71 | apt-get install -y app-protect ; \ 72 | fi 73 | RUN sed -i "6 a load_module modules/ngx_http_app_protect_module.so;" /etc/nginx/nginx.conf 74 | 75 | 76 | FROM nap-installer as cleaner 77 | # cleanup sensitive nginx-plus data 78 | RUN rm /etc/apt/sources.list.d/nginx-plus.list \ 79 | && rm /etc/apt/apt.conf.d/90pkgs-nginx \ 80 | && apt-key del $NGINX_GPGKEY \ 81 | && rm -rf /var/lib/apt/lists/* 82 | 83 | 84 | FROM cleaner as runner 85 | ## e.g '1234567890' 86 | ARG API_KEY 87 | ENV ENV_CONTROLLER_API_KEY=$API_KEY 88 | 89 | # e.g https:///install/controller-agent 90 | ARG CONTROLLER_URL 91 | ENV ENV_CONTROLLER_URL=$CONTROLLER_URL 92 | 93 | # e.g True or False 94 | ARG STORE_UUID 95 | ENV ENV_CONTROLLER_STORE_UUID=$STORE_UUID 96 | 97 | # e.g Instance location already defined in Controller 98 | ARG LOCATION 99 | ENV ENV_CONTROLLER_LOCATION=$LOCATION 100 | 101 | # e.g Instance group already defined in Controller 102 | ARG INSTANCE_GROUP 103 | ENV ENV_CONTROLLER_INSTANCE_GROUP=$INSTANCE_GROUP 104 | 105 | # Forward request logs to Docker log collector 106 | RUN ln -sf /proc/1/fd/1 /var/log/nginx-controller/agent.log \ 107 | && ln -sf /proc/1/fd/2 /var/log/nginx/error.log 108 | 109 | COPY custom_log_format.json /etc/nginx/ 110 | 111 | EXPOSE 80 112 | 113 | STOPSIGNAL SIGTERM 114 | 115 | ENTRYPOINT ["sh", "/entrypoint.sh"] 116 | -------------------------------------------------------------------------------- /debian/nap/custom_log_format.json: -------------------------------------------------------------------------------- 1 | { 2 | "filter": { 3 | "request_type": "all" 4 | }, 5 | "content": { 6 | "format": "splunk", 7 | "max_request_size": "any", 8 | "max_message_size": "10k" 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /debian/nap/entrypoint.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 3 | # This script launches nginx and the NGINX Controller Agent. 4 | # 5 | 6 | # Variables 7 | agent_conf_file="/etc/controller-agent/agent.conf" 8 | agent_log_file="/var/log/nginx-controller/agent.log" 9 | nginx_status_conf="/etc/nginx/conf.d/stub_status.conf" 10 | 11 | api_key="" 12 | instance_name="$(hostname -f)" 13 | controller_api_url="" 14 | location="" 15 | 16 | handle_term() 17 | { 18 | echo "received TERM signal" 19 | echo "stopping bd-socket-plugin" 20 | kill -TERM "${bd_socket_pid}" 2>/dev/null 21 | echo "stopping bd-agent" 22 | kill -TERM "${bd_agent_pid}" 2>/dev/null 23 | echo "stopping controller-agent ..." 24 | kill -TERM "${agent_pid}" 2>/dev/null 25 | echo "stopping nginx ..." 26 | kill -TERM "${nginx_pid}" 2>/dev/null 27 | } 28 | 29 | trap 'handle_term' TERM 30 | 31 | # Launch NAP 32 | /bin/su -s /bin/bash -c '/opt/app_protect/bin/bd_agent &' nginx 33 | bd_agent_pid=$(pgrep bd_agent) 34 | /bin/su -s /bin/bash -c "/usr/share/ts/bin/bd-socket-plugin tmm_count 4 proc_cpuinfo_cpu_mhz 2000000 total_xml_memory 307200000 total_umu_max_size 3129344 sys_max_account_id 1024 no_static_config 2>&1 > /var/log/app_protect/bd-socket-plugin.log &" nginx 35 | bd_socket_pid=$(pgrep bd-socket) 36 | # Launch nginx 37 | echo "starting nginx ..." 38 | nginx -g "daemon off;" & 39 | 40 | nginx_pid=$! 41 | 42 | wait_workers() 43 | { 44 | while ! pgrep -f 'nginx: worker process' >/dev/null 2>&1; do 45 | echo "waiting for nginx workers ..." 46 | sleep 2 47 | done 48 | } 49 | 50 | wait_workers 51 | 52 | test -n "${ENV_CONTROLLER_API_KEY}" && \ 53 | api_key=${ENV_CONTROLLER_API_KEY} 54 | 55 | # if instance_name is defined in the env vars, use it 56 | test -n "${ENV_CONTROLLER_INSTANCE_NAME}" && \ 57 | instance_name=${ENV_CONTROLLER_INSTANCE_NAME} 58 | 59 | test -n "${ENV_CONTROLLER_API_URL}" && \ 60 | controller_api_url=${ENV_CONTROLLER_API_URL} 61 | 62 | test -n "${ENV_CONTROLLER_LOCATION}" && \ 63 | location=${ENV_CONTROLLER_LOCATION} 64 | 65 | test -n "${ENV_CONTROLLER_INSTANCE_GROUP}" && \ 66 | instance_group=${ENV_CONTROLLER_INSTANCE_GROUP} 67 | 68 | if [ -n "${api_key}" -o -n "${instance_name}" -o -n "${controller_api_url}" -o -n "${location}" -o -n "${instance_group}" ]; then 69 | echo "updating ${agent_conf_file} ..." 70 | 71 | if [ ! -f "${agent_conf_file}" ]; then 72 | test -f "${agent_conf_file}.default" && \ 73 | cp -p "${agent_conf_file}.default" "${agent_conf_file}" || \ 74 | { echo "no ${agent_conf_file}.default found! exiting."; exit 1; } 75 | fi 76 | 77 | test -n "${api_key}" && \ 78 | echo " ---> using api_key = ${api_key}" && \ 79 | sh -c "sed -i.old -e 's/api_key.*$/api_key = $api_key/' \ 80 | ${agent_conf_file}" 81 | 82 | test -n "${controller_api_url}" && \ 83 | echo " ---> using controller api url = ${controller_api_url}" && \ 84 | sh -c "sed -i.old -e 's@^api_url.*@api_url = $controller_api_url@' \ 85 | ${agent_conf_file}" 86 | 87 | test -n "${instance_name}" && \ 88 | echo " ---> using instance_name = ${instance_name}" && \ 89 | sh -c "sed -i.old -e 's/instance_name.*$/instance_name = $instance_name/' \ 90 | ${agent_conf_file}" 91 | 92 | test -n "${location}" && \ 93 | echo " ---> using location = ${location}" && \ 94 | sh -c "sed -i.old -e 's/location_name.*$/location_name = $location/' \ 95 | ${agent_conf_file}" 96 | 97 | test -n "${instance_group}" && \ 98 | echo " ---> using instance group = ${instance_group}" && \ 99 | sh -c "sed -i.old -e 's/instance_group.*$/instance_group = $instance_group/' \ 100 | ${agent_conf_file}" 101 | 102 | if ! grep -Fq "security = " ${agent_conf_file}; then 103 | sh -c "sed -i.old -e 's/\[extensions\]/&\nsecurity = True/g' \ 104 | ${agent_conf_file}" 105 | fi 106 | 107 | test -f "${agent_conf_file}" && \ 108 | chmod 644 ${agent_conf_file} && \ 109 | chown nginx ${agent_conf_file} > /dev/null 2>&1 110 | 111 | test -f "${nginx_status_conf}" && \ 112 | chmod 644 ${nginx_status_conf} && \ 113 | chown nginx ${nginx_status_conf} > /dev/null 2>&1 114 | fi 115 | 116 | if ! grep '^api_key.*=[ ]*[[:alnum:]].*' ${agent_conf_file} > /dev/null 2>&1; then 117 | echo "no api_key found in ${agent_conf_file}! exiting." 118 | exit 1 119 | fi 120 | 121 | echo "starting controller-agent ..." 122 | /usr/bin/nginx-controller-agent > /dev/null 2>&1 < /dev/null & 123 | 124 | agent_pid=$! 125 | 126 | if [ $? != 0 ]; then 127 | echo "couldn't start the agent, please check ${agent_log_file}" 128 | exit 1 129 | fi 130 | 131 | wait_term() 132 | { 133 | wait ${agent_pid} 134 | trap - TERM 135 | kill -QUIT "${nginx_pid}" 2>/dev/null 136 | echo "waiting for nginx to stop..." 137 | wait ${nginx_pid} 138 | } 139 | 140 | wait_term 141 | 142 | echo "controller-agent process has stopped, exiting." 143 | -------------------------------------------------------------------------------- /debian/nap/nginx-plus-api.conf: -------------------------------------------------------------------------------- 1 | # This sample NGINX Plus configuration enables the NGINX Plus API, for live 2 | # activity monitoring and the built-in dashboard, dynamic configuration of 3 | # upstream groups, and key-value stores. Keep in mind that any features 4 | # added to the API in future NGINX Plus releases will be enabled 5 | # automatically by this file. 6 | # Created in May 2018 by NGINX, Inc. for NGINX Plus R14 and later. 7 | 8 | # Documentation: 9 | # https://docs.nginx.com/nginx/admin-guide/monitoring/live-activity-monitoring/ 10 | # https://www.nginx.com/blog/live-activity-monitoring-nginx-plus-3-simple-steps 11 | 12 | # To conform with the conventional configuration scheme, place this file in 13 | # the /etc/nginx/conf.d directory and add an 'include' directive that 14 | # references it in the main configuration file, /etc/nginx/nginx.conf, 15 | # either by name or with a wildcard expression. Then validate and reload 16 | # the configuration, for example with this command: 17 | # 18 | # nginx -t && nginx -s reload 19 | 20 | # Note that additional directives are required in other parts of the 21 | # configuration: 22 | # 23 | # For metrics to be gathered for an HTTP or TCP/UDP virtual server, you must 24 | # include the 'status_zone' directive in its 'server' block. See: 25 | # http://nginx.org/r/status_zone 26 | # 27 | # Similarly, for metrics to be gathered for an upstream server group, you 28 | # must include the 'zone' directive in the 'upstream' block. See: 29 | # http://nginx.org/r/zone 30 | # 31 | # For more information and instructions, see: 32 | # https://docs.nginx.com/nginx/admin-guide/monitoring/live-activity-monitoring#status_data 33 | 34 | # We strongly recommend that you restrict access to the NGINX Plus API so 35 | # that only authorized users can view metrics and configuration, change 36 | # configuration, or both. Here are a few options: 37 | # 38 | # (1) Configure your firewall to limit access to port 8080. 39 | # 40 | # (2) Use SSL/TLS client certificates. See: 41 | # https://docs.nginx.com/nginx/admin-guide/security-controls/terminating-ssl-http/ 42 | # 43 | # (3) Enable HTTP Basic authentication (RFC 7617) by uncommenting the 44 | # 'auth_basic*' directives in the 'server' block below. You can add users 45 | # with an htpasswd generator, which is readily available, or reuse an 46 | # existing htpasswd file (from an Apache HTTP Server, for example). See: 47 | # http://nginx.org/en/docs/http/ngx_http_auth_basic_module.html 48 | # 49 | # (4) Enable access from a defined network and disable it from all others, 50 | # by uncommenting the 'allow' and 'deny' directives in the 'server' block 51 | # below and specifying the appropriate network ranges. See: 52 | # http://nginx.org/en/docs/http/ngx_http_access_module.html 53 | # 54 | # You can create further restrictions on write operations, to distinguish 55 | # between users with read permission and those who can change configuration. 56 | # Uncomment the sample 'limit_except' directive in the 'location api' 57 | # block below. In addition to the HTTP Basic authentication shown, other 58 | # authentication schemes are supported. See: 59 | # http://nginx.org/r/limit_except 60 | server { 61 | # Conventional port for the NGINX Plus API is 8080 62 | listen 8080; 63 | 64 | # Uncomment to use HTTP Basic authentication; see (3) above 65 | #auth_basic "NGINX Plus API"; 66 | #auth_basic_user_file /etc/nginx/users; 67 | 68 | # Uncomment to use permissions based on IP address; see (4) above 69 | #allow 10.0.0.0/8; 70 | #deny all; 71 | 72 | # Conventional location for accessing the NGINX Plus API 73 | app_protect_enable on; 74 | app_protect_security_log_enable on; 75 | app_protect_security_log "/etc/nginx/custom_log_format.json" stderr; 76 | location /api/ { 77 | # Enable in read-write mode 78 | api write=on; 79 | # Uncomment to further restrict write permissions; see note above 80 | #limit_except GET { 81 | #auth_basic "NGINX Plus API"; 82 | #auth_basic_user_file /etc/nginx/admins; 83 | #} 84 | } 85 | 86 | # Conventional location of the NGINX Plus dashboard 87 | location = /dashboard.html { 88 | root /usr/share/nginx/html; 89 | } 90 | 91 | # Redirect requests for "/" to "/dashboard.html" 92 | location / { 93 | return 301 /dashboard.html; 94 | } 95 | 96 | # Redirect requests for pre-R14 dashboard 97 | location /status.html { 98 | return 301 /dashboard.html; 99 | } 100 | } 101 | 102 | # vim: syntax=nginx 103 | -------------------------------------------------------------------------------- /debian/no-nap/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM debian:buster-slim 2 | 3 | LABEL maintainer="NGINX Controller Engineering" 4 | 5 | # e.g '1234567890' 6 | ARG API_KEY 7 | ENV ENV_CONTROLLER_API_KEY=$API_KEY 8 | 9 | # e.g https:///install/controller-agent 10 | ARG CONTROLLER_URL 11 | ENV ENV_CONTROLLER_URL=$CONTROLLER_URL 12 | 13 | # e.g True or False 14 | ARG STORE_UUID=False 15 | ENV ENV_CONTROLLER_STORE_UUID=$STORE_UUID 16 | 17 | # e.g Instance location already defined in Controller 18 | ARG LOCATION 19 | ENV ENV_CONTROLLER_LOCATION=$LOCATION 20 | 21 | # e.g Instance group already defined in Controller 22 | ARG INSTANCE_GROUP 23 | ENV ENV_CONTROLLER_INSTANCE_GROUP=$INSTANCE_GROUP 24 | 25 | # NGXIN Plus release e.g 25 26 | ARG NGINX_PLUS_VERSION=25 27 | 28 | COPY nginx-plus-api.conf /etc/nginx/conf.d/ 29 | COPY entrypoint.sh / 30 | 31 | # Install NGINX Plus 32 | RUN --mount=type=secret,id=nginx-crt,dst=/etc/ssl/nginx/nginx-repo.crt,mode=0644 \ 33 | --mount=type=secret,id=nginx-key,dst=/etc/ssl/nginx/nginx-repo.key,mode=0644 \ 34 | set -ex \ 35 | && apt-get update && apt-get upgrade -y \ 36 | && apt-get install --no-install-recommends --no-install-suggests -y curl sudo procps apt-utils apt-transport-https ca-certificates gnupg1 distro-info-data libmpdec2 \ 37 | lsb-release python python-minimal binutils net-tools \ 38 | && \ 39 | NGINX_GPGKEY=573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62; \ 40 | found=''; \ 41 | for server in \ 42 | ha.pool.sks-keyservers.net \ 43 | hkp://keyserver.ubuntu.com:80 \ 44 | hkp://p80.pool.sks-keyservers.net:80 \ 45 | pgp.mit.edu \ 46 | ; do \ 47 | echo "Fetching GPG key $NGINX_GPGKEY from $server"; \ 48 | apt-key adv --keyserver "$server" --keyserver-options timeout=10 --recv-keys "$NGINX_GPGKEY" && found=yes && break; \ 49 | done; \ 50 | test -z "$found" && echo >&2 "error: failed to fetch GPG key $NGINX_GPGKEY" && exit 1; \ 51 | echo "Acquire::https::pkgs.nginx.com::Verify-Peer \"true\";" >> /etc/apt/apt.conf.d/90pkgs-nginx \ 52 | && echo "Acquire::https::pkgs.nginx.com::Verify-Host \"true\";" >> /etc/apt/apt.conf.d/90pkgs-nginx \ 53 | && echo "Acquire::https::pkgs.nginx.com::SslCert \"/etc/ssl/nginx/nginx-repo.crt\";" >> /etc/apt/apt.conf.d/90pkgs-nginx \ 54 | && echo "Acquire::https::pkgs.nginx.com::SslKey \"/etc/ssl/nginx/nginx-repo.key\";" >> /etc/apt/apt.conf.d/90pkgs-nginx \ 55 | && printf "deb https://pkgs.nginx.com/plus/debian buster nginx-plus\n" > /etc/apt/sources.list.d/nginx-plus.list \ 56 | # NGINX Javascript module needed for APIM 57 | && apt-get update && apt-get install -y nginx-plus=${NGINX_PLUS_VERSION}* nginx-plus-module-njs=${NGINX_PLUS_VERSION}* \ 58 | && rm -rf /var/lib/apt/lists/* \ 59 | # Install Controller Agent 60 | && curl -k -sS -L ${CONTROLLER_URL} > install.sh \ 61 | && sed -i 's/^assume_yes=""/assume_yes="-y"/' install.sh \ 62 | && sed -i 's,-n "${NGINX_GPGKEY}",true,' install.sh \ 63 | && sh ./install.sh -y \ 64 | # cleanup sensitive nginx-plus data 65 | && rm /etc/apt/sources.list.d/nginx-plus.list \ 66 | && rm /etc/apt/apt.conf.d/90pkgs-nginx \ 67 | && apt-key del "$NGINX_GPGKEY" 68 | 69 | # Forward request logs to Docker log collector 70 | RUN ln -sf /proc/1/fd/1 /var/log/nginx-controller/agent.log \ 71 | && ln -sf /proc/1/fd/2 /var/log/nginx/error.log 72 | 73 | EXPOSE 80 74 | 75 | STOPSIGNAL SIGTERM 76 | 77 | ENTRYPOINT ["sh", "/entrypoint.sh"] 78 | -------------------------------------------------------------------------------- /debian/no-nap/entrypoint.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 3 | # This script launches nginx and the NGINX Controller Agent. 4 | # 5 | 6 | # Variables 7 | agent_conf_file="/etc/controller-agent/agent.conf" 8 | agent_log_file="/var/log/nginx-controller/agent.log" 9 | nginx_status_conf="/etc/nginx/conf.d/stub_status.conf" 10 | api_key="" 11 | instance_name="$(hostname -f)" 12 | controller_api_url="" 13 | location="" 14 | 15 | handle_term() 16 | { 17 | echo "received TERM signal" 18 | echo "stopping controller-agent ..." 19 | kill -TERM "${agent_pid}" 2>/dev/null 20 | echo "stopping nginx ..." 21 | kill -TERM "${nginx_pid}" 2>/dev/null 22 | } 23 | 24 | trap 'handle_term' TERM 25 | 26 | # Launch nginx 27 | echo "starting nginx ..." 28 | nginx -g "daemon off;" & 29 | 30 | nginx_pid=$! 31 | 32 | wait_workers() 33 | { 34 | while ! pgrep -f 'nginx: worker process' >/dev/null 2>&1; do 35 | echo "waiting for nginx workers ..." 36 | sleep 2 37 | done 38 | } 39 | 40 | wait_workers 41 | 42 | test -n "${ENV_CONTROLLER_API_KEY}" && \ 43 | api_key=${ENV_CONTROLLER_API_KEY} 44 | 45 | # if instance_name is defined in the env vars, use it 46 | test -n "${ENV_CONTROLLER_INSTANCE_NAME}" && \ 47 | instance_name=${ENV_CONTROLLER_INSTANCE_NAME} 48 | 49 | test -n "${ENV_CONTROLLER_API_URL}" && \ 50 | controller_api_url=${ENV_CONTROLLER_API_URL} 51 | 52 | test -n "${ENV_CONTROLLER_LOCATION}" && \ 53 | location=${ENV_CONTROLLER_LOCATION} 54 | 55 | test -n "${ENV_CONTROLLER_INSTANCE_GROUP}" && \ 56 | instance_group=${ENV_CONTROLLER_INSTANCE_GROUP} 57 | 58 | if [ -n "${api_key}" -o -n "${instance_name}" -o -n "${controller_api_url}" -o -n "${location}" -o -n "${instance_group}" ]; then 59 | echo "updating ${agent_conf_file} ..." 60 | 61 | if [ ! -f "${agent_conf_file}" ]; then 62 | test -f "${agent_conf_file}.default" && \ 63 | cp -p "${agent_conf_file}.default" "${agent_conf_file}" || \ 64 | { echo "no ${agent_conf_file}.default found! exiting."; exit 1; } 65 | fi 66 | 67 | test -n "${api_key}" && \ 68 | echo " ---> using api_key = ${api_key}" && \ 69 | sh -c "sed -i.old -e 's/api_key.*$/api_key = $api_key/' \ 70 | ${agent_conf_file}" 71 | 72 | test -n "${controller_api_url}" && \ 73 | echo " ---> using controller api url = ${controller_api_url}" && \ 74 | sh -c "sed -i.old -e 's@^api_url.*@api_url = $controller_api_url@' \ 75 | ${agent_conf_file}" 76 | 77 | test -n "${instance_name}" && \ 78 | echo " ---> using instance_name = ${instance_name}" && \ 79 | sh -c "sed -i.old -e 's/instance_name.*$/instance_name = $instance_name/' \ 80 | ${agent_conf_file}" 81 | 82 | test -n "${location}" && \ 83 | echo " ---> using location = ${location}" && \ 84 | sh -c "sed -i.old -e 's/location_name.*$/location_name = $location/' \ 85 | ${agent_conf_file}" 86 | 87 | test -n "${instance_group}" && \ 88 | echo " ---> using instance group = ${instance_group}" && \ 89 | sh -c "sed -i.old -e 's/instance_group.*$/instance_group = $instance_group/' \ 90 | ${agent_conf_file}" 91 | 92 | test -f "${agent_conf_file}" && \ 93 | chmod 644 ${agent_conf_file} && \ 94 | chown nginx ${agent_conf_file} > /dev/null 2>&1 95 | 96 | test -f "${nginx_status_conf}" && \ 97 | chmod 644 ${nginx_status_conf} && \ 98 | chown nginx ${nginx_status_conf} > /dev/null 2>&1 99 | fi 100 | 101 | if ! grep '^api_key.*=[ ]*[[:alnum:]].*' ${agent_conf_file} > /dev/null 2>&1; then 102 | echo "no api_key found in ${agent_conf_file}! exiting." 103 | exit 1 104 | fi 105 | 106 | echo "starting controller-agent ..." 107 | /usr/bin/nginx-controller-agent > /dev/null 2>&1 < /dev/null & 108 | 109 | agent_pid=$! 110 | 111 | if [ $? != 0 ]; then 112 | echo "couldn't start the agent, please check ${agent_log_file}" 113 | exit 1 114 | fi 115 | 116 | wait_term() 117 | { 118 | wait ${agent_pid} 119 | trap - TERM 120 | kill -QUIT "${nginx_pid}" 2>/dev/null 121 | echo "waiting for nginx to stop..." 122 | wait ${nginx_pid} 123 | } 124 | 125 | wait_term 126 | 127 | echo "controller-agent process has stopped, exiting." 128 | -------------------------------------------------------------------------------- /debian/no-nap/nginx-plus-api.conf: -------------------------------------------------------------------------------- 1 | # This sample NGINX Plus configuration enables the NGINX Plus API, for live 2 | # activity monitoring and the built-in dashboard, dynamic configuration of 3 | # upstream groups, and key-value stores. Keep in mind that any features 4 | # added to the API in future NGINX Plus releases will be enabled 5 | # automatically by this file. 6 | # Created in May 2018 by NGINX, Inc. for NGINX Plus R14 and later. 7 | 8 | # Documentation: 9 | # https://docs.nginx.com/nginx/admin-guide/monitoring/live-activity-monitoring/ 10 | # https://www.nginx.com/blog/live-activity-monitoring-nginx-plus-3-simple-steps 11 | 12 | # To conform with the conventional configuration scheme, place this file in 13 | # the /etc/nginx/conf.d directory and add an 'include' directive that 14 | # references it in the main configuration file, /etc/nginx/nginx.conf, 15 | # either by name or with a wildcard expression. Then validate and reload 16 | # the configuration, for example with this command: 17 | # 18 | # nginx -t && nginx -s reload 19 | 20 | # Note that additional directives are required in other parts of the 21 | # configuration: 22 | # 23 | # For metrics to be gathered for an HTTP or TCP/UDP virtual server, you must 24 | # include the 'status_zone' directive in its 'server' block. See: 25 | # http://nginx.org/r/status_zone 26 | # 27 | # Similarly, for metrics to be gathered for an upstream server group, you 28 | # must include the 'zone' directive in the 'upstream' block. See: 29 | # http://nginx.org/r/zone 30 | # 31 | # For more information and instructions, see: 32 | # https://docs.nginx.com/nginx/admin-guide/monitoring/live-activity-monitoring#status_data 33 | 34 | # We strongly recommend that you restrict access to the NGINX Plus API so 35 | # that only authorized users can view metrics and configuration, change 36 | # configuration, or both. Here are a few options: 37 | # 38 | # (1) Configure your firewall to limit access to port 8080. 39 | # 40 | # (2) Use SSL/TLS client certificates. See: 41 | # https://docs.nginx.com/nginx/admin-guide/security-controls/terminating-ssl-http/ 42 | # 43 | # (3) Enable HTTP Basic authentication (RFC 7617) by uncommenting the 44 | # 'auth_basic*' directives in the 'server' block below. You can add users 45 | # with an htpasswd generator, which is readily available, or reuse an 46 | # existing htpasswd file (from an Apache HTTP Server, for example). See: 47 | # http://nginx.org/en/docs/http/ngx_http_auth_basic_module.html 48 | # 49 | # (4) Enable access from a defined network and disable it from all others, 50 | # by uncommenting the 'allow' and 'deny' directives in the 'server' block 51 | # below and specifying the appropriate network ranges. See: 52 | # http://nginx.org/en/docs/http/ngx_http_access_module.html 53 | # 54 | # You can create further restrictions on write operations, to distinguish 55 | # between users with read permission and those who can change configuration. 56 | # Uncomment the sample 'limit_except' directive in the 'location api' 57 | # block below. In addition to the HTTP Basic authentication shown, other 58 | # authentication schemes are supported. See: 59 | # http://nginx.org/r/limit_except 60 | 61 | server { 62 | # Conventional port for the NGINX Plus API is 8080 63 | listen 8080; 64 | 65 | # Uncomment to use HTTP Basic authentication; see (3) above 66 | #auth_basic "NGINX Plus API"; 67 | #auth_basic_user_file /etc/nginx/users; 68 | 69 | # Uncomment to use permissions based on IP address; see (4) above 70 | #allow 10.0.0.0/8; 71 | #deny all; 72 | 73 | # Conventional location for accessing the NGINX Plus API 74 | location /api/ { 75 | # Enable in read-write mode 76 | api write=on; 77 | 78 | # Uncomment to further restrict write permissions; see note above 79 | #limit_except GET { 80 | #auth_basic "NGINX Plus API"; 81 | #auth_basic_user_file /etc/nginx/admins; 82 | #} 83 | } 84 | 85 | # Conventional location of the NGINX Plus dashboard 86 | location = /dashboard.html { 87 | root /usr/share/nginx/html; 88 | } 89 | 90 | # Redirect requests for "/" to "/dashboard.html" 91 | location / { 92 | return 301 /dashboard.html; 93 | } 94 | 95 | # Redirect requests for pre-R14 dashboard 96 | location /status.html { 97 | return 301 /dashboard.html; 98 | } 99 | } 100 | 101 | # vim: syntax=nginx -------------------------------------------------------------------------------- /ubuntu/examples/agent-layer/Dockerfile: -------------------------------------------------------------------------------- 1 | # nginx-plus is an example base image located in debian/examples/nginx-plus 2 | FROM nginx-plus 3 | 4 | # Start container with environment variables for CONTROLLER_URL and API_KEY 5 | # docker run --name apigw --hostname apigw -e CONTROLLER_URL=https:///install/controller-agent -e API_KEY=deadbeef -d -P nginx-ctrl 6 | 7 | # Install everything we will need to install the Controller Agent so that the container can start quickly 8 | RUN apt-get update &&\ 9 | apt install -y \ 10 | curl gnupg procps distro-info-data libmpdec2 \ 11 | lsb-release sudo 12 | 13 | EXPOSE 80 443 8080 14 | STOPSIGNAL SIGTERM 15 | 16 | WORKDIR /controller 17 | 18 | # This script will download install and start the agent using `service controller-agent start` command. 19 | # Agent service should be stopped gracefully using `service controller-agent stop` command, which 20 | # is not done in this example as a solution strictly depends on the form of cmd/entrypoint of the base image. 21 | # Example solution for multiple service management in docker image could be found in official docker 22 | # documentation: 23 | # https://docs.docker.com/config/containers/multi-service_container 24 | RUN printf "curl -skSL \$CONTROLLER_URL | bash -s - -y\n exec nginx -g 'daemon off;'" > start 25 | 26 | CMD ["sh", "/controller/start"] 27 | -------------------------------------------------------------------------------- /ubuntu/examples/nginx-plus/Dockerfile: -------------------------------------------------------------------------------- 1 | # Example nginx-plus only Dockerfile 2 | FROM ubuntu:18.04 3 | 4 | LABEL maintainer="NGINX Docker Maintainers " 5 | 6 | # Install NGINX Plus 7 | RUN --mount=type=secret,id=nginx-crt,dst=/etc/ssl/nginx/nginx-repo.crt,mode=0644 \ 8 | --mount=type=secret,id=nginx-key,dst=/etc/ssl/nginx/nginx-repo.key,mode=0644 \ 9 | set -x \ 10 | && apt-get update && apt-get upgrade -y \ 11 | && apt-get install --no-install-recommends --no-install-suggests -y apt-transport-https ca-certificates gnupg1 procps wget curl vim \ 12 | && \ 13 | NGINX_GPGKEY=573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62; \ 14 | found=''; \ 15 | for server in \ 16 | ha.pool.sks-keyservers.net \ 17 | hkp://keyserver.ubuntu.com:80 \ 18 | hkp://p80.pool.sks-keyservers.net:80 \ 19 | pgp.mit.edu \ 20 | ; do \ 21 | echo "Fetching GPG key $NGINX_GPGKEY from $server"; \ 22 | apt-key adv --keyserver "$server" --keyserver-options timeout=10 --recv-keys "$NGINX_GPGKEY" && found=yes && break; \ 23 | done; \ 24 | test -z "$found" && echo >&2 "error: failed to fetch GPG key $NGINX_GPGKEY" && exit 1; \ 25 | echo "Acquire::https::plus-pkgs.nginx.com::Verify-Peer \"true\";" >> /etc/apt/apt.conf.d/90nginx \ 26 | && echo "Acquire::https::plus-pkgs.nginx.com::Verify-Host \"true\";" >> /etc/apt/apt.conf.d/90nginx \ 27 | && echo "Acquire::https::plus-pkgs.nginx.com::SslCert \"/etc/ssl/nginx/nginx-repo.crt\";" >> /etc/apt/apt.conf.d/90nginx \ 28 | && echo "Acquire::https::plus-pkgs.nginx.com::SslKey \"/etc/ssl/nginx/nginx-repo.key\";" >> /etc/apt/apt.conf.d/90nginx \ 29 | && printf "deb https://plus-pkgs.nginx.com/ubuntu bionic nginx-plus\n" > /etc/apt/sources.list.d/nginx-plus.list \ 30 | && apt-get update && apt-get install -y nginx-plus \ 31 | && apt-get remove --purge --auto-remove -y gnupg1 \ 32 | && rm -rf /var/lib/apt/lists/* 33 | 34 | # Forward request logs to Docker log collector 35 | RUN ln -sf /dev/stdout /var/log/nginx/access.log \ 36 | && ln -sf /dev/stderr /var/log/nginx/error.log 37 | 38 | EXPOSE 80 39 | 40 | STOPSIGNAL SIGTERM 41 | 42 | COPY ./entrypoint.sh /entrypoint.sh 43 | 44 | ENTRYPOINT ["/entrypoint.sh"] 45 | CMD ["nginx", "-g", "daemon off;"] 46 | -------------------------------------------------------------------------------- /ubuntu/examples/nginx-plus/entrypoint.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | exec "$@" 4 | -------------------------------------------------------------------------------- /ubuntu/nap/Dockerfile: -------------------------------------------------------------------------------- 1 | ARG STORE_UUID=False 2 | # This dockerfile also support Ubuntu 16.04 3 | # To use Ubuntu 16.04 replace base image with below one: 4 | #FROM ubuntu:16.04 as nginx-installer 5 | FROM ubuntu:18.04 as nginx-installer 6 | 7 | LABEL maintainer="NGINX Controller Engineering" 8 | 9 | # NGXIN Plus release e.g 25 10 | ARG NGINX_PLUS_VERSION=25 11 | 12 | COPY nginx-plus-api.conf /etc/nginx/conf.d/ 13 | COPY entrypoint.sh / 14 | 15 | ENV NGINX_GPGKEY=573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62 16 | 17 | # Install NGINX Plus 18 | RUN --mount=type=secret,id=nginx-crt,dst=/etc/ssl/nginx/nginx-repo.crt,mode=0644 \ 19 | --mount=type=secret,id=nginx-key,dst=/etc/ssl/nginx/nginx-repo.key,mode=0644 \ 20 | set -ex \ 21 | && apt-get update \ 22 | && apt-get install --no-install-recommends --no-install-suggests -y \ 23 | curl \ 24 | sudo\ 25 | vim\ 26 | procps\ 27 | gnupg\ 28 | apt-utils\ 29 | apt-transport-https\ 30 | ca-certificates\ 31 | distro-info-data\ 32 | libmpdec2\ 33 | lsb-release \ 34 | binutils \ 35 | net-tools \ 36 | && apt-get upgrade -y \ 37 | && \ 38 | found=''; \ 39 | for server in \ 40 | ha.pool.sks-keyservers.net \ 41 | hkp://keyserver.ubuntu.com:80 \ 42 | hkp://p80.pool.sks-keyservers.net:80 \ 43 | pgp.mit.edu \ 44 | ; do \ 45 | echo "Fetching GPG key $NGINX_GPGKEY from $server"; \ 46 | apt-key adv --keyserver "$server" --keyserver-options timeout=10 --recv-keys "$NGINX_GPGKEY" && found=yes && break; \ 47 | done; \ 48 | test -z "$found" && echo >&2 "error: failed to fetch GPG key $NGINX_GPGKEY" && exit 1; \ 49 | echo "Acquire::https::pkgs.nginx.com::Verify-Peer \"true\";" >> /etc/apt/apt.conf.d/90pkgs-nginx \ 50 | && echo "Acquire::https::pkgs.nginx.com::Verify-Host \"true\";" >> /etc/apt/apt.conf.d/90pkgs-nginx \ 51 | && echo "Acquire::https::pkgs.nginx.com::SslCert \"/etc/ssl/nginx/nginx-repo.crt\";" >> /etc/apt/apt.conf.d/90pkgs-nginx \ 52 | && echo "Acquire::https::pkgs.nginx.com::SslKey \"/etc/ssl/nginx/nginx-repo.key\";" >> /etc/apt/apt.conf.d/90pkgs-nginx \ 53 | && printf "deb https://pkgs.nginx.com/plus/ubuntu $(lsb_release -cs) nginx-plus\n" > /etc/apt/sources.list.d/nginx-plus.list \ 54 | && printf "deb https://pkgs.nginx.com/app-protect/ubuntu $(lsb_release -cs) nginx-plus\n" > /etc/apt/sources.list.d/nginx-app-protect.list \ 55 | # NGINX Javascript module needed for APIM 56 | && apt-get update && apt-get install -y nginx-plus=${NGINX_PLUS_VERSION}* nginx-plus-module-njs=${NGINX_PLUS_VERSION}* 57 | 58 | FROM nginx-installer as agent-installer 59 | # Install Controller Agent 60 | ARG CONTROLLER_URL 61 | ARG API_KEY 62 | ENV ENV_CONTROLLER_API_KEY=$API_KEY 63 | ARG STORE_UUID 64 | RUN --mount=type=secret,id=nginx-crt,dst=/etc/ssl/nginx/nginx-repo.crt,mode=0644 \ 65 | --mount=type=secret,id=nginx-key,dst=/etc/ssl/nginx/nginx-repo.key,mode=0644 \ 66 | curl -k -sS -L ${CONTROLLER_URL} > install.sh \ 67 | && sed -i 's/^assume_yes=""/assume_yes="-y"/' install.sh \ 68 | && sed -i 's,-n "${NGINX_GPGKEY}",true,' install.sh \ 69 | && sh ./install.sh -y 70 | 71 | 72 | FROM agent-installer as nap-installer 73 | # Install nginx-app-protect 74 | ARG NGINX_PLUS_VERSION=25 75 | RUN --mount=type=secret,id=nginx-crt,dst=/etc/ssl/nginx/nginx-repo.crt,mode=0644 \ 76 | --mount=type=secret,id=nginx-key,dst=/etc/ssl/nginx/nginx-repo.key,mode=0644 \ 77 | if [ "$NGINX_PLUS_VERSION" -lt 23 ] ; then \ 78 | echo "NGINX Plus version ${NGINX_PLUS_VERSION} is not supported by NAP" \ 79 | && exit 1 ; \ 80 | else \ 81 | apt-get install -y app-protect ; \ 82 | fi 83 | RUN sed -i "6 a load_module modules/ngx_http_app_protect_module.so;" /etc/nginx/nginx.conf 84 | 85 | 86 | FROM nap-installer as cleaner 87 | # cleanup sensitive nginx-plus data 88 | RUN rm /etc/apt/sources.list.d/nginx-plus.list \ 89 | && rm /etc/apt/apt.conf.d/90pkgs-nginx \ 90 | && apt-key del $NGINX_GPGKEY \ 91 | && rm -rf /var/lib/apt/lists/* 92 | 93 | 94 | FROM cleaner as runner 95 | ## e.g '1234567890' 96 | ARG API_KEY 97 | ENV ENV_CONTROLLER_API_KEY=$API_KEY 98 | 99 | # e.g https:///install/controller-agent 100 | ARG CONTROLLER_URL 101 | ENV ENV_CONTROLLER_URL=$CONTROLLER_URL 102 | 103 | # e.g True or False 104 | ARG STORE_UUID 105 | ENV ENV_CONTROLLER_STORE_UUID=$STORE_UUID 106 | 107 | # e.g Instance location already defined in Controller 108 | ARG LOCATION 109 | ENV ENV_CONTROLLER_LOCATION=$LOCATION 110 | 111 | # e.g Instance group already defined in Controller 112 | ARG INSTANCE_GROUP 113 | ENV ENV_CONTROLLER_INSTANCE_GROUP=$INSTANCE_GROUP 114 | 115 | # Forward request logs to Docker log collector 116 | RUN ln -sf /proc/1/fd/1 /var/log/nginx-controller/agent.log \ 117 | && ln -sf /proc/1/fd/2 /var/log/nginx/error.log 118 | 119 | COPY custom_log_format.json /etc/nginx/ 120 | 121 | EXPOSE 80 122 | 123 | STOPSIGNAL SIGTERM 124 | 125 | ENTRYPOINT ["sh", "/entrypoint.sh"] 126 | -------------------------------------------------------------------------------- /ubuntu/nap/custom_log_format.json: -------------------------------------------------------------------------------- 1 | { 2 | "filter": { 3 | "request_type": "all" 4 | }, 5 | "content": { 6 | "format": "splunk", 7 | "max_request_size": "any", 8 | "max_message_size": "10k" 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /ubuntu/nap/entrypoint.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 3 | # This script launches nginx and the NGINX Controller Agent. 4 | # 5 | 6 | # Variables 7 | agent_conf_file="/etc/controller-agent/agent.conf" 8 | agent_log_file="/var/log/nginx-controller/agent.log" 9 | nginx_status_conf="/etc/nginx/conf.d/stub_status.conf" 10 | 11 | api_key="" 12 | instance_name="$(hostname -f)" 13 | controller_api_url="" 14 | location="" 15 | 16 | handle_term() 17 | { 18 | echo "received TERM signal" 19 | echo "stopping bd-socket-plugin" 20 | kill -TERM "${bd_socket_pid}" 2>/dev/null 21 | echo "stopping bd-agent" 22 | kill -TERM "${bd_agent_pid}" 2>/dev/null 23 | echo "stopping controller-agent ..." 24 | kill -TERM "${agent_pid}" 2>/dev/null 25 | echo "stopping nginx ..." 26 | kill -TERM "${nginx_pid}" 2>/dev/null 27 | } 28 | 29 | trap 'handle_term' TERM 30 | 31 | # Launch NAP 32 | /bin/su -s /bin/bash -c '/opt/app_protect/bin/bd_agent &' nginx 33 | bd_agent_pid=$(pgrep bd_agent) 34 | /bin/su -s /bin/bash -c "/usr/share/ts/bin/bd-socket-plugin tmm_count 4 proc_cpuinfo_cpu_mhz 2000000 total_xml_memory 307200000 total_umu_max_size 3129344 sys_max_account_id 1024 no_static_config 2>&1 > /var/log/app_protect/bd-socket-plugin.log &" nginx 35 | bd_socket_pid=$(pgrep bd-socket) 36 | # Launch nginx 37 | echo "starting nginx ..." 38 | nginx -g "daemon off;" & 39 | 40 | nginx_pid=$! 41 | 42 | wait_workers() 43 | { 44 | while ! pgrep -f 'nginx: worker process' >/dev/null 2>&1; do 45 | echo "waiting for nginx workers ..." 46 | sleep 2 47 | done 48 | } 49 | 50 | wait_workers 51 | 52 | test -n "${ENV_CONTROLLER_API_KEY}" && \ 53 | api_key=${ENV_CONTROLLER_API_KEY} 54 | 55 | # if instance_name is defined in the env vars, use it 56 | test -n "${ENV_CONTROLLER_INSTANCE_NAME}" && \ 57 | instance_name=${ENV_CONTROLLER_INSTANCE_NAME} 58 | 59 | test -n "${ENV_CONTROLLER_API_URL}" && \ 60 | controller_api_url=${ENV_CONTROLLER_API_URL} 61 | 62 | test -n "${ENV_CONTROLLER_LOCATION}" && \ 63 | location=${ENV_CONTROLLER_LOCATION} 64 | 65 | test -n "${ENV_CONTROLLER_INSTANCE_GROUP}" && \ 66 | instance_group=${ENV_CONTROLLER_INSTANCE_GROUP} 67 | 68 | if [ -n "${api_key}" -o -n "${instance_name}" -o -n "${controller_api_url}" -o -n "${location}" -o -n "${instance_group}" ]; then 69 | echo "updating ${agent_conf_file} ..." 70 | 71 | if [ ! -f "${agent_conf_file}" ]; then 72 | test -f "${agent_conf_file}.default" && \ 73 | cp -p "${agent_conf_file}.default" "${agent_conf_file}" || \ 74 | { echo "no ${agent_conf_file}.default found! exiting."; exit 1; } 75 | fi 76 | 77 | test -n "${api_key}" && \ 78 | echo " ---> using api_key = ${api_key}" && \ 79 | sh -c "sed -i.old -e 's/api_key.*$/api_key = $api_key/' \ 80 | ${agent_conf_file}" 81 | 82 | test -n "${controller_api_url}" && \ 83 | echo " ---> using controller api url = ${controller_api_url}" && \ 84 | sh -c "sed -i.old -e 's@^api_url.*@api_url = $controller_api_url@' \ 85 | ${agent_conf_file}" 86 | 87 | test -n "${instance_name}" && \ 88 | echo " ---> using instance_name = ${instance_name}" && \ 89 | sh -c "sed -i.old -e 's/instance_name.*$/instance_name = $instance_name/' \ 90 | ${agent_conf_file}" 91 | 92 | test -n "${location}" && \ 93 | echo " ---> using location = ${location}" && \ 94 | sh -c "sed -i.old -e 's/location_name.*$/location_name = $location/' \ 95 | ${agent_conf_file}" 96 | 97 | test -n "${instance_group}" && \ 98 | echo " ---> using instance group = ${instance_group}" && \ 99 | sh -c "sed -i.old -e 's/instance_group.*$/instance_group = $instance_group/' \ 100 | ${agent_conf_file}" 101 | 102 | if ! grep -Fq "security = " ${agent_conf_file}; then 103 | sh -c "sed -i.old -e 's/\[extensions\]/&\nsecurity = True/g' \ 104 | ${agent_conf_file}" 105 | fi 106 | 107 | test -f "${agent_conf_file}" && \ 108 | chmod 644 ${agent_conf_file} && \ 109 | chown nginx ${agent_conf_file} > /dev/null 2>&1 110 | 111 | test -f "${nginx_status_conf}" && \ 112 | chmod 644 ${nginx_status_conf} && \ 113 | chown nginx ${nginx_status_conf} > /dev/null 2>&1 114 | fi 115 | 116 | if ! grep '^api_key.*=[ ]*[[:alnum:]].*' ${agent_conf_file} > /dev/null 2>&1; then 117 | echo "no api_key found in ${agent_conf_file}! exiting." 118 | exit 1 119 | fi 120 | 121 | echo "starting controller-agent ..." 122 | /usr/bin/nginx-controller-agent > /dev/null 2>&1 < /dev/null & 123 | 124 | agent_pid=$! 125 | 126 | if [ $? != 0 ]; then 127 | echo "couldn't start the agent, please check ${agent_log_file}" 128 | exit 1 129 | fi 130 | 131 | wait_term() 132 | { 133 | wait ${agent_pid} 134 | trap - TERM 135 | kill -QUIT "${nginx_pid}" 2>/dev/null 136 | echo "waiting for nginx to stop..." 137 | wait ${nginx_pid} 138 | } 139 | 140 | wait_term 141 | 142 | echo "controller-agent process has stopped, exiting." 143 | -------------------------------------------------------------------------------- /ubuntu/nap/nginx-plus-api.conf: -------------------------------------------------------------------------------- 1 | # This sample NGINX Plus configuration enables the NGINX Plus API, for live 2 | # activity monitoring and the built-in dashboard, dynamic configuration of 3 | # upstream groups, and key-value stores. Keep in mind that any features 4 | # added to the API in future NGINX Plus releases will be enabled 5 | # automatically by this file. 6 | # Created in May 2018 by NGINX, Inc. for NGINX Plus R14 and later. 7 | 8 | # Documentation: 9 | # https://docs.nginx.com/nginx/admin-guide/monitoring/live-activity-monitoring/ 10 | # https://www.nginx.com/blog/live-activity-monitoring-nginx-plus-3-simple-steps 11 | 12 | # To conform with the conventional configuration scheme, place this file in 13 | # the /etc/nginx/conf.d directory and add an 'include' directive that 14 | # references it in the main configuration file, /etc/nginx/nginx.conf, 15 | # either by name or with a wildcard expression. Then validate and reload 16 | # the configuration, for example with this command: 17 | # 18 | # nginx -t && nginx -s reload 19 | 20 | # Note that additional directives are required in other parts of the 21 | # configuration: 22 | # 23 | # For metrics to be gathered for an HTTP or TCP/UDP virtual server, you must 24 | # include the 'status_zone' directive in its 'server' block. See: 25 | # http://nginx.org/r/status_zone 26 | # 27 | # Similarly, for metrics to be gathered for an upstream server group, you 28 | # must include the 'zone' directive in the 'upstream' block. See: 29 | # http://nginx.org/r/zone 30 | # 31 | # For more information and instructions, see: 32 | # https://docs.nginx.com/nginx/admin-guide/monitoring/live-activity-monitoring#status_data 33 | 34 | # We strongly recommend that you restrict access to the NGINX Plus API so 35 | # that only authorized users can view metrics and configuration, change 36 | # configuration, or both. Here are a few options: 37 | # 38 | # (1) Configure your firewall to limit access to port 8080. 39 | # 40 | # (2) Use SSL/TLS client certificates. See: 41 | # https://docs.nginx.com/nginx/admin-guide/security-controls/terminating-ssl-http/ 42 | # 43 | # (3) Enable HTTP Basic authentication (RFC 7617) by uncommenting the 44 | # 'auth_basic*' directives in the 'server' block below. You can add users 45 | # with an htpasswd generator, which is readily available, or reuse an 46 | # existing htpasswd file (from an Apache HTTP Server, for example). See: 47 | # http://nginx.org/en/docs/http/ngx_http_auth_basic_module.html 48 | # 49 | # (4) Enable access from a defined network and disable it from all others, 50 | # by uncommenting the 'allow' and 'deny' directives in the 'server' block 51 | # below and specifying the appropriate network ranges. See: 52 | # http://nginx.org/en/docs/http/ngx_http_access_module.html 53 | # 54 | # You can create further restrictions on write operations, to distinguish 55 | # between users with read permission and those who can change configuration. 56 | # Uncomment the sample 'limit_except' directive in the 'location api' 57 | # block below. In addition to the HTTP Basic authentication shown, other 58 | # authentication schemes are supported. See: 59 | # http://nginx.org/r/limit_except 60 | server { 61 | # Conventional port for the NGINX Plus API is 8080 62 | listen 8080; 63 | 64 | # Uncomment to use HTTP Basic authentication; see (3) above 65 | #auth_basic "NGINX Plus API"; 66 | #auth_basic_user_file /etc/nginx/users; 67 | 68 | # Uncomment to use permissions based on IP address; see (4) above 69 | #allow 10.0.0.0/8; 70 | #deny all; 71 | 72 | # Conventional location for accessing the NGINX Plus API 73 | app_protect_enable on; 74 | app_protect_security_log_enable on; 75 | app_protect_security_log "/etc/nginx/custom_log_format.json" stderr; 76 | location /api/ { 77 | # Enable in read-write mode 78 | api write=on; 79 | # Uncomment to further restrict write permissions; see note above 80 | #limit_except GET { 81 | #auth_basic "NGINX Plus API"; 82 | #auth_basic_user_file /etc/nginx/admins; 83 | #} 84 | } 85 | 86 | # Conventional location of the NGINX Plus dashboard 87 | location = /dashboard.html { 88 | root /usr/share/nginx/html; 89 | } 90 | 91 | # Redirect requests for "/" to "/dashboard.html" 92 | location / { 93 | return 301 /dashboard.html; 94 | } 95 | 96 | # Redirect requests for pre-R14 dashboard 97 | location /status.html { 98 | return 301 /dashboard.html; 99 | } 100 | } 101 | 102 | # vim: syntax=nginx 103 | -------------------------------------------------------------------------------- /ubuntu/no-nap/Dockerfile: -------------------------------------------------------------------------------- 1 | # This dockerfile also support Ubuntu 16.04 2 | # To use Ubuntu 16.04 replace base image with below one: 3 | #FROM ubuntu:16.04 4 | FROM ubuntu:18.04 5 | 6 | LABEL maintainer="NGINX Controller Engineering" 7 | 8 | # e.g '1234567890' 9 | ARG API_KEY 10 | ENV ENV_CONTROLLER_API_KEY=$API_KEY 11 | 12 | # e.g https:///install/controller-agent 13 | ARG CONTROLLER_URL 14 | ENV ENV_CONTROLLER_URL=$CONTROLLER_URL 15 | 16 | # e.g True or False 17 | ARG STORE_UUID=False 18 | ENV ENV_CONTROLLER_STORE_UUID=$STORE_UUID 19 | 20 | # e.g Instance location already defined in Controller 21 | ARG LOCATION 22 | ENV ENV_CONTROLLER_LOCATION=$LOCATION 23 | 24 | # e.g Instance group already defined in Controller 25 | ARG INSTANCE_GROUP 26 | ENV ENV_CONTROLLER_INSTANCE_GROUP=$INSTANCE_GROUP 27 | 28 | # NGXIN Plus release e.g 25 29 | ARG NGINX_PLUS_VERSION=25 30 | 31 | COPY nginx-plus-api.conf /etc/nginx/conf.d/ 32 | COPY entrypoint.sh / 33 | 34 | # Install NGINX Plus 35 | RUN --mount=type=secret,id=nginx-crt,dst=/etc/ssl/nginx/nginx-repo.crt,mode=0644 \ 36 | --mount=type=secret,id=nginx-key,dst=/etc/ssl/nginx/nginx-repo.key,mode=0644 \ 37 | set -ex \ 38 | && apt-get update \ 39 | && apt-get install --no-install-recommends --no-install-suggests -y \ 40 | curl \ 41 | sudo\ 42 | vim\ 43 | procps\ 44 | gnupg\ 45 | apt-utils\ 46 | apt-transport-https\ 47 | ca-certificates\ 48 | distro-info-data\ 49 | libmpdec2\ 50 | lsb-release \ 51 | binutils \ 52 | net-tools \ 53 | && apt-get upgrade -y \ 54 | && \ 55 | NGINX_GPGKEY=573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62; \ 56 | found=''; \ 57 | for server in \ 58 | ha.pool.sks-keyservers.net \ 59 | hkp://keyserver.ubuntu.com:80 \ 60 | hkp://p80.pool.sks-keyservers.net:80 \ 61 | pgp.mit.edu \ 62 | ; do \ 63 | echo "Fetching GPG key $NGINX_GPGKEY from $server"; \ 64 | apt-key adv --keyserver "$server" --keyserver-options timeout=10 --recv-keys "$NGINX_GPGKEY" && found=yes && break; \ 65 | done; \ 66 | test -z "$found" && echo >&2 "error: failed to fetch GPG key $NGINX_GPGKEY" && exit 1; \ 67 | echo "Acquire::https::pkgs.nginx.com::Verify-Peer \"true\";" >> /etc/apt/apt.conf.d/90pkgs-nginx \ 68 | && echo "Acquire::https::pkgs.nginx.com::Verify-Host \"true\";" >> /etc/apt/apt.conf.d/90pkgs-nginx \ 69 | && echo "Acquire::https::pkgs.nginx.com::SslCert \"/etc/ssl/nginx/nginx-repo.crt\";" >> /etc/apt/apt.conf.d/90pkgs-nginx \ 70 | && echo "Acquire::https::pkgs.nginx.com::SslKey \"/etc/ssl/nginx/nginx-repo.key\";" >> /etc/apt/apt.conf.d/90pkgs-nginx \ 71 | && printf "deb https://pkgs.nginx.com/plus/ubuntu $(lsb_release -cs) nginx-plus\n" > /etc/apt/sources.list.d/nginx-plus.list \ 72 | # NGINX Javascript module needed for APIM 73 | && apt-get update && apt-get install -y nginx-plus=${NGINX_PLUS_VERSION}* nginx-plus-module-njs=${NGINX_PLUS_VERSION}* \ 74 | && rm -rf /var/lib/apt/lists/* \ 75 | # Install Controller Agent 76 | && curl -k -sS -L ${CONTROLLER_URL} > install.sh \ 77 | && sed -i 's/^assume_yes=""/assume_yes="-y"/' install.sh \ 78 | && sed -i 's,-n "${NGINX_GPGKEY}",true,' install.sh \ 79 | && sh ./install.sh -y \ 80 | # cleanup sensitive nginx-plus data 81 | && rm /etc/apt/sources.list.d/nginx-plus.list \ 82 | && rm /etc/apt/apt.conf.d/90pkgs-nginx \ 83 | && apt-key del "$NGINX_GPGKEY" 84 | 85 | # Forward request logs to Docker log collector 86 | RUN ln -sf /proc/1/fd/1 /var/log/nginx-controller/agent.log \ 87 | && ln -sf /proc/1/fd/2 /var/log/nginx/error.log 88 | 89 | EXPOSE 80 90 | 91 | STOPSIGNAL SIGTERM 92 | 93 | ENTRYPOINT ["sh", "/entrypoint.sh"] 94 | -------------------------------------------------------------------------------- /ubuntu/no-nap/entrypoint.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 3 | # This script launches nginx and the NGINX Controller Agent. 4 | # 5 | 6 | # Variables 7 | agent_conf_file="/etc/controller-agent/agent.conf" 8 | agent_log_file="/var/log/nginx-controller/agent.log" 9 | nginx_status_conf="/etc/nginx/conf.d/stub_status.conf" 10 | api_key="" 11 | instance_name="$(hostname -f)" 12 | controller_api_url="" 13 | location="" 14 | 15 | handle_term() 16 | { 17 | echo "received TERM signal" 18 | echo "stopping controller-agent ..." 19 | kill -TERM "${agent_pid}" 2>/dev/null 20 | echo "stopping nginx ..." 21 | kill -TERM "${nginx_pid}" 2>/dev/null 22 | } 23 | 24 | trap 'handle_term' TERM 25 | 26 | # Launch nginx 27 | echo "starting nginx ..." 28 | nginx -g "daemon off;" & 29 | 30 | nginx_pid=$! 31 | 32 | wait_workers() 33 | { 34 | while ! pgrep -f 'nginx: worker process' >/dev/null 2>&1; do 35 | echo "waiting for nginx workers ..." 36 | sleep 2 37 | done 38 | } 39 | 40 | wait_workers 41 | 42 | test -n "${ENV_CONTROLLER_API_KEY}" && \ 43 | api_key=${ENV_CONTROLLER_API_KEY} 44 | 45 | # if instance_name is defined in the env vars, use it 46 | test -n "${ENV_CONTROLLER_INSTANCE_NAME}" && \ 47 | instance_name=${ENV_CONTROLLER_INSTANCE_NAME} 48 | 49 | test -n "${ENV_CONTROLLER_API_URL}" && \ 50 | controller_api_url=${ENV_CONTROLLER_API_URL} 51 | 52 | test -n "${ENV_CONTROLLER_LOCATION}" && \ 53 | location=${ENV_CONTROLLER_LOCATION} 54 | 55 | test -n "${ENV_CONTROLLER_INSTANCE_GROUP}" && \ 56 | instance_group=${ENV_CONTROLLER_INSTANCE_GROUP} 57 | 58 | if [ -n "${api_key}" -o -n "${instance_name}" -o -n "${controller_api_url}" -o -n "${location}" -o -n "${instance_group}" ]; then 59 | echo "updating ${agent_conf_file} ..." 60 | 61 | if [ ! -f "${agent_conf_file}" ]; then 62 | test -f "${agent_conf_file}.default" && \ 63 | cp -p "${agent_conf_file}.default" "${agent_conf_file}" || \ 64 | { echo "no ${agent_conf_file}.default found! exiting."; exit 1; } 65 | fi 66 | 67 | test -n "${api_key}" && \ 68 | echo " ---> using api_key = ${api_key}" && \ 69 | sh -c "sed -i.old -e 's/api_key.*$/api_key = $api_key/' \ 70 | ${agent_conf_file}" 71 | 72 | test -n "${controller_api_url}" && \ 73 | echo " ---> using controller api url = ${controller_api_url}" && \ 74 | sh -c "sed -i.old -e 's@^api_url.*@api_url = $controller_api_url@' \ 75 | ${agent_conf_file}" 76 | 77 | test -n "${instance_name}" && \ 78 | echo " ---> using instance_name = ${instance_name}" && \ 79 | sh -c "sed -i.old -e 's/instance_name.*$/instance_name = $instance_name/' \ 80 | ${agent_conf_file}" 81 | 82 | test -n "${location}" && \ 83 | echo " ---> using location = ${location}" && \ 84 | sh -c "sed -i.old -e 's/location_name.*$/location_name = $location/' \ 85 | ${agent_conf_file}" 86 | 87 | test -n "${instance_group}" && \ 88 | echo " ---> using instance group = ${instance_group}" && \ 89 | sh -c "sed -i.old -e 's/instance_group.*$/instance_group = $instance_group/' \ 90 | ${agent_conf_file}" 91 | 92 | test -f "${agent_conf_file}" && \ 93 | chmod 644 ${agent_conf_file} && \ 94 | chown nginx ${agent_conf_file} > /dev/null 2>&1 95 | 96 | test -f "${nginx_status_conf}" && \ 97 | chmod 644 ${nginx_status_conf} && \ 98 | chown nginx ${nginx_status_conf} > /dev/null 2>&1 99 | fi 100 | 101 | if ! grep '^api_key.*=[ ]*[[:alnum:]].*' ${agent_conf_file} > /dev/null 2>&1; then 102 | echo "no api_key found in ${agent_conf_file}! exiting." 103 | exit 1 104 | fi 105 | 106 | echo "starting controller-agent ..." 107 | /usr/bin/nginx-controller-agent > /dev/null 2>&1 < /dev/null & 108 | 109 | agent_pid=$! 110 | 111 | if [ $? != 0 ]; then 112 | echo "couldn't start the agent, please check ${agent_log_file}" 113 | exit 1 114 | fi 115 | 116 | wait_term() 117 | { 118 | wait ${agent_pid} 119 | trap - TERM 120 | kill -QUIT "${nginx_pid}" 2>/dev/null 121 | echo "waiting for nginx to stop..." 122 | wait ${nginx_pid} 123 | } 124 | 125 | wait_term 126 | 127 | echo "controller-agent process has stopped, exiting." 128 | -------------------------------------------------------------------------------- /ubuntu/no-nap/nginx-plus-api.conf: -------------------------------------------------------------------------------- 1 | # This sample NGINX Plus configuration enables the NGINX Plus API, for live 2 | # activity monitoring and the built-in dashboard, dynamic configuration of 3 | # upstream groups, and key-value stores. Keep in mind that any features 4 | # added to the API in future NGINX Plus releases will be enabled 5 | # automatically by this file. 6 | # Created in May 2018 by NGINX, Inc. for NGINX Plus R14 and later. 7 | 8 | # Documentation: 9 | # https://docs.nginx.com/nginx/admin-guide/monitoring/live-activity-monitoring/ 10 | # https://www.nginx.com/blog/live-activity-monitoring-nginx-plus-3-simple-steps 11 | 12 | # To conform with the conventional configuration scheme, place this file in 13 | # the /etc/nginx/conf.d directory and add an 'include' directive that 14 | # references it in the main configuration file, /etc/nginx/nginx.conf, 15 | # either by name or with a wildcard expression. Then validate and reload 16 | # the configuration, for example with this command: 17 | # 18 | # nginx -t && nginx -s reload 19 | 20 | # Note that additional directives are required in other parts of the 21 | # configuration: 22 | # 23 | # For metrics to be gathered for an HTTP or TCP/UDP virtual server, you must 24 | # include the 'status_zone' directive in its 'server' block. See: 25 | # http://nginx.org/r/status_zone 26 | # 27 | # Similarly, for metrics to be gathered for an upstream server group, you 28 | # must include the 'zone' directive in the 'upstream' block. See: 29 | # http://nginx.org/r/zone 30 | # 31 | # For more information and instructions, see: 32 | # https://docs.nginx.com/nginx/admin-guide/monitoring/live-activity-monitoring#status_data 33 | 34 | # We strongly recommend that you restrict access to the NGINX Plus API so 35 | # that only authorized users can view metrics and configuration, change 36 | # configuration, or both. Here are a few options: 37 | # 38 | # (1) Configure your firewall to limit access to port 8080. 39 | # 40 | # (2) Use SSL/TLS client certificates. See: 41 | # https://docs.nginx.com/nginx/admin-guide/security-controls/terminating-ssl-http/ 42 | # 43 | # (3) Enable HTTP Basic authentication (RFC 7617) by uncommenting the 44 | # 'auth_basic*' directives in the 'server' block below. You can add users 45 | # with an htpasswd generator, which is readily available, or reuse an 46 | # existing htpasswd file (from an Apache HTTP Server, for example). See: 47 | # http://nginx.org/en/docs/http/ngx_http_auth_basic_module.html 48 | # 49 | # (4) Enable access from a defined network and disable it from all others, 50 | # by uncommenting the 'allow' and 'deny' directives in the 'server' block 51 | # below and specifying the appropriate network ranges. See: 52 | # http://nginx.org/en/docs/http/ngx_http_access_module.html 53 | # 54 | # You can create further restrictions on write operations, to distinguish 55 | # between users with read permission and those who can change configuration. 56 | # Uncomment the sample 'limit_except' directive in the 'location api' 57 | # block below. In addition to the HTTP Basic authentication shown, other 58 | # authentication schemes are supported. See: 59 | # http://nginx.org/r/limit_except 60 | 61 | server { 62 | # Conventional port for the NGINX Plus API is 8080 63 | listen 8080; 64 | 65 | # Uncomment to use HTTP Basic authentication; see (3) above 66 | #auth_basic "NGINX Plus API"; 67 | #auth_basic_user_file /etc/nginx/users; 68 | 69 | # Uncomment to use permissions based on IP address; see (4) above 70 | #allow 10.0.0.0/8; 71 | #deny all; 72 | 73 | # Conventional location for accessing the NGINX Plus API 74 | location /api/ { 75 | # Enable in read-write mode 76 | api write=on; 77 | 78 | # Uncomment to further restrict write permissions; see note above 79 | #limit_except GET { 80 | #auth_basic "NGINX Plus API"; 81 | #auth_basic_user_file /etc/nginx/admins; 82 | #} 83 | } 84 | 85 | # Conventional location of the NGINX Plus dashboard 86 | location = /dashboard.html { 87 | root /usr/share/nginx/html; 88 | } 89 | 90 | # Redirect requests for "/" to "/dashboard.html" 91 | location / { 92 | return 301 /dashboard.html; 93 | } 94 | 95 | # Redirect requests for pre-R14 dashboard 96 | location /status.html { 97 | return 301 /dashboard.html; 98 | } 99 | } 100 | 101 | # vim: syntax=nginx -------------------------------------------------------------------------------- /unprivileged/examples/ubuntu-nap/Dockerfile: -------------------------------------------------------------------------------- 1 | ARG STORE_UUID=False 2 | # This dockerfile also support Ubuntu 16.04 3 | # To use Ubuntu 16.04 replace base image with below one: 4 | #FROM ubuntu:16.04 as nginx-installer 5 | FROM ubuntu:18.04 as nginx-installer 6 | 7 | LABEL maintainer="NGINX Controller Engineering" 8 | 9 | # NGXIN Plus release e.g 24 10 | ARG NGINX_PLUS_VERSION=24 11 | 12 | ARG EXPOSE_PORT=8080 13 | ENV NGINX_EXPOSE_PORT=$EXPOSE_PORT 14 | 15 | ARG NON_ROOT_USER=nginx 16 | ENV CONTROLLER_USER=$NON_ROOT_USER 17 | 18 | ARG NON_ROOT_GROUP=nginx 19 | ENV CONTROLLER_GROUP=$NON_ROOT_GROUP 20 | 21 | ARG NAP_SYSLOG_PORT=5114 22 | ENV LISTENERS_NAP_SYSLOG_PORT=$NAP_SYSLOG_PORT 23 | 24 | COPY nginx-plus-api.conf /etc/nginx/conf.d/ 25 | COPY entrypoint.sh / 26 | 27 | ENV NGINX_GPGKEY=573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62 28 | 29 | # Install NGINX Plus 30 | RUN --mount=type=secret,id=nginx-crt,dst=/etc/ssl/nginx/nginx-repo.crt,mode=0644 \ 31 | --mount=type=secret,id=nginx-key,dst=/etc/ssl/nginx/nginx-repo.key,mode=0644 \ 32 | set -ex \ 33 | && apt-get update \ 34 | && apt-get install --no-install-recommends --no-install-suggests -y \ 35 | curl \ 36 | sudo\ 37 | vim\ 38 | procps\ 39 | gnupg\ 40 | apt-utils\ 41 | apt-transport-https\ 42 | ca-certificates\ 43 | distro-info-data\ 44 | libmpdec2\ 45 | lsb-release \ 46 | binutils \ 47 | net-tools \ 48 | && apt-get upgrade -y \ 49 | && \ 50 | found=''; \ 51 | for server in \ 52 | ha.pool.sks-keyservers.net \ 53 | hkp://keyserver.ubuntu.com:80 \ 54 | hkp://p80.pool.sks-keyservers.net:80 \ 55 | pgp.mit.edu \ 56 | ; do \ 57 | echo "Fetching GPG key $NGINX_GPGKEY from $server"; \ 58 | apt-key adv --keyserver "$server" --keyserver-options timeout=10 --recv-keys "$NGINX_GPGKEY" && found=yes && break; \ 59 | done; \ 60 | test -z "$found" && echo >&2 "error: failed to fetch GPG key $NGINX_GPGKEY" && exit 1; \ 61 | echo "Acquire::https::plus-pkgs.nginx.com::Verify-Peer \"true\";" >> /etc/apt/apt.conf.d/90nginx \ 62 | && echo "Acquire::https::plus-pkgs.nginx.com::Verify-Host \"true\";" >> /etc/apt/apt.conf.d/90nginx \ 63 | && echo "Acquire::https::plus-pkgs.nginx.com::SslCert \"/etc/ssl/nginx/nginx-repo.crt\";" >> /etc/apt/apt.conf.d/90nginx \ 64 | && echo "Acquire::https::plus-pkgs.nginx.com::SslKey \"/etc/ssl/nginx/nginx-repo.key\";" >> /etc/apt/apt.conf.d/90nginx \ 65 | && printf "deb https://plus-pkgs.nginx.com/ubuntu $(lsb_release -cs) nginx-plus\n" > /etc/apt/sources.list.d/nginx-plus.list \ 66 | # NGINX Javascript module needed for APIM 67 | && apt-get update && apt-get install -y nginx-plus=${NGINX_PLUS_VERSION}* nginx-plus-module-njs=${NGINX_PLUS_VERSION}* 68 | 69 | FROM nginx-installer as agent-installer 70 | # Install Controller Agent 71 | ARG CONTROLLER_URL 72 | ARG API_KEY 73 | ENV ENV_CONTROLLER_API_KEY=$API_KEY 74 | ARG STORE_UUID 75 | RUN curl -k -sS -L ${CONTROLLER_URL} > install.sh \ 76 | && sed -i 's/^assume_yes=""/assume_yes="-y"/' install.sh \ 77 | && sed -i 's,-n "${NGINX_GPGKEY}",true,' install.sh \ 78 | && sh ./install.sh -y 79 | 80 | 81 | FROM agent-installer as nap-installer 82 | # Install nginx-app-protect 83 | ARG NGINX_PLUS_VERSION=24 84 | RUN --mount=type=secret,id=nginx-crt,dst=/etc/ssl/nginx/nginx-repo.crt,mode=0644 \ 85 | --mount=type=secret,id=nginx-key,dst=/etc/ssl/nginx/nginx-repo.key,mode=0644 \ 86 | if [ "$NGINX_PLUS_VERSION" -lt 23 ] ; then \ 87 | echo "NGINX Plus version ${NGINX_PLUS_VERSION} is not supported by NAP" \ 88 | && exit 1 ; \ 89 | else \ 90 | apt-get install -y app-protect ; \ 91 | fi 92 | RUN sed -i "6 a load_module modules/ngx_http_app_protect_module.so;" /etc/nginx/nginx.conf 93 | 94 | 95 | FROM nap-installer as cleaner 96 | # cleanup sensitive nginx-plus data 97 | RUN rm /etc/apt/sources.list.d/nginx-plus.list \ 98 | && rm /etc/apt/apt.conf.d/90nginx \ 99 | && apt-key del $NGINX_GPGKEY \ 100 | && rm -rf /var/lib/apt/lists/* 101 | 102 | 103 | FROM cleaner as runner 104 | ## e.g '1234567890' 105 | ARG API_KEY 106 | ENV ENV_CONTROLLER_API_KEY=$API_KEY 107 | 108 | # e.g https:///install/controller-agent 109 | ARG CONTROLLER_URL 110 | ENV ENV_CONTROLLER_URL=$CONTROLLER_URL 111 | 112 | # e.g True or False 113 | ARG STORE_UUID 114 | ENV ENV_CONTROLLER_STORE_UUID=$STORE_UUID 115 | 116 | # e.g Instance location already defined in Controller 117 | ARG LOCATION 118 | ENV ENV_CONTROLLER_LOCATION=$LOCATION 119 | 120 | # e.g Instance group already defined in Controller 121 | ARG INSTANCE_GROUP 122 | ENV ENV_CONTROLLER_INSTANCE_GROUP=$INSTANCE_GROUP 123 | 124 | # Forward request logs to Docker log collector 125 | RUN ln -sf /proc/1/fd/1 /var/log/nginx-controller/agent.log \ 126 | && ln -sf /proc/1/fd/2 /var/log/nginx/error.log 127 | 128 | COPY custom_log_format.json /etc/nginx/ 129 | 130 | # Update ownership for the necessary filesystem objects for running under non-root user 131 | RUN chown -R $CONTROLLER_USER:$CONTROLLER_GROUP /var/run/ \ 132 | && chown -R $CONTROLLER_USER:$CONTROLLER_GROUP /var/log/nginx/ \ 133 | && chown -R $CONTROLLER_USER:$CONTROLLER_GROUP /var/cache/nginx/ \ 134 | && chown -R $CONTROLLER_USER:$CONTROLLER_GROUP /etc/controller-agent/ \ 135 | && chown -R $CONTROLLER_USER:$CONTROLLER_GROUP /etc/nginx/ \ 136 | && chown -R $CONTROLLER_USER:$CONTROLLER_GROUP /var/log/app_protect/ || true \ 137 | && chown -R $CONTROLLER_USER:$CONTROLLER_GROUP /opt/app_protect/ || true \ 138 | && sed -i "s,listen 80 default_server;,listen $NGINX_EXPOSE_PORT default_server;," /etc/nginx/conf.d/default.conf \ 139 | && sed -i '/user nginx;/d' /etc/nginx/nginx.conf 140 | 141 | USER $CONTROLLER_USER 142 | 143 | EXPOSE $NGINX_EXPOSE_PORT 144 | 145 | STOPSIGNAL SIGTERM 146 | 147 | ENTRYPOINT ["sh", "/entrypoint.sh"] 148 | -------------------------------------------------------------------------------- /unprivileged/examples/ubuntu-nap/custom_log_format.json: -------------------------------------------------------------------------------- 1 | { 2 | "filter": { 3 | "request_type": "all" 4 | }, 5 | "content": { 6 | "format": "splunk", 7 | "max_request_size": "any", 8 | "max_message_size": "10k" 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /unprivileged/examples/ubuntu-nap/entrypoint.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 3 | # This script launches nginx and the NGINX Controller Agent. 4 | # 5 | 6 | # Variables 7 | agent_conf_file="/etc/controller-agent/agent.conf" 8 | agent_log_file="/var/log/nginx-controller/agent.log" 9 | nginx_status_conf="/etc/nginx/conf.d/stub_status.conf" 10 | 11 | api_key="" 12 | instance_name="$(hostname -f)" 13 | controller_api_url="" 14 | location="" 15 | 16 | handle_term() 17 | { 18 | echo "received TERM signal" 19 | echo "stopping bd-socket-plugin" 20 | kill -TERM "${bd_socket_pid}" 2>/dev/null 21 | echo "stopping bd-agent" 22 | kill -TERM "${bd_agent_pid}" 2>/dev/null 23 | echo "stopping controller-agent ..." 24 | kill -TERM "${agent_pid}" 2>/dev/null 25 | echo "stopping nginx ..." 26 | kill -TERM "${nginx_pid}" 2>/dev/null 27 | } 28 | 29 | trap 'handle_term' TERM 30 | 31 | # Launch NAP 32 | /bin/bash -c '/opt/app_protect/bin/bd_agent &' nginx 33 | bd_agent_pid=$(pgrep bd_agent) 34 | /bin/bash -c "/usr/share/ts/bin/bd-socket-plugin tmm_count 4 proc_cpuinfo_cpu_mhz 2000000 total_xml_memory 307200000 total_umu_max_size 3129344 sys_max_account_id 1024 no_static_config 2>&1 > /var/log/app_protect/bd-socket-plugin.log &" nginx 35 | bd_socket_pid=$(pgrep bd-socket) 36 | # Launch nginx 37 | echo "starting nginx ..." 38 | nginx -g "daemon off;" & 39 | 40 | nginx_pid=$! 41 | 42 | wait_workers() 43 | { 44 | while ! pgrep -f 'nginx: worker process' >/dev/null 2>&1; do 45 | echo "waiting for nginx workers ..." 46 | sleep 2 47 | done 48 | } 49 | 50 | wait_workers 51 | 52 | test -n "${ENV_CONTROLLER_API_KEY}" && \ 53 | api_key=${ENV_CONTROLLER_API_KEY} 54 | 55 | # if instance_name is defined in the env vars, use it 56 | test -n "${ENV_CONTROLLER_INSTANCE_NAME}" && \ 57 | instance_name=${ENV_CONTROLLER_INSTANCE_NAME} 58 | 59 | test -n "${ENV_CONTROLLER_API_URL}" && \ 60 | controller_api_url=${ENV_CONTROLLER_API_URL} 61 | 62 | test -n "${ENV_CONTROLLER_LOCATION}" && \ 63 | location=${ENV_CONTROLLER_LOCATION} 64 | 65 | test -n "${ENV_CONTROLLER_INSTANCE_GROUP}" && \ 66 | instance_group=${ENV_CONTROLLER_INSTANCE_GROUP} 67 | 68 | if [ -n "${api_key}" -o -n "${instance_name}" -o -n "${controller_api_url}" -o -n "${location}" -o -n "${instance_group}" ]; then 69 | echo "updating ${agent_conf_file} ..." 70 | 71 | if [ ! -f "${agent_conf_file}" ]; then 72 | test -f "${agent_conf_file}.default" && \ 73 | cp -p "${agent_conf_file}.default" "${agent_conf_file}" || \ 74 | { echo "no ${agent_conf_file}.default found! exiting."; exit 1; } 75 | fi 76 | 77 | test -n "${api_key}" && \ 78 | echo " ---> using api_key = ${api_key}" && \ 79 | sh -c "sed -i.old -e 's/api_key.*$/api_key = $api_key/' \ 80 | ${agent_conf_file}" 81 | 82 | test -n "${controller_api_url}" && \ 83 | echo " ---> using controller api url = ${controller_api_url}" && \ 84 | sh -c "sed -i.old -e 's@^api_url.*@api_url = $controller_api_url@' \ 85 | ${agent_conf_file}" 86 | 87 | test -n "${instance_name}" && \ 88 | echo " ---> using instance_name = ${instance_name}" && \ 89 | sh -c "sed -i.old -e 's/instance_name.*$/instance_name = $instance_name/' \ 90 | ${agent_conf_file}" 91 | 92 | test -n "${location}" && \ 93 | echo " ---> using location = ${location}" && \ 94 | sh -c "sed -i.old -e 's/location_name.*$/location_name = $location/' \ 95 | ${agent_conf_file}" 96 | 97 | test -n "${instance_group}" && \ 98 | echo " ---> using instance group = ${instance_group}" && \ 99 | sh -c "sed -i.old -e 's/instance_group.*$/instance_group = $instance_group/' \ 100 | ${agent_conf_file}" 101 | 102 | if ! grep -Fq "security = " ${agent_conf_file}; then 103 | sh -c "sed -i.old -e 's/\[extensions\]/&\nsecurity = True/g' \ 104 | ${agent_conf_file}" 105 | fi 106 | 107 | test -f "${agent_conf_file}" && \ 108 | chmod 644 ${agent_conf_file} && \ 109 | chown nginx ${agent_conf_file} > /dev/null 2>&1 110 | 111 | test -f "${nginx_status_conf}" && \ 112 | chmod 644 ${nginx_status_conf} && \ 113 | chown nginx ${nginx_status_conf} > /dev/null 2>&1 114 | fi 115 | 116 | if ! grep '^api_key.*=[ ]*[[:alnum:]].*' ${agent_conf_file} > /dev/null 2>&1; then 117 | echo "no api_key found in ${agent_conf_file}! exiting." 118 | exit 1 119 | fi 120 | 121 | echo "starting controller-agent ..." 122 | /usr/bin/nginx-controller-agent > /dev/null 2>&1 < /dev/null & 123 | 124 | agent_pid=$! 125 | 126 | if [ $? != 0 ]; then 127 | echo "couldn't start the agent, please check ${agent_log_file}" 128 | exit 1 129 | fi 130 | 131 | wait_term() 132 | { 133 | wait ${agent_pid} 134 | trap - TERM 135 | kill -QUIT "${nginx_pid}" 2>/dev/null 136 | echo "waiting for nginx to stop..." 137 | wait ${nginx_pid} 138 | } 139 | 140 | wait_term 141 | 142 | echo "controller-agent process has stopped, exiting." 143 | -------------------------------------------------------------------------------- /unprivileged/examples/ubuntu-nap/nginx-plus-api.conf: -------------------------------------------------------------------------------- 1 | # This sample NGINX Plus configuration enables the NGINX Plus API, for live 2 | # activity monitoring and the built-in dashboard, dynamic configuration of 3 | # upstream groups, and key-value stores. Keep in mind that any features 4 | # added to the API in future NGINX Plus releases will be enabled 5 | # automatically by this file. 6 | # Created in May 2018 by NGINX, Inc. for NGINX Plus R14 and later. 7 | 8 | # Documentation: 9 | # https://docs.nginx.com/nginx/admin-guide/monitoring/live-activity-monitoring/ 10 | # https://www.nginx.com/blog/live-activity-monitoring-nginx-plus-3-simple-steps 11 | 12 | # To conform with the conventional configuration scheme, place this file in 13 | # the /etc/nginx/conf.d directory and add an 'include' directive that 14 | # references it in the main configuration file, /etc/nginx/nginx.conf, 15 | # either by name or with a wildcard expression. Then validate and reload 16 | # the configuration, for example with this command: 17 | # 18 | # nginx -t && nginx -s reload 19 | 20 | # Note that additional directives are required in other parts of the 21 | # configuration: 22 | # 23 | # For metrics to be gathered for an HTTP or TCP/UDP virtual server, you must 24 | # include the 'status_zone' directive in its 'server' block. See: 25 | # http://nginx.org/r/status_zone 26 | # 27 | # Similarly, for metrics to be gathered for an upstream server group, you 28 | # must include the 'zone' directive in the 'upstream' block. See: 29 | # http://nginx.org/r/zone 30 | # 31 | # For more information and instructions, see: 32 | # https://docs.nginx.com/nginx/admin-guide/monitoring/live-activity-monitoring#status_data 33 | 34 | # We strongly recommend that you restrict access to the NGINX Plus API so 35 | # that only authorized users can view metrics and configuration, change 36 | # configuration, or both. Here are a few options: 37 | # 38 | # (1) Configure your firewall to limit access to port 8080. 39 | # 40 | # (2) Use SSL/TLS client certificates. See: 41 | # https://docs.nginx.com/nginx/admin-guide/security-controls/terminating-ssl-http/ 42 | # 43 | # (3) Enable HTTP Basic authentication (RFC 7617) by uncommenting the 44 | # 'auth_basic*' directives in the 'server' block below. You can add users 45 | # with an htpasswd generator, which is readily available, or reuse an 46 | # existing htpasswd file (from an Apache HTTP Server, for example). See: 47 | # http://nginx.org/en/docs/http/ngx_http_auth_basic_module.html 48 | # 49 | # (4) Enable access from a defined network and disable it from all others, 50 | # by uncommenting the 'allow' and 'deny' directives in the 'server' block 51 | # below and specifying the appropriate network ranges. See: 52 | # http://nginx.org/en/docs/http/ngx_http_access_module.html 53 | # 54 | # You can create further restrictions on write operations, to distinguish 55 | # between users with read permission and those who can change configuration. 56 | # Uncomment the sample 'limit_except' directive in the 'location api' 57 | # block below. In addition to the HTTP Basic authentication shown, other 58 | # authentication schemes are supported. See: 59 | # http://nginx.org/r/limit_except 60 | server { 61 | # Conventional port for the NGINX Plus API is 8080 62 | listen 8080; 63 | 64 | # Uncomment to use HTTP Basic authentication; see (3) above 65 | #auth_basic "NGINX Plus API"; 66 | #auth_basic_user_file /etc/nginx/users; 67 | 68 | # Uncomment to use permissions based on IP address; see (4) above 69 | #allow 10.0.0.0/8; 70 | #deny all; 71 | 72 | # Conventional location for accessing the NGINX Plus API 73 | app_protect_enable on; 74 | app_protect_security_log_enable on; 75 | app_protect_security_log "/etc/nginx/custom_log_format.json" stderr; 76 | location /api/ { 77 | # Enable in read-write mode 78 | api write=on; 79 | # Uncomment to further restrict write permissions; see note above 80 | #limit_except GET { 81 | #auth_basic "NGINX Plus API"; 82 | #auth_basic_user_file /etc/nginx/admins; 83 | #} 84 | } 85 | 86 | # Conventional location of the NGINX Plus dashboard 87 | location = /dashboard.html { 88 | root /usr/share/nginx/html; 89 | } 90 | 91 | # Redirect requests for "/" to "/dashboard.html" 92 | location / { 93 | return 301 /dashboard.html; 94 | } 95 | 96 | # Redirect requests for pre-R14 dashboard 97 | location /status.html { 98 | return 301 /dashboard.html; 99 | } 100 | } 101 | 102 | # vim: syntax=nginx 103 | -------------------------------------------------------------------------------- /unprivileged/examples/ubuntu-no-nap/Dockerfile: -------------------------------------------------------------------------------- 1 | # This dockerfile also support Ubuntu 16.04 2 | # To use Ubuntu 16.04 replace base image with below one: 3 | #FROM ubuntu:16.04 4 | FROM ubuntu:18.04 5 | 6 | LABEL maintainer="NGINX Controller Engineering" 7 | 8 | # e.g '1234567890' 9 | ARG API_KEY 10 | ENV ENV_CONTROLLER_API_KEY=$API_KEY 11 | 12 | # e.g https:///install/controller-agent 13 | ARG CONTROLLER_URL 14 | ENV ENV_CONTROLLER_URL=$CONTROLLER_URL 15 | 16 | # e.g True or False 17 | ARG STORE_UUID=False 18 | ENV ENV_CONTROLLER_STORE_UUID=$STORE_UUID 19 | 20 | # e.g Instance location already defined in Controller 21 | ARG LOCATION 22 | ENV ENV_CONTROLLER_LOCATION=$LOCATION 23 | 24 | # e.g Instance group already defined in Controller 25 | ARG INSTANCE_GROUP 26 | ENV ENV_CONTROLLER_INSTANCE_GROUP=$INSTANCE_GROUP 27 | 28 | # NGXIN Plus release e.g 24 29 | ARG NGINX_PLUS_VERSION=24 30 | 31 | ARG EXPOSE_PORT=8080 32 | ENV NGINX_EXPOSE_PORT=$EXPOSE_PORT 33 | 34 | ARG NON_ROOT_USER=nginx 35 | ENV CONTROLLER_USER=$NON_ROOT_USER 36 | 37 | ARG NON_ROOT_GROUP=nginx 38 | ENV CONTROLLER_GROUP=$NON_ROOT_GROUP 39 | 40 | ARG NAP_SYSLOG_PORT=5114 41 | ENV LISTENERS_NAP_SYSLOG_PORT=$NAP_SYSLOG_PORT 42 | 43 | COPY nginx-plus-api.conf /etc/nginx/conf.d/ 44 | COPY entrypoint.sh / 45 | 46 | # Install NGINX Plus 47 | RUN --mount=type=secret,id=nginx-crt,dst=/etc/ssl/nginx/nginx-repo.crt,mode=0644 \ 48 | --mount=type=secret,id=nginx-key,dst=/etc/ssl/nginx/nginx-repo.key,mode=0644 \ 49 | set -ex \ 50 | && apt-get update \ 51 | && apt-get install --no-install-recommends --no-install-suggests -y \ 52 | curl \ 53 | sudo\ 54 | vim\ 55 | procps\ 56 | gnupg\ 57 | apt-utils\ 58 | apt-transport-https\ 59 | ca-certificates\ 60 | distro-info-data\ 61 | libmpdec2\ 62 | lsb-release \ 63 | binutils \ 64 | net-tools \ 65 | && apt-get upgrade -y \ 66 | && \ 67 | NGINX_GPGKEY=573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62; \ 68 | found=''; \ 69 | for server in \ 70 | ha.pool.sks-keyservers.net \ 71 | hkp://keyserver.ubuntu.com:80 \ 72 | hkp://p80.pool.sks-keyservers.net:80 \ 73 | pgp.mit.edu \ 74 | ; do \ 75 | echo "Fetching GPG key $NGINX_GPGKEY from $server"; \ 76 | apt-key adv --keyserver "$server" --keyserver-options timeout=10 --recv-keys "$NGINX_GPGKEY" && found=yes && break; \ 77 | done; \ 78 | test -z "$found" && echo >&2 "error: failed to fetch GPG key $NGINX_GPGKEY" && exit 1; \ 79 | echo "Acquire::https::plus-pkgs.nginx.com::Verify-Peer \"true\";" >> /etc/apt/apt.conf.d/90nginx \ 80 | && echo "Acquire::https::plus-pkgs.nginx.com::Verify-Host \"true\";" >> /etc/apt/apt.conf.d/90nginx \ 81 | && echo "Acquire::https::plus-pkgs.nginx.com::SslCert \"/etc/ssl/nginx/nginx-repo.crt\";" >> /etc/apt/apt.conf.d/90nginx \ 82 | && echo "Acquire::https::plus-pkgs.nginx.com::SslKey \"/etc/ssl/nginx/nginx-repo.key\";" >> /etc/apt/apt.conf.d/90nginx \ 83 | && printf "deb https://plus-pkgs.nginx.com/ubuntu $(lsb_release -cs) nginx-plus\n" > /etc/apt/sources.list.d/nginx-plus.list \ 84 | # NGINX Javascript module needed for APIM 85 | && apt-get update && apt-get install -y nginx-plus=${NGINX_PLUS_VERSION}* nginx-plus-module-njs=${NGINX_PLUS_VERSION}* \ 86 | && rm -rf /var/lib/apt/lists/* \ 87 | # Install Controller Agent 88 | && curl -k -sS -L ${CONTROLLER_URL} > install.sh \ 89 | && sed -i 's/^assume_yes=""/assume_yes="-y"/' install.sh \ 90 | && sed -i 's,-n "${NGINX_GPGKEY}",true,' install.sh \ 91 | && sh ./install.sh -y \ 92 | # cleanup sensitive nginx-plus data 93 | && rm /etc/apt/sources.list.d/nginx-plus.list \ 94 | && rm /etc/apt/apt.conf.d/90nginx \ 95 | && apt-key del "$NGINX_GPGKEY" 96 | 97 | # Forward request logs to Docker log collector 98 | RUN ln -sf /proc/1/fd/1 /var/log/nginx-controller/agent.log \ 99 | && ln -sf /proc/1/fd/2 /var/log/nginx/error.log 100 | 101 | # Update ownership for the necessary filesystem objects for running under non-root user 102 | RUN chown -R $CONTROLLER_USER:$CONTROLLER_GROUP /var/run/ \ 103 | && chown -R $CONTROLLER_USER:$CONTROLLER_GROUP /var/log/nginx/ \ 104 | && chown -R $CONTROLLER_USER:$CONTROLLER_GROUP /var/cache/nginx/ \ 105 | && chown -R $CONTROLLER_USER:$CONTROLLER_GROUP /etc/controller-agent/ \ 106 | && chown -R $CONTROLLER_USER:$CONTROLLER_GROUP /etc/nginx/ \ 107 | && chown -R $CONTROLLER_USER:$CONTROLLER_GROUP /var/log/app_protect/ || true \ 108 | && chown -R $CONTROLLER_USER:$CONTROLLER_GROUP /opt/app_protect/ || true \ 109 | && sed -i "s,listen 80 default_server;,listen $NGINX_EXPOSE_PORT default_server;," /etc/nginx/conf.d/default.conf \ 110 | && sed -i '/user nginx;/d' /etc/nginx/nginx.conf 111 | 112 | USER $CONTROLLER_USER 113 | 114 | EXPOSE $NGINX_EXPOSE_PORT 115 | 116 | STOPSIGNAL SIGTERM 117 | 118 | ENTRYPOINT ["sh", "/entrypoint.sh"] 119 | -------------------------------------------------------------------------------- /unprivileged/examples/ubuntu-no-nap/entrypoint.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 3 | # This script launches nginx and the NGINX Controller Agent. 4 | # 5 | 6 | # Variables 7 | agent_conf_file="/etc/controller-agent/agent.conf" 8 | agent_log_file="/var/log/nginx-controller/agent.log" 9 | nginx_status_conf="/etc/nginx/conf.d/stub_status.conf" 10 | api_key="" 11 | instance_name="$(hostname -f)" 12 | controller_api_url="" 13 | location="" 14 | 15 | handle_term() 16 | { 17 | echo "received TERM signal" 18 | echo "stopping controller-agent ..." 19 | kill -TERM "${agent_pid}" 2>/dev/null 20 | echo "stopping nginx ..." 21 | kill -TERM "${nginx_pid}" 2>/dev/null 22 | } 23 | 24 | trap 'handle_term' TERM 25 | 26 | # Launch nginx 27 | echo "starting nginx ..." 28 | nginx -g "daemon off;" & 29 | 30 | nginx_pid=$! 31 | 32 | wait_workers() 33 | { 34 | while ! pgrep -f 'nginx: worker process' >/dev/null 2>&1; do 35 | echo "waiting for nginx workers ..." 36 | sleep 2 37 | done 38 | } 39 | 40 | wait_workers 41 | 42 | test -n "${ENV_CONTROLLER_API_KEY}" && \ 43 | api_key=${ENV_CONTROLLER_API_KEY} 44 | 45 | # if instance_name is defined in the env vars, use it 46 | test -n "${ENV_CONTROLLER_INSTANCE_NAME}" && \ 47 | instance_name=${ENV_CONTROLLER_INSTANCE_NAME} 48 | 49 | test -n "${ENV_CONTROLLER_API_URL}" && \ 50 | controller_api_url=${ENV_CONTROLLER_API_URL} 51 | 52 | test -n "${ENV_CONTROLLER_LOCATION}" && \ 53 | location=${ENV_CONTROLLER_LOCATION} 54 | 55 | test -n "${ENV_CONTROLLER_INSTANCE_GROUP}" && \ 56 | instance_group=${ENV_CONTROLLER_INSTANCE_GROUP} 57 | 58 | if [ -n "${api_key}" -o -n "${instance_name}" -o -n "${controller_api_url}" -o -n "${location}" -o -n "${instance_group}" ]; then 59 | echo "updating ${agent_conf_file} ..." 60 | 61 | if [ ! -f "${agent_conf_file}" ]; then 62 | test -f "${agent_conf_file}.default" && \ 63 | cp -p "${agent_conf_file}.default" "${agent_conf_file}" || \ 64 | { echo "no ${agent_conf_file}.default found! exiting."; exit 1; } 65 | fi 66 | 67 | test -n "${api_key}" && \ 68 | echo " ---> using api_key = ${api_key}" && \ 69 | sh -c "sed -i.old -e 's/api_key.*$/api_key = $api_key/' \ 70 | ${agent_conf_file}" 71 | 72 | test -n "${controller_api_url}" && \ 73 | echo " ---> using controller api url = ${controller_api_url}" && \ 74 | sh -c "sed -i.old -e 's@^api_url.*@api_url = $controller_api_url@' \ 75 | ${agent_conf_file}" 76 | 77 | test -n "${instance_name}" && \ 78 | echo " ---> using instance_name = ${instance_name}" && \ 79 | sh -c "sed -i.old -e 's/instance_name.*$/instance_name = $instance_name/' \ 80 | ${agent_conf_file}" 81 | 82 | test -n "${location}" && \ 83 | echo " ---> using location = ${location}" && \ 84 | sh -c "sed -i.old -e 's/location_name.*$/location_name = $location/' \ 85 | ${agent_conf_file}" 86 | 87 | test -n "${instance_group}" && \ 88 | echo " ---> using instance group = ${instance_group}" && \ 89 | sh -c "sed -i.old -e 's/instance_group.*$/instance_group = $instance_group/' \ 90 | ${agent_conf_file}" 91 | 92 | test -f "${agent_conf_file}" && \ 93 | chmod 644 ${agent_conf_file} && \ 94 | chown nginx ${agent_conf_file} > /dev/null 2>&1 95 | 96 | test -f "${nginx_status_conf}" && \ 97 | chmod 644 ${nginx_status_conf} && \ 98 | chown nginx ${nginx_status_conf} > /dev/null 2>&1 99 | fi 100 | 101 | if ! grep '^api_key.*=[ ]*[[:alnum:]].*' ${agent_conf_file} > /dev/null 2>&1; then 102 | echo "no api_key found in ${agent_conf_file}! exiting." 103 | exit 1 104 | fi 105 | 106 | echo "starting controller-agent ..." 107 | /usr/bin/nginx-controller-agent > /dev/null 2>&1 < /dev/null & 108 | 109 | agent_pid=$! 110 | 111 | if [ $? != 0 ]; then 112 | echo "couldn't start the agent, please check ${agent_log_file}" 113 | exit 1 114 | fi 115 | 116 | wait_term() 117 | { 118 | wait ${agent_pid} 119 | trap - TERM 120 | kill -QUIT "${nginx_pid}" 2>/dev/null 121 | echo "waiting for nginx to stop..." 122 | wait ${nginx_pid} 123 | } 124 | 125 | wait_term 126 | 127 | echo "controller-agent process has stopped, exiting." 128 | -------------------------------------------------------------------------------- /unprivileged/examples/ubuntu-no-nap/nginx-plus-api.conf: -------------------------------------------------------------------------------- 1 | # This sample NGINX Plus configuration enables the NGINX Plus API, for live 2 | # activity monitoring and the built-in dashboard, dynamic configuration of 3 | # upstream groups, and key-value stores. Keep in mind that any features 4 | # added to the API in future NGINX Plus releases will be enabled 5 | # automatically by this file. 6 | # Created in May 2018 by NGINX, Inc. for NGINX Plus R14 and later. 7 | 8 | # Documentation: 9 | # https://docs.nginx.com/nginx/admin-guide/monitoring/live-activity-monitoring/ 10 | # https://www.nginx.com/blog/live-activity-monitoring-nginx-plus-3-simple-steps 11 | 12 | # To conform with the conventional configuration scheme, place this file in 13 | # the /etc/nginx/conf.d directory and add an 'include' directive that 14 | # references it in the main configuration file, /etc/nginx/nginx.conf, 15 | # either by name or with a wildcard expression. Then validate and reload 16 | # the configuration, for example with this command: 17 | # 18 | # nginx -t && nginx -s reload 19 | 20 | # Note that additional directives are required in other parts of the 21 | # configuration: 22 | # 23 | # For metrics to be gathered for an HTTP or TCP/UDP virtual server, you must 24 | # include the 'status_zone' directive in its 'server' block. See: 25 | # http://nginx.org/r/status_zone 26 | # 27 | # Similarly, for metrics to be gathered for an upstream server group, you 28 | # must include the 'zone' directive in the 'upstream' block. See: 29 | # http://nginx.org/r/zone 30 | # 31 | # For more information and instructions, see: 32 | # https://docs.nginx.com/nginx/admin-guide/monitoring/live-activity-monitoring#status_data 33 | 34 | # We strongly recommend that you restrict access to the NGINX Plus API so 35 | # that only authorized users can view metrics and configuration, change 36 | # configuration, or both. Here are a few options: 37 | # 38 | # (1) Configure your firewall to limit access to port 8080. 39 | # 40 | # (2) Use SSL/TLS client certificates. See: 41 | # https://docs.nginx.com/nginx/admin-guide/security-controls/terminating-ssl-http/ 42 | # 43 | # (3) Enable HTTP Basic authentication (RFC 7617) by uncommenting the 44 | # 'auth_basic*' directives in the 'server' block below. You can add users 45 | # with an htpasswd generator, which is readily available, or reuse an 46 | # existing htpasswd file (from an Apache HTTP Server, for example). See: 47 | # http://nginx.org/en/docs/http/ngx_http_auth_basic_module.html 48 | # 49 | # (4) Enable access from a defined network and disable it from all others, 50 | # by uncommenting the 'allow' and 'deny' directives in the 'server' block 51 | # below and specifying the appropriate network ranges. See: 52 | # http://nginx.org/en/docs/http/ngx_http_access_module.html 53 | # 54 | # You can create further restrictions on write operations, to distinguish 55 | # between users with read permission and those who can change configuration. 56 | # Uncomment the sample 'limit_except' directive in the 'location api' 57 | # block below. In addition to the HTTP Basic authentication shown, other 58 | # authentication schemes are supported. See: 59 | # http://nginx.org/r/limit_except 60 | 61 | server { 62 | # Conventional port for the NGINX Plus API is 8080 63 | listen 8080; 64 | 65 | # Uncomment to use HTTP Basic authentication; see (3) above 66 | #auth_basic "NGINX Plus API"; 67 | #auth_basic_user_file /etc/nginx/users; 68 | 69 | # Uncomment to use permissions based on IP address; see (4) above 70 | #allow 10.0.0.0/8; 71 | #deny all; 72 | 73 | # Conventional location for accessing the NGINX Plus API 74 | location /api/ { 75 | # Enable in read-write mode 76 | api write=on; 77 | 78 | # Uncomment to further restrict write permissions; see note above 79 | #limit_except GET { 80 | #auth_basic "NGINX Plus API"; 81 | #auth_basic_user_file /etc/nginx/admins; 82 | #} 83 | } 84 | 85 | # Conventional location of the NGINX Plus dashboard 86 | location = /dashboard.html { 87 | root /usr/share/nginx/html; 88 | } 89 | 90 | # Redirect requests for "/" to "/dashboard.html" 91 | location / { 92 | return 301 /dashboard.html; 93 | } 94 | 95 | # Redirect requests for pre-R14 dashboard 96 | location /status.html { 97 | return 301 /dashboard.html; 98 | } 99 | } 100 | 101 | # vim: syntax=nginx --------------------------------------------------------------------------------