Docker下安装Nginx
下载nginx1.10的docker镜像:
docker pull nginx:1.10
从容器中拷贝nginx配置
先运行一次容器(为了拷贝配置文件):
docker run -p 80:80 --name nginx \
-d nginx:1.10
将容器内的配置文件拷贝到指定目录:
# 创建文件夹
mkdir -p /mydata/nginx/conf/conf.d /mydata/nginx/conf /mydata/nginx/html /mydata/nginx/logs /mydata/nginx/ssl
docker container cp nginx:/etc/nginx/nginx.conf /mydata/nginx/conf
docker container cp nginx:/etc/nginx/conf.d/default.conf /mydata/nginx/conf.d/default.conf
docker container cp nginx:/usr/share/nginx/html/index.html /mydata/nginx/html
终止并删除容器:
docker stop nginx
docker rm nginx
使用docker命令启动:
docker run -p 80:80 --name nginx \
-v /mydata/nginx/html:/usr/share/nginx/html \
-v /mydata/nginx/logs:/var/log/nginx \
-v /mydata/nginx/conf:/etc/nginx \
-d nginx:1.10
证书扩展
在 /mydata/nginx/conf.d/default.conf 文件中增加一个server
server {
listen 443 ssl;
server_name nas.haibaba.top;
ssl_certificate /etc/nginx/ssl/9947905_nas.haibaba.top.pem;
ssl_certificate_key /etc/nginx/ssl/9947905_nas.haibaba.top.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
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;
}
}
运行docker容器
docker run \
--name nginx \
-p 443:443 -p 80:80 \
-v /mydata/nginx/logs:/var/log/nginx \
-v /mydata/nginx/html:/usr/share/nginx/html \
-v /mydata/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
-v /mydata/nginx/conf.d:/etc/nginx/conf.d \
-v /mydata/nginx/ssl:/etc/nginx/ssl/ \
--privileged=true -d --restart=always nginx