Docker安装Nginx

Docker安装Nginx

Nginx

Nginx虚拟主机

Docker安装Nginx

# 创建文件夹
cd /usr/local
mkdir docker
cd docker
mkdir nginx
cd nginx
# 创建docker-compose.yml
vi docker-compose.yml

docker-compose.yml

version: '3.1'
services:
  nginx:
    restart: always
    image: nginx
    container_name: nginx
    ports:
      - 80:80
      - 8080:8080
    volumes:
      - ./conf/nginx.conf:/etc/nginx/nginx.conf
      - ./html:/usr/share/nginx/html

nginx.conf

# 启动进程,通常设置成和 CPU 的数量相等
worker_processes  2;
events {
    # epoll 是多路复用 IO(I/O Multiplexing) 中的一种方式
    # 但是仅用于 linux2.6 以上内核,可以大大提高 nginx 的性能
    use epoll;
    # 单个后台 worker process 进程的最大并发链接数    
    worker_connections  1024;
}
http {
    # 设定 mime 类型,类型由 mime.type 文件定义    
    include       mime.types;
    default_type  application/octet-stream;
    # sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,对于普通应用,   
    # 必须设为 on,如果用来进行下载等应用磁盘 IO 重负载应用,可设置为 off,以平衡磁盘与网络 I/O 处理速度,降低系统的 uptime.
    sendfile        on;
    # 连接超时时间    
    keepalive_timeout  65;
    # 设定请求缓冲    
    client_header_buffer_size 2k;
    # 配置虚拟主机 192.168.65.161
    server {
        # 监听的 IP 和端口,配置 192.168.65.161:80
        listen       80;
        # 虚拟主机名称这里配置 IP 地址
        server_name  192.168.65.161;
        # 所有的请求都以 / 开始,所有的请求都可以匹配此 location
        location / {
        # 使用 root 指令指定虚拟主机目录即网页存放目录    
        # 比如访问 http://ip/index.html 将找到/usr/local/docker/nginx/html/html80/index.html
        # 比如访问 http://ip/item/index.html 将找到/usr/local/docker/nginx/html/html80/item/index.html
            root   /usr/share/nginx/html/html80;
            # 指定欢迎页面,按从左到右顺序查找    
            index  index.html index.htm;
        }
    }
    # 配置虚拟主机 192.168.65.161
    server {
        listen       8080;
        server_name  192.168.65.161;
        location / {
            root   /usr/share/nginx/html/html8080;
            index  index.html index.htm;
        }
    }
}

创建文件

cd /usr/local/docker/nginx
mkdir html
cd html
mkdir html80
vi index.html
cd ..
mkdir html8080
vi index.html

执行

cd /usr/local/docker/nginx
docker-compose up -d

访问 http://192.168.65.161:80

访问 http://192.168.65.161:8080

nginx.conf IP地址

worker_processes  2;
events {
    use epoll;
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    client_header_buffer_size 2k;
    server {
        listen       80;
        server_name  192.168.65.161;
        location / {
            root   /usr/share/nginx/html/html80;
            index  index.html index.htm;
        }
    }
    server {
        listen       8080;
        server_name  192.168.65.161;
        location / {
            root   /usr/share/nginx/html/html8080;
            index  index.html index.htm;
        }
    }
}

nginx.conf 域名

要把域名添加到Hosts

192.168.65.161 service1.wenqu.com
192.168.65.161 service2.wenqu.com
worker_processes  2;
events {
    use epoll;
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    client_header_buffer_size 2k;
    server {
        listen       80;
        server_name  service1.wenqu.com;
        location / {
            root   /usr/share/nginx/html/html80;
            index  index.html index.htm;
        }
    }
    server {
        listen       80;
        server_name  service2.wenqu.com;
        location / {
            root   /usr/share/nginx/html/html8080;
            index  index.html index.htm;
        }
    }
}

访问 http://service1.wenqu.com

访问 http://service2.wenqu.com

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

问渠科技

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值