docker-compose安装nginx
# 编写yml
version: '3'
services:
nginx:
image: nginx:1.21.1 # 镜像`nginx:1.21.1`
container_name: nginx # 容器名为'nginx'
restart: unless-stopped # 指定容器退出后的重启策略为始终重启,但是不考虑在Docker守护进程启动时就已经停止了的容器
volumes: # 数据卷挂载路径设置,将本机目录映射到容器目录
- "./nginx/conf/nginx.conf:/etc/nginx/nginx.conf"
- "./nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf"
- "./nginx/html:/usr/share/nginx/html"
- "./nginx/log:/var/log/nginx"
environment: # 设置环境变量,相当于docker run命令中的-e
TZ: Asia/Shanghai
LANG: en_US.UTF-8
ports: # 映射端口
- "80:80"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 创建挂载的目录
# 记得修改为你自己的配置
mkdir -p /data/nginx/{conf,conf.d,html,logs}
1
2
2
# 创建配置文件
# 记得修改为你自己的配置
vim /data/nginx/conf/nginx.conf
1
2
2
查看conf内容
这个是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;
}
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
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
# 记得修改为你自己的配置
vim /data/nginx/conf.d/default.conf
1
2
2
查看conf内容
这个是default.conf参考内容
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;
location / {
#root /data/nginx/html;
root /usr/share/nginx/html;
index index.html index.htm;
#autoindex on;
#try_files $uri /index/index/page.html;
#try_files $uri /index/map/page.html;
}
#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;
#}
}
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
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
# 记得修改为你自己的配置
# 这个html就是默认访问nginx看到的页面可以自定义
vim /data/nginx/html/index.html
1
2
3
2
3
查看html内容
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>nginx牛皮</title>
</head>
<body>
<div id="datetime">
<script>
setInterval("document.getElementById('datetime').innerHTML=new Date().toLocaleString();", 1000);
</script>
</div>
</body>
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
# 运行
docker-compose -f docker-compose-nginx.yml -p nginx up -d
1
访问地址:[`ip地址:80`]
1
# 重载配置文件
docker exec -it 容器id service nginx reload
1
最近更新: 2024/12/25, 11:44:08