Docker Deploy nginx and Modify Configuration File Implementation

  • 2021-06-28 09:53:04
  • OfStack

docker Deploying an nginx is easy, okay

Direct 1-line command:


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

Running start-up is also happy ~~~~Suddenly the front end comes up and says, "You have to add a configuration to nginx". Incidentally, it tells you, "Someone was matched this way before."

You can't refuse to win at this time, but it takes a lot of effort to configure it. In general, docker is configured at startup. It is simple and convenient to mount the directory of the configuration file, but nginx first loads a main configuration file, nginx.conf.In nginx.conf, load the subconfiguration file (1 usually at least 1 default.conf file) in the conf.d directory.It's a lot more cumbersome than mounting a single directory, but it's not difficult as long as you have a clear idea.

Let's look at the mounted commands 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

Here are a few considerations:

(1) The first "-v" is the location of the project. Just place the project in the mounted directory;

(2) The second'-v', is the mounted main configuration file,'nginx.conf', note that there is one line in the file,'include/etc/nginx/conf.d/*.conf';This include points to the path of the subconfiguration file, here note that path 1 following include must not be an error.

(3) The third'-v', the path of the sub-configuration file within docker is also mounted, note that it should be the same as that in (2) include points to path 1

(4) Emphasis 1, nginx.conf mounts a file (docker is not recommended for this purpose), conf.d mounts a directory

Let's start 1 first, and we can see that it's problematic because the configuration file is not there yet.

Configuration Profile

We found the configuration file generated when nginx was installed by the general method (typically under'/etc/nginx'), corresponding to the mount location in the boot command above, 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 directory'/etc/docker/myNginx/conf.d').

Re-run the boot command, found that it is ready, so the files in docker can be configured at will, just like the native installation

Ideas: Configuration 1 must rivet a way of thinking: when the mounted file is running, it is loaded into the docker process!This is not easy to confuse.

---------------------------------------------------------------------------------------------------------------

Post my profile:

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;
 #}
}

Related articles: