CentOS 7 Docker built using Nginx

1. The premise is Docker already installed

2. Pull the mirror Nginx

docker pull nginx
#使用命令查看拉取到的镜像
docker images

image.png

3. Run Nginx Mirror

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

Parameter Description

  • -d Run-time image in order to protect mode, which is running in the background
  • -pHost port mapping mirroring port, host port is on the left, the right is the mirror port, 80is Nginx access port
  • --nameTo the container from a unique alias

After starting the input docker ps -ato see the vessel running:

image.png

4. Access Nginx

Browser access http://ipto, the following instructions to run a successful page

image.png

5. Configuration Nginx

  • We first need to create a host for storage in nginx logs, configuration files and directories related to static resources, and mount it corresponds to the container path.
  • Subsequent updates we only need to change the profile or static files in a directory on the host can update the container resources, so you can ensure that the container hang only need to restart a container mounted on the opinions faultless data reduction, which is a lightweight container the reason quickly and easily. Nginx is not just a container, like the rest of the mysql container also must remember to mount / data data files to prevent loss of data containers shoot down.
mkdir -p /home/service/nginx/log
mkdir -p /home/service/nginx/conf
mkdir -p /home/service/nginx/conf.d
mkdir -p /home/service/nginx/static
mkdir -p /home/service/nginx/ssl

Conf directory and copy the configuration file to a host just created from Nginx container

docker cp nginx:/etc/nginx/nginx.conf /home/service/nginx/conf/nginx.conf

We can see already

image.png

Look at content

image.png

The figure can be seen, the configuration file also introduced other configuration files, so we need to includefile a copy also introduced to the host, but we do not know what those files is called, so we need to see into the inner container

docker exec -it nginx /bin/bash
cd /etc/nginx/conf.d
ls

You can see there are a default.conf file

image.png

We need to copy the file to the host, use the exitcommand to exit the container

exit
docker cp nginx:/etc/nginx/conf.d/default.conf /home/service/nginx/conf.d/default.conf

Remember when we visit nginx in front of that page? Yes, that page should be copied to the host

docker cp nginx:/usr/share/nginx/html/index.html /home/service/nginx/static/index.html

6. Modify Profile

Began to modify the copy out of the host conf file, first modify nginx.conf, modify configuration files modified after the results:

user  nginx;
worker_processes  1;
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;
worker_rlimit_nofile 65535;

events {
    use epoll;
    worker_connections 65535;
}


http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;
    charset utf-8;
    keepalive_timeout 60;
    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;
    server {
        listen 80;
        server_name  www.roes.top;
        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }
    }
    include /etc/nginx/conf.d/*.conf;
}

Viewdefault.conf

server {
    listen       80;
    server_name  localhost;
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

}
Nginx last stop container and remove the container
docker stop nginx
docker rm nginx
Nginx restart a mirror
docker run -p 443:443 -p 80:80 --name nginx \    
    --link jenkins \   
    -v /home/service/nginx/static:/usr/share/nginx/html  \  
    -v /home/service/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \    
    -v /home/service/nginx/log:/var/log/nginx  \   
    -v /home/service/nginx/conf.d:/etc/nginx/conf.d \   
    -v /home/service/nginx/ssl:/ssl \    
    -d nginx

-vIt means that the host directory directory is mounted to the container after the colon

--linkFor connecting a container, followed by a unique name zero container, so that nginx you can use the profile jenkins:端口configured

Here a multi-monitor 443 port for https configuration later
modify the default nginx index.html, more recognizable

vim /home/service/nginx/static/index.html

7. Configure access Https

I was in a cloud Ali applied for a one year free ssl certificate, we can look at Baidu, the download is nginx

And added a number of optimized configuration nginx.conf:

user  nginx;
worker_processes  1;
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;
worker_rlimit_nofile 65535;

events {
    use epoll;
    worker_connections 65535;
}


http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;
    charset utf-8;
    server_names_hash_bucket_size 128;
    client_header_buffer_size 2k;
    large_client_header_buffers 4 4k;
    client_max_body_size 8m;
    sendfile on;
    tcp_nopush on;
    keepalive_timeout 60;
    open_file_cache max=204800 inactive=20s;
    open_file_cache_min_uses 1;
    open_file_cache_valid 30s;
    tcp_nodelay on;
    gzip on;
    gzip_min_length 1k;
    gzip_buffers 4 16k;
    gzip_http_version 1.0;
    gzip_comp_level 2;
    gzip_types text/plain application/x-javascript text/css application/xml;
    gzip_vary on;
    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;
    server {
        listen  80;
        server_name   www.example.com ; 
        rewrite ^(.*)$  https://$host$1 permanent;
    }
    server {
        #监听的端口号
        listen  443 ssl;
        server_name  www.example.com ;
        ssl_certificate      /ssl/1492507_www.example.com.pem;
        ssl_certificate_key  /ssl/1492507_www.example.com.key;
        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;
        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;
        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }
    }
    include /etc/nginx/conf.d/*.conf;
}

Note: this update is on the host nginx.conf

Then restart nginx into the container can
also be restarted without entering the container, the container can reboot directly
docker restart nginx

docker exec -it nginx /bin/bash
nginx -s reload

Configuration

Guess you like

Origin www.cnblogs.com/roes/p/11069305.html