How to deploy nginx with Docker and modify the configuration file

How to deploy nginx with Docker and modify the configuration file

Deploy nginx with docker, it's so simple

Just one line of command:

docker run \
 --name nginx-health-web-pc \
 -d -p 6800:80 \
 -v /usr/docker/nginx/html:/usr/share/nginx/html \
 nginx

Running and starting is fun~~~~~At this time, the front-end suddenly comes over and says: "You need to add a configuration to your nginx", and also tells you: "So-and-so configured it like this before",

Of course you can't refuse at this time, but the actual configuration still requires some thought. Under normal circumstances, Docker is configured when it starts. Just mount the directory of the configuration file, which is simple and convenient. However, nginx first loads a main configuration file nginx.conf, and then loads the sub-configuration files in the conf.d directory in nginx.conf (usually at least one default.conf file). This is a lot more troublesome than mounting a directory separately, but it is not difficult as long as you have a clear idea.

Let's first look at the mounted command:

Start Docker command

docker run \
 --name myNginx \
 -d -p 80:80 \
 -v /usr/docker/myNginx/html:/usr/share/nginx/html \
 -v /etc/docker/myNginx/nginx.conf:/etc/nginx/nginx.conf:ro \
 -v /etc/docker/myNginx/conf.d:/etc/nginx/conf.d \
 nginx

There are a few things to note here:

(1) The first "-v" is the project location. Just put the project in the directory where it is mounted;

(2) The second "-v" is the main configuration file "nginx.conf" that is mounted. Note that there is a line "include /etc/nginx/conf.d/*.conf;" in the "nginx.conf" file. This include points to the path of the sub-configuration file. Please note that the path following include must not be wrong.

(3) The third "-v" also mounts the path of the sub-configuration file in Docker. Note that it must be consistent with the path pointed to by include in (2).

(4) It is important to emphasize that nginx.conf mounts a file (Docker does not recommend this), and conf.d mounts a directory

Let's start it first and find that there is a problem because the configuration file is not there yet.

Configuring Profiles

We find the configuration file generated when nginx is installed by the conventional method (usually under "/etc/nginx"), corresponding to the mount location in the above startup command, put the main configuration file nginx.conf in the corresponding location "/etc/docker/myNginx/nginx.conf", and put the sub-configuration file "default.conf" in the "/etc/docker/myNginx/conf.d" directory

Re-run the startup command and find that it is already OK. At this point, the files in docker can be configured at will, which is exactly the same as the native installation

Idea: When configuring, you must have an idea: the mounted file must be loaded into the docker process when it runs! This way it won't be easy to get confused.

---------------------------------------------------Dividing line-------------------------------------------------------

Post my configuration file:

nginx.conf

user root;
worker_processes 1;
 
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
 
 
events {
 worker_connections 1024;
}
 
 
http {
 include /etc/nginx/mime.types;
 default_type application/octet-stream;
 
 log_format main '$remote_addr - $remote_user [$time_local] "$request" '
      '$status $body_bytes_sent "$http_referer" '
      '"$http_user_agent" "$http_x_forwarded_for"';
 
 access_log /var/log/nginx/access.log main;
 
 sendfile on;
 #tcp_nopush on;
 
 keepalive_timeout 65;
 
 autoindex on;
 
 #gzip on;
 
 include /etc/nginx/conf.d/*.conf;
 client_max_body_size 100M;
 client_header_buffer_size 128k;
 large_client_header_buffers 4 128k;
}

default.conf

server {
 listen 80;
 server_name localhost;
 
 #charset koi8-r;
 #access_log /var/log/nginx/log/host.access.log main;
 
 location / {
  root /usr/nginx/dacheng-wechat-web;
  # root /usr/nginx/html;
  index index.html index.htm;
  autoindex on;
 try_files $uri /index/index/page.html;
  #try_files $uri /index/map/page.html;
 }
 
 #error_page 404 /404.html;
 
 # redirect server error pages to the static page /50x.html
 #
 error_page 500 502 503 504 /50x.html;
 location = /50x.html {
  root /usr/share/nginx/html;
 }
 
 # proxy the PHP scripts to Apache listening on 127.0.0.1:80
 #
 #location ~ \.php$ {
 # proxy_pass http://127.0.0.1;
 #}
 
 # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
 #
 #location ~ \.php$ {
 #root html;
 # fastcgi_pass 127.0.0.1:9000;
 # fastcgi_index index.php;
 # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
 #include fastcgi_params;
 #}
 
 # deny access to .htaccess files, if Apache's document root
 # concurs with nginx's one
 #
 #location ~ /\.ht {
 # deny all;
 #}
}

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Docker builds Nginx+PHP+MySQL environment and deploys WordPress practice
  • Docker for Beginners and Detailed Steps for Deploying NGINX
  • Docker nginx example method to deploy multiple projects
  • Docker deployment nginx implementation process graphic and text detailed explanation
  • Docker deploys Nginx and configures reverse proxy
  • Try Docker+Nginx to deploy single page application method
  • Docker Nginx container production and deployment implementation method
  • How to deploy Nginx on Docker

<<:  Summary of the use of vue Watch and Computed

>>:  MySQL 5.7 deployment and remote access configuration under Linux

Recommend

MySQL InnoDB transaction lock source code analysis

Table of contents 1. Lock and Latch 2. Repeatable...

Rendering Function & JSX Details

Table of contents 1. Basics 2. Nodes, trees, and ...

Vue and react in detail

Table of contents 1. Panorama II. Background 1. R...

How to configure domestic sources in CentOS8 yum/dnf

CentOS 8 changed the software package installatio...

Web Design Experience: Efficiently Writing Web Code

Originally, this seventh chapter should be a deep ...

Use of MySQL stress testing tool Mysqlslap

1. MySQL's own stress testing tool Mysqlslap ...

Install CentOS 7 on VMware14 Graphic Tutorial

Introduction to CentOS CentOS is an enterprise-cl...

mysql three tables connected to create a view

Three tables are connected. Field a of table A co...

Press Enter to automatically submit the form. Unexpected discovery

Copy code The code is as follows: <!DOCTYPE ht...

Nginx domain name SSL certificate configuration (website http upgraded to https)

Preface HTTP and HTTPS In our daily life, common ...

Solution to 1290 error when importing file data in mysql

Error scenario Use the mysql command in cmd to ad...

Detailed description of component-based front-end development process

Background <br />Students who work on the fr...

CSS style solves the problem of displaying ellipsis when the text is too long

1. CSS style solves the problem of displaying ell...

Detailed tutorial for downloading and installing mysql8.0.21

Official website address: https://www.mysql.com/ ...