Docker安装配置nginx

docker安装配置nginx

拉取镜像:

1
docker pull nginx:1.19.5

创建映射文件夹

1
mkdir -p /data/nginx/www /data/nginx/logs /data/nginx/conf

创建一个默认的niginx 镜像,并复制出配置文件,此步的目的主要是把配置文件复制出来,如果,已经有配置文件,此步跳过

1
2
3
4
5
6
7
docker run --name nginx-test -p 10080:80 -d nginx:1.19.5

docker cp 865626ab7f68:/etc/nginx/nginx.conf /data/nginx/conf

docker stop 865626ab7f68

docker rm 865626ab7f68

启动nginx 镜像,并映射出文件

1
docker run -d -p 10080:80 --name nginx-web -v /data/nginx/www:/usr/share/nginx/html -v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /data/nginx/logs:/var/log/nginx nginx:1.19.5

//查找需要映射的docker容器真实ip

1
docker inspect --format='{{.NetworkSettings.IPAddress}}' 70d927e27fa5

进入到/root/conf/nginx.conf 添加如下即可

1
2
3
4
5
6
7
8
9
10
11
server{
listen 80;
charset utf-8;
server_name 192.168.43.50;
#server_name 172.17.0.4;
location / {
proxy_pass http://192.168.43.50:3959;
#proxy_pass http://172.17.0.4:3959;
proxy_redirect default;
}
}

nginx.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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;
server{
listen 80;
charset utf-8;
server_name 192.168.43.50;
#server_name 172.17.0.4;
location / {
proxy_pass http://192.168.43.50:3959;
#proxy_pass http://172.17.0.4:3959;
proxy_redirect default;
}
}

}

将index.html的文件放入/data/nginx/www

重启容器

1
2
docker stop nginx-web
docker start nginx-web
继开 wechat
欢迎加我的微信,共同交流技术