1、docker pull nginx
2、简易启动:docker run -d --name nginx -p 18089:80 nginx
3、将容器nginx.conf文件、conf.d文件夹内容复制到宿主机conf下

docker cp nginx:/etc/nginx/nginx.conf /usr/local/docker/nginx/conf
docker cp nginx:/etc/nginx/conf.d /usr/local/docker/nginx/conf

4、停止并删除容器:docker stop nginx docker rm nginx
5、挂载方式启动:

docker run \
-p 18089:80 \
--name nginx \
--restart always \
-v /usr/local/docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
-v /usr/local/docker/nginx/conf/conf.d:/etc/nginx/conf.d \
-v /usr/local/docker/nginx/log:/var/log/nginx \
-v /usr/local/docker/nginx/html:/usr/share/nginx/html \
-d nginx

6、在/usr/local/docker/nginx/html创建图片文件夹img、并上传一个图片
在这里插入图片描述
配置conf.d下的default.conf文件,添加:

location /img {
       root    /usr/share/nginx/html;
       autoindex       on;
}

7、重启:docker restart nginx
8、开放端口并访问:http://47.xxx.xxx.xx:18089/img/10b9f2859c586dd2c24f35d5d3246c7.jpg

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;
        keepalive_timeout  65;
        include /etc/nginx/conf.d/*.conf;
}

default.conf

server {
        listen       80;
        listen  [::]:80;
        server_name  localhost;

		#access_log  /var/log/nginx/host.access.log  main;

        location / {
                root   /usr/share/nginx/html;
                index  index.html index.htm;
        }

        location /img {
                root    /usr/share/nginx/html;
                autoindex       on;
        }

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

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐