简单的Nginx踩坑记----docker-compose安装nginx

在/usr/local/docker/nginx 目录下放配置文件docker-compose.yml --[目录自定义]
docker-compose.yml:

version: '3.7'
services:
  nginx:
    container_name: nginx
    image: nginx:1.18.0-alpine
    restart: always
    privileged: true
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
      - ./data:/usr/share/nginx/html 
    environment:
      - TZ=Asia/Shanghai
    ports:
      - 80:80

在/usr/local/docker/nginx 目录下放配置文件nginx.conf --[目录自定义]
nginx.conf:


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  192.168.x.137;
        location / {
            root   /usr/share/nginx/html/;
            index  index.html index.htm;
            allow all;
            charset utf-8;
        }
        location = /50x.html {
            root   html;
        }
    }
}

$ docker-compose up [-d]

访问192.168.x.137:80
Welcome to nginx!

说明nginx是成功了的。

报错: open() “/usr/local/nginx/html/favicon.ico” failed (2: No such file or directory)
在nginx.conf中加入
location = /favicon.ico {
log_not_found off;
access_log off;
}

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  192.168.x.137;
        location / {
            root   /usr/share/nginx/html/;
            index  index.html index.htm;
            allow all;
            charset utf-8;
        }
        ####加入的段##########
        location = /favicon.ico {
              log_not_found off;
              access_log off;
        }
        ####加入的段##########
        location = /50x.html {
            root   html;
        }
    }
}

创建一个index.html
index.html

<!DOCTYPE html>
<html>
  <head>
    <title>example</title>
   </head>
   <body>
      <h1>example </h1>
      <a href="http://www.baidu.com" target="_Blank">百度</a>
   </body>
</html>

启先这个html被放在/usr/share/nginx/html/下,结果访问

http://192.168.x.173/index.html

一直报错
nginx | 2020/05/09 19:21:14 [error] 6#6: *3 open() “/usr/share/nginx/html/index.html” failed (2: No such file or directory),
client: 192.168.x.1, server: 192.168.x.173, request: “GET /index.html HTTP/1.1”, host: “192.168.x.173”

查到index.html挂载docker出了问题
把index.html放到/usr/local/docker/nginx/data下,这个目录自己mkdir

在这里插入图片描述

小记一次


版权声明:本文为janckgger原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。