Home  >  Article  >  Operation and Maintenance  >  Where is the nginx docker configuration file?

Where is the nginx docker configuration file?

PHPz
PHPzOriginal
2023-04-04 09:12:045232browse

Nginx is a very popular web server and reverse proxy server, which is widely used in Internet applications and cloud computing systems. With the popularity of Docker, more and more developers have begun to deploy Nginx servers in Docker containers to more conveniently manage and maintain the deployment and operation of the entire application. However, when configuring configuration files for Nginx containers, many people will encounter a problem: Where are the configuration files of the Nginx Docker container placed?

First of all, it needs to be made clear that Nginx Docker containers are usually built based on the official Nginx image, and the official image already contains an empty configuration file by default /etc/nginx/nginx.conf. If we need to modify the default configuration, we can do it in two ways:

The first way is to mount the Nginx configuration file of the local host to the container through the -v parameter of the docker run command. middle. For example:

docker run \
-d \
-p 80:80 \
-v /path/to/nginx.conf:/etc/nginx/nginx.conf \
nginx

The above command will start a container named nginx and mount the local host’s /path/to/nginx.conf file to The /etc/nginx/nginx.conf file location in the container enables the modification and management of the Nginx configuration file.

If you need to mount the log files and other data of the Nginx container to the local host, you can add other -v parameters. For example:

docker run \
-d \
-p 80:80 \
-v /path/to/nginx.conf:/etc/nginx/nginx.conf \
-v /path/to/logs:/var/log/nginx \
-v /path/to/data:/var/www/html \
nginx

The second way is to customize an Nginx image through Dockerfile and copy the customized configuration file to the container. For example:

First create a file named Dockerfile with the following content:

FROM nginx:latest

COPY nginx.conf /etc/nginx/nginx.conf

Then write your own configuration in the nginx.conf file information, and then execute the following command to build a custom image:

docker build -t my-nginx .

Finally, we can start the custom Nginx container like a normal Nginx container:

docker run -d -p 80:80 my-nginx

The above is in the Nginx Docker container Two common ways to configure files. It should be noted that when using the first method, do not modify the /etc/nginx/nginx.conf configuration file directly in the container, because the container is temporary, and the files in the container will be modified every time it is closed. It will also disappear, doing so will cause all your modifications to be lost. The correct approach is to remount the modified configuration file on the local host into the container.

The above is the detailed content of Where is the nginx docker configuration file?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn