1.Nginx安装及配置文件介绍

Nginx安装

1.yum安装

http://nginx.org/en/linux_packages.html#RHEL-CentOS 按照官方文档操作

2.docker安装

1.检查docker,docker-compose是否启动
[root@localhost ~]# docker-compose
[root@localhost ~]# systemctl start docker
2.创建安装维护Nginx的目录
[root@localhost ~]# mkdir /opt/docker_nignx
[root@localhost ~]# cd /opt/docker_nginx
3.编写docker-compose.yml文件安装docker
[root@localhost docker_nignx]# vi docker-compose.yml
version: '3.1'
services: 
  nginx:
    restart: always
    image: daocloud.io/library/nginx:latest
    container_name: nginx
    ports: 
      - 80:80
[root@localhost docker_nignx]# docker-compose up -d
Creating nginx ... done

Nginx配置文件介绍

1.容器默认存放路径
docker ps
docker exec -it 容器id bash
root@684e9f3e88f2:~# cd /etc/nginx
2.nginx的核心配置文件nginx.conf
[全局块]
worker_processes  1; # worker_processes的数值越大,Nginx的并发能力就越强
error_log  /var/log/nginx/error.log warn; # error_log代表Nginx错误日志存放的位置
pid        /var/run/nginx.pid; # pid是Nginx运行的一个标识
[events块]
events {
    worker_connections  1024; # worker_connections的数值越大,Nginx的并发能力就越强,运维人员根据服务情况做相应调整
}
[http块]
http {
    include       /etc/nginx/mime.types; # include引入一个外部文件->> /mime.types中存放着大量的媒体响应类型
    default_type  application/octet-stream; # default_type 默认响应类型
    include /etc/nginx/conf.d/*.conf; # include /etc/nginx/conf.d/*.conf;->> 引入conf.d目录下以.conf结尾的配置文件
}
root@684e9f3e88f2:~# vi /etc/nginx/conf.d/default.conf 有个默认的default.conf配置文件
[server块]
server {
    listen       80;        # listen代表Nginx监听的端口号
    server_name  localhost; # server_name代表Nginx接受请求的IP

[location块在sever块的里面]
    location / {
        root   /usr/share/nginx/html;  # 将接受到的请求根据/usr/share/nginx/html去查找静态资源
        index  index.html index.htm;   # 默认去上述的路径中找到index.html或index.htm
    } 
}

nginx基本设置

这里为了以后方便我们编辑容器里的nginx配置文件,所以需要映射一下数据卷到宿主机上,重新构建一下docker-compose文件就好

1.先停止docker-compose
[root@localhost docker_nignx]# docker-compose down
Stopping nginx ... done
Removing nginx ... done
Removing network docker_nignx_default
2.编辑docker-compose文件,重新构建,并启动
[root@localhost docker_nignx]# vi docker-compose.yml
version: '3.1'
services: 
  nginx:
    restart: always
    image: daocloud.io/library/nginx:latest
    container_name: nginx
    ports: 
      - 80:80
    volumes:
      - /opt/docker_nginx/conf.d/:/etc/nginx/conf.d
[root@localhost docker_nignx]# docker-compose build
nginx uses an image, skipping
[root@localhost docker_nignx]# docker-compose up -d
Creating nginx ... done
[root@localhost docker_nignx]# ls     #查看是否创建文件
conf.d  docker-compose.yml
3.重新编写server块进行测试
[root@localhost docker_nginx]# cd conf.d/
[root@localhost conf.d]# vi defualt.conf
server{
  listen 80;
  server_name localhost;
  
  location / {
    root /usr/share/nginx/html;
    index index.html index.htm;
  }
}

[root@localhost conf.d]# cd .. #回到docker-compose.yml存放目录
[root@localhost docker_nginx]# docker-compose restart #重启一下
Restarting nginx ... done

3.访问主机IP测试是否成功

遇到问题以下问题就是没有进入到docker-compose.yml存放目录下运行

ERROR:
        Can't find a suitable configuration file in this directory or any
        parent. Are you in the right directory?

        Supported filenames: docker-compose.yml, docker-compose.yaml
原文地址:https://www.cnblogs.com/eba001/p/14304247.html