1. Pull nginx

docker pull nginx
docker tag nginx nginx:v1
Copy the code

2. Create and run the container

docker run -p 9093:80 --name nginx -d nginx:v1
Copy the code

3. Enter the container

docker exec -it nginx /bin/bash
Copy the code

4. In the var directory of the container, create the images directory and files directory

mkdir images/html
mkdir files/html
Copy the code

5. Package new Nginx images based on the container

Docker stop nginx docker commit nginx nginx:v2 // docker commit Container name New image nameCopy the code

6. Create nginx, images, and Files in the var directory of the host and create corresponding files

mkdir images/html/pics
mkdir iamges/html/videos
mkdir files/html/txt
mkdir files/html/pdf
mkdir files/html/office
mkdir nginx/conf/conf.d
cd conf
touch nginx.conf
cd conf.d
touch default.conf
mkdir nginx/log
Copy the code

Default. conf: The following comment is not correct, delete it when creating

server { listen 80; listen [::]:80; server_name localhost; #charset koi8-r; #access_log /var/log/nginx/host.access.log main; /var/images/html has been mounted to the /var/images/html directory on the host. // location /pics means that we need to create the pics directory // on the host Location /pics {root /var/images/html; root /var/images/html; } // file server location/TXT {root /var/files/ 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 the code

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;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}
Copy the code

7. Use the new image to create an nginx container and mount the logs and configuration files of the nginx container to the var/nginx directory on the host

Containers / / nginx proxy configuration file/etc/nginx/conf. D/default. Conf / / nginx container configuration file/etc/nginx/nginx. Conf docker run - p 9093:80 - name nginx - v  /var/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /var/nginx/conf/conf.d/default.conf:/etc/nginx/conf.d/default.conf -v /var/nginx/log:/var/log/nginx -v /var/images/html:/var/images/html -v /var/files/html:/var/files/html -d nginx:v2Copy the code

After the success of the configuration, the browser visit: http://192.168.244.134:9093/pics/1.jpeg, you can see:

accesshttp://192.168.244.134:9093/txt/1.txt, garbled characters appear

The code solution in/var/nginx/conf/conf. D/default. Add the following code in the conf:

// Add charset 'utf-8' to location/TXT;Copy the code

8, to configure a new file server (such as video resources), you only need to configure a new resource address in the var directory of the local nginx default.conf, stop the container, and restart it.

location /videos {
    root /var/images/html;
}
Copy the code