Applying our custom NGINX configuration

Our directive after the system update (RUN rm /etc/nginx/conf.d/default.conf) is one that removes the default web server configuration from the container. You can find out more about the NGINX configuration with the link from our last tip, but for now, it will suffice to say that by default, all the individual site configuration files are stored in /etc/nginx/conf.d and NGINX Docker image comes out of the box with a simple example file called default.conf , which we absolutely do not want to use.

While we could overwrite the mentioned file, we would be stuck with the name default, which isn't very descriptive, so for our configuration, we will delete this one and add ours with a better filename.

Next, we need to make sure that the folder we will be serving files from is available and readable by the web server process. The first command using mkdir -p creates all the relevant directories, but since NGINX doesn't run as the root, we need to know what user the process will be reading the files we want to serve up or otherwise our server will not be able to display anything. We can find what the original configuration has there as the default user by showing the first few lines of the system-wide NGINX configuration included in the image at /etc/nginx/nginx.conf:

$ # Print top 2 lines of main config file in NGINX image
$ docker run --rm
nginx /bin/head -2 /etc/nginx/nginx.conf


user nginx;

Perfect! Well, now that the user that needs to be able to read this directory is nginx , we will change the owner of our target folder with chown nginx:nginx /srv/www/html , but what is going on with that new style of run Docker command we just used when trying to find this out? If you include a command after specifying the image name instead of the CMD directive in the image, Docker will substitute it with this new command. In the preceding command, we are running the /bin/head executable, passing in arguments to tell it that we only want the top two lines from the /etc/nginx/nginx.conf file. Since this command exits as soon as it is done, the container stops and is fully removed because we used the --rm flag.

With the default configuration gone and our directories created, we can now copy our main configuration for NGINX in place with COPY nginx_main_site.conf /etc/nginx/conf.d/. The COPY argument does pretty much the obvious thing of copying a file from the current build directory into the image at a specified location.

Be very careful with how you end the COPY directive argument, as leaving the slash off will put the source into a file at the destination even if the destination is a directory. To ensure that this doesn't happen, always end your target directory paths with a slash.

Adding our main test.txt file that we want hosted is the last part, and it follows along the same lines as the other COPY directive, but we will make sure that we put this one in the folder that our NGINX configuration is referencing. Since we turned on the autoindex flag for this endpoint, there are no additional steps to be taken as the folder itself will be browsable.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.118.157