- 在Docker下载Nginx镜像
docker pull nginx:1.21.6
docker images
- 创建挂载目录(在下面的/data/nginx/html目录下编写自己的html文件,不挂载html目录也可以)
mkdir -p /mnt/nginx/{conf,html,logs}
mkdir -p /mnt/nginx/conf.d
- 编写nginx.conf配置文件,并放在自己创建的 /data/nginx 文件夹中
#user nobody;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html; #指定容器中的路径
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html; #指定容器中的路径
}
}
include /etc/nginx/conf.d/*.conf;
}
- 启动容器(挂载nginx.conf文件,conf.d目录,html目录,和logs目录)
docker run --name nginx -d -p 80:80 --restart unless-stopped -v /mnt/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /mnt/nginx/conf/conf.d:/etc/nginx/conf.d -v /mnt/nginx/logs:/var/log/nginx -v /mnt/nginx/html:/usr/share/nginx/html -d nginx:1.21.6
tip:如果你html中没有index.html网页会出现错误404,自己随意编写index.html就可以
直接访问 自己IP即可