安装Nginx
- 拉取最新的镜像
新建待映射的目录
1
| mkdir -p /usr/local/nginx/{conf,html,logs,ssl}
|
运行一个docker容器(当前实例的作用就是拷贝出默认配置文件)
1
| docker run --name nginx -p 80:80 -d nginx
|
如果遇到运行报错的情况,可以考虑重启服务器(来自浪费了一个小时排查却找不到问题的屑)
获取实例的容器ID,拷贝容器文件到待映射的目录
1 2 3 4 5 6
| # 获取容器id列表,找到对应的容器ID docker ps -a
# 拷贝文件 docker cp <容器ID>:/etc/nginx/nginx.conf /usr/local/nginx/conf/ docker cp <容器ID>:/etc/nginx/conf.d /usr/local/nginx/
|
停止并删除容器实例
1 2
| docker stop <容器ID> docker rm <容器ID>
|
运行最终的实例
1 2 3 4 5 6 7 8 9
| docker run \ --name nginx \ -p 443:443 -p 80:80 \ -v /usr/local/nginx/logs:/var/log/nginx \ -v /usr/local/nginx/html:/usr/share/nginx/html \ -v /usr/local/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \ -v /usr/local/nginx/conf.d:/etc/nginx/conf.d \ -v /usr/local/nginx/ssl:/etc/nginx/ssl/ \ --privileged=true -d --restart=always nginx
|
最后附一张启动正常的访问截图。
![]()
配置多站点
在默认的nginx配置文件中,有这么一行:
1
| include /etc/nginx/conf.d/*.conf;
|
表示nginx在启动时,会自动加载conf.d下的所有配置项。我们只需要在conf.d
目录下创建多个配置文件就能完成多站点的配置。
配置SSL
配置ssl需要做两件事
1、创建SSL证书
2、调整nginx配置文件,增加ssl_certificate
和ssl_certificate_key
等配置项
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
| server { listen 80; 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; }
}
server { listen 443 ssl; server_name localhost;
ssl_certificate /etc/nginx/ssl/localhost.pem; ssl_certificate_key /etc/nginx/ssl/localhost.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; }
error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } }
|
参考文档
docker安装nginx配置ssl(IT技术)