How Docker deploys nginx and modifies configuration files

WBOY
Release: 2023-05-12 15:19:13
forward
1471 people have browsed it

Get it done with 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
Copy after login

It’s a joy to run and start~~~~~At this time, the front end suddenly came over and said: "You need to add a configuration to your nginx", and by the way, he also told you: " "So-and-so was configured like this before",

You can't refuse if you are competitive at this time, but it still takes some effort to actually configure it. Under normal circumstances, docker is configured when it starts, as long as the configuration file Just mount the directory, 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 alone, but it's not difficult as long as you have a clear idea.

Let’s look at the mounted command first:

Command to start docker

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
Copy after login

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 Mount the main configuration file "nginx.conf". 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. This Please pay attention to the path following include and make sure there is no mistake.

(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 is not recommended to use this way), and conf.d mounts a directory.

Let’s start it first, and we can find that there is a problem. Because the configuration file does not exist yet.

Configuration configuration file

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

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

Thinking: There must be one idea when configuring: the mounted file must be loaded into the docker process when running! This way there is less confusion.

-------------------------------------------------- -------Divider 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;
}
Copy after login

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;
 #}
}
Copy after login

The above is the detailed content of How Docker deploys nginx and modifies configuration files. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!