注:本文是本人根据B站【狂神说Java】Docker视频所写,仅供学习参考。
Docker 安装 nginx
1、搜索镜像 docker search nginx
[root@MiWiFi-R3600-srv /]# docker search nginx
2、下载镜像 docker pull nginx
[root@MiWiFi-R3600-srv /]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
bb79b6b2107f: Pull complete
5a9f1c0027a7: Pull complete
b5c20b2b484f: Pull complete
166a2418f7e8: Pull complete
1966ea362d23: Pull complete
Digest: sha256:aeade65e99e5d5e7ce162833636f692354c227ff438556e5f3ed0335b7cc2f1b
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
3、运行调试
[root@MiWiFi-R3600-srv /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest c39a868aad02 5 days ago 133MB
centos latest 0d120b6ccaa8 3 months ago 215MB
# -d 后台运行
# --name 给容器命名
# -p 宿主机端口:容器内部端口
[root@MiWiFi-R3600-srv /]# docker run -d --name nginx -p 80:80 nginx
4b2ff905ed58c6b8812195097f09423014f9bc9aab06728c395be437430a6546
[root@MiWiFi-R3600-srv /]# curl localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
4、测试访问: http://ip
5、下面创建文件夹方便进行映射,准备配置文件,其中ssl中放域名证书。
创建文件夹
mkdir -p /data/docker/nginx/{logs,ssl,html,conf/conf.d}/
复制配置文件
docker cp nginx:/etc/nginx/nginx.conf /data/docker/nginx/conf/nginx.conf
docker cp nginx:/etc/nginx/conf.d/default.conf /data/docker/nginx/conf/conf.d/default.conf
6、准备工作完成
#停止
docker stop nginx
#删除
docker rm nginx
7、重新运行挂载
docker run --detach --name nginx -p 443:443 -p 80:80 --restart=always --privileged=true -v /data/docker/nginx/html:/usr/share/nginx/html:rw -v /data/docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf/:rw -v /data/docker/nginx/conf/conf.d/default.conf:/etc/nginx/conf.d/default.conf:rw -v/data/docker/nginx/logs:/var/log/nginx/:rw -v /data/docker/nginx/ssl:/ssl/:rw -d nginx
8、在宿主机挂在卷html文件夹下添加index.html文件测试
# 进入 /data/docker/nginx/html/
[root@localhost ~]# cd /data/docker/nginx/html/
[root@localhost html]# vim index.html
[root@localhost html]# cat index.html
<p> Hello Nginx </p>
9、测试访问: http://ip
安装成功
nginx的开放式CORS配置
location / {
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
#
# 自定义标题和标题各种浏览器*应该*可以,但不是
#
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
#
# 告诉客户这个飞行前信息有效期为20天
#
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain; charset=utf-8';
add_header 'Content-Length' 0;
return 204;
}
if ($request_method = 'POST') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
}
if ($request_method = 'GET') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
}
}