先使用docker安装好Nginx没安装可以转入下边链接安装

https://www.sky12580.cn/archives/docker-an-zhuang-nginx

1、先创建存放配置文件(nginx.conf)和项目的文件夹(我这是macOs,linux操作和这个一样)

mkdir -p /Users/yinwenshi/data/nginx

mkdir conf conf.d html logs

2、然后在conf文件夹创建nginx.conf文件,填入下边内容

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
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;

}

3、然后在conf.d创建default.conf文件

server {
    listen       80;

    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;
    }
    
    # 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;
    #}
}

4、在html文件中添加index.html,添加以下内容(可以自定义)

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Docker启动Nginx</title>
</head>
<body>
    <div>
        Docker启动Nginx!!
     </div>
</body>
</html>

5、挂载自定义文件启动nginx(需要先把之前nginx的容器停止并删除,否则启动失败)(注意路径)

docker run -d --name nginx  -p 80:80  \
-v  /Users/yinwenshi/data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
-v  /Users/yinwenshi/data/nginx/conf.d:/etc/nginx/conf.d  \
-v  /Users/yinwenshi/data/nginx/html:/usr/share/nginx/html  \
-v  /Users/yinwenshi/data/nginx/logs:/var/log/nginx -d nginx

6、然后打开浏览器,输入ip查看

dockernginx