docker安装nginx

docker运行nginx

镜像地址:https://hub.docker.com/_/nginx

快速启动nginx容器

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

持久化配置

mkdir -p /etc/nginx/
docker run -d --name tmp-nginx-container nginx
docker cp tmp-nginx-container:/etc/nginx/nginx.conf /etc/nginx/
docker rm -f tmp-nginx-container

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

持久化数据

mkdir -p /usr/share/nginx
docker run -d --name tmp-nginx-container nginx
docker cp tmp-nginx-container:/usr/share/nginx/html /usr/share/nginx
docker rm -f tmp-nginx-container

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

dockerfile持久化配置和数据

FROM nginx
COPY static-html-directory /usr/share/nginx/html

FROM nginx
COPY nginx.conf /etc/nginx/nginx.conf

完整示例

#取出nginx容器默认配置文件
mkdir -p /data/nginx/
docker run -d --name tmp-nginx-container nginx
docker cp tmp-nginx-container:/etc/nginx/nginx.conf /data/nginx/
docker cp tmp-nginx-container:/etc/nginx/conf.d /data/nginx/
docker cp tmp-nginx-container:/usr/share/nginx/html /data/nginx/
docker rm -f tmp-nginx-container

#运行nginx容器
docker run -d --name nginx -p 80:80 \
  -e TZ=Asia/Shanghai \
  -v /data/nginx/nginx.conf:/etc/nginx/nginx.conf \
  -v /data/nginx/conf.d:/etc/nginx/conf.d \
  -v /data/nginx/html:/usr/share/nginx/html \
  -v /data/nginx/logs:/var/log/nginx \
  nginx

浏览器访问服务器80端口验证:
docker安装nginx_第1张图片
docker中/etc/nginx/nginx.conf默认配置文件

# cat /etc/nginx/nginx.conf
user  nginx;
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;
}

docker中/etc/nginx/conf.d/default.conf配置

# cat /etc/nginx/conf.d/default.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;
    }
}

配置示例

创建自定义html文件

mkdir -p /data/nginx/html/docker/

cat > /data/nginx/html/docker/index.html <<EOF



Docker Nginx



Hello Nginx Docker!



EOF

创建对应配置文件

cat > /data/nginx/conf.d/docker.conf <<EOF
server {
    listen       80;
    server_name  localhost;

    location / {
        root   /usr/share/nginx/html/docker;
        index  index.html index.htm;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}
EOF

检查配置文件是否正确

docker exec -it nginx nginx -t

配置热更新

docker exec -it nginx nginx -s reload

或重启nginx容器

docker restart nginx

浏览器访问验证
docker安装nginx_第2张图片

你可能感兴趣的:(Linux)