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