docker安装nginx
特别注意:
将容器里面的文件映射到本地的时候,一定要现在本地创建文件,然后将容器内部需要映射的文件的内容拷贝到本地文件里面,之后才能启动
# 安装
docker pull nginx
1
# 创建挂载的目录
mkdir -p /data/nginx/{conf,conf.d,html,logs}
1
# 创建配置文件
vim /data/nginx/conf/nginx.conf
1
查看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
查看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
vim /data/nginx/html/index.html 这个html就是默认访问nginx看到的页面可以自定义
1
查看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 exec -it 容器id service nginx reload
1
# 启动nginx
docker run --name nginx -d -p 80:80 -v /data/nginx/html:/usr/share/nginx/html -v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /data/nginx/logs:/var/log/nginx -v /data/nginx/conf.d:/etc/nginx/conf.d -d nginx:latest
1
#冒号前是本地路径(需要绝对路径),冒号后是容器中的路径
-v 是挂载的意思 将容器里面的配置文件挂载到本地 (使用-v挂载文件夹(推荐)
这种方法是在启动的时候使用-v将容器内部的文件夹挂载(映射)到本地的某个路径下,以后以后可以直接在本地修改,不需要进入容器内部.)
最近更新: 2024/12/25, 11:44:08