前置条件
- 安装docker
- 安装docker-compose
基于容器运行服务
编辑启动文件
创建启动文件存放路径
mkdir -p /usr/local/docker/docker-nginx
切换到路径 docker-nginx
cd /usr/local/docker/docker-nginx
将内容写入 docker-compose.yml
vi /usr/local/docker/docker-nginx/docker-compose.yml
version: '3.7'
services:
nginx:
image: nginx
container_name: nginx
restart: always
environment:
- TZ=Asia/Shanghai
ports:
- '80:80'
- '443:443'
也可以使用将内容直接写入文件: cat > /usr/local/docker/nginx/docker-compose.yml<<‘EOF’ 内容 EOF
运行容器并访问
运行容器
docker-compose -f /usr/local/docker/docker-nginx/docker-compose.yml up -d
浏览器访问自己的域名,看到如下页面说明启动成功
配置文件
宿主机创建映射路径
创建nginx存放配置、静态资源文件及目录
mkdir -p /usr/local/docker-nginx/nginx/
mkdir -p /usr/local/docker-nginx/nginx/conf.d/
mkdir -p /usr/local/docker-nginx/nginx/html/
mkdir -p /var/local/docker-nginx/nginx/logs/
touch /usr/local/docker/docker-nginx/nginx/nginx.conf
touch /usr/local/docker/docker-nginx/nginx/conf.d/default.conf
映射说明见附录:数据卷映射说明
复制内部配置及静态页
将容器内部配置文件复制出来,稍后配置在宿主机中
进入容器
- 宿主机执行命令通过容器名称进入容器内部
docker exec -it nginx bash
- 切换到nginx默认配置路径
cd /etc/nginx
nginx.conf
- 查看配置文件并复制
more /etc/nginx/nginx.conf
default.conf
- 查看配置文件并复制
more /etc/nginx/conf.d/default.conf
粘贴对应的配置文件
nginx.conf
将刚才复制的配置文件,按照上边宿主机创建的文件进行粘贴
vi /usr/local/docker/docker-nginx/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;
}
配置说明见附录:nginx.conf 配置说明
default.conf
将刚才复制的配置文件,按照上边宿主机创建的文件进行粘贴
vi /usr/local/docker/docker-nginx/nginx/conf.d/default.conf
server {
listen 80; # Nginx监听的端口号,配置中默认是占用80端口
listen [::]:80;
server_name localhost; # Nginx接受请求的IP或域名
location / {
root /usr/share/nginx/html; # 将接受到的请求根据/usr/share/nginx/html去查找静态资源
index index.html index.htm; # 默认去上述的路径中找到index.html或index.htm
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
配置说明见附录:default.conf 配置说明
欢迎页和异常页
数据卷映射静态文件
cd /usr/local/docker/docker-nginx/nginx/html
- 新建index.html
<!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>
- 新建50x.html
<!DOCTYPE html>
<html>
<head>
<title>Error</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>An error occurred.</h1>
<p>Sorry, the page you are looking for is currently unavailable.<br/>
Please try again later.</p>
<p>If you are the system administrator of this resource then you should check
the error log for details.</p>
<p><em>Faithfully yours, nginx.</em></p>
</body>
</html>
重新运行容器服务
修改启动文件
- 启动文件中加上数据卷映射
volumes
部分
vi /usr/local/docker/docker-nginx/docker-compose.yml
version: '3.7'
services:
nginx:
image: nginx
container_name: nginx
restart: always
environment:
- TZ=Asia/Shanghai
ports:
- '80:80'
- '443:443'
volumes:
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
- ./nginx/conf.d/:/etc/nginx/conf.d
- ./nginx/html/:/usr/share/nginx/html
- /var/local/nginx/logs/:/var/log/nginx
停止容器
docker-compose -f /usr/local/docker/docker-nginx/docker-compose.yml down
运行容器并访问
控制台运行容器
docker-compose -f /usr/local/docker/docker-nginx/docker-compose.yml up -d
浏览器访问自己的域名,看到如下页面说明启动成功
配置文件重新加载
如果在运行的过程中需要调整配置,可以使用如下两个命令重新加载配置文件
- 检查配置文件
docker exec -it nginx service nginx check-reload
- 重新加载配置文件
docker exec -it nginx service nginx reload
附录
问题记录
外部访问无响应
解决方案:
- 关闭防火墙 (如果仅开发使用关闭即可,无需要考虑服务器是否安全)
- 防火墙开放端口 (生产环境需要配置一些规则开放对应的端口)
数据卷映射说明
mkdir -p /usr/local/docker-nginx/nginx/ # Nginx 配置主路径
mkdir -p /usr/local/docker-nginx/nginx/conf.d/ # Nginx 所有引用配置存放路径,以后多数配置在此路径中
mkdir -p /usr/local/docker-nginx/nginx/html/ # 静态资源路径
mkdir -p /var/local/docker-nginx/nginx/logs/ # 日志存放路径
touch /usr/local/docker/docker-nginx/nginx/nginx.conf # 全局配置文件 主配置文件会扫描 conf.d路径下所有以.conf结尾的配置文件
touch /usr/local/docker/docker-nginx/nginx/conf.d/default.conf # 引用配置文件
docker-compose 简单说明
version: '3.7' # docker-compose 对应命令版本
services: # 服务组
nginx: # 容器
image: nginx # 镜像名称
container_name: nginx # 容器名称
restart: always # 重启服务器自动启动
environment:
- TZ=Asia/Shanghai # 指定时区
ports:
- '80:80' # http 宿主机端口:容器内端口
- '443:443' # https 宿主机端口:容器内端口
volumes:
- ./nginx/nginx.conf:/etc/nginx/nginx.conf # 全局配置文件
- ./nginx/conf.d/:/etc/nginx/conf.d # server 详细配置引用
- ./nginx/html/:/usr/share/nginx/html # 静态文件存放路径
- /var/local/nginx/logs/:/var/log/nginx # 运行日志路径
nginx.conf 配置说明
user nginx; # 当前需要运行nginx的linux用户
worker_processes 1; # 数值越大,Nginx 并发能力越强(不是随便写的,需要根据实际服务器配置编写)
error_log /var/log/nginx/error.log warn; # 错误日志存放位置
pid /var/run/nginx.pid; # 进程id
events {
worker_connections 1024; # 数值越大,Nginx 并发能力越强(不是随便写的,需要根据实际服务器配置编写)
}
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; # 引入了conf.d路径下所有以.conf结尾的配置文件
}
default.conf 配置说明
server {
listen 80; # Nginx监听的端口号,配置中默认是占用80端口
listen [::]:80;
server_name localhost; # Nginx接受请求的IP或域名
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html; # 将接受到的请求根据/usr/share/nginx/html去查找静态资源
index index.html index.htm; # 默认去上述的路径中找到index.html或index.htm
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}