注:此处部分简单说明省略
一、拉取镜像
docker pull nginx:stable
二、编写配置
1. docker-compose.yml配置
version: '3.1'
networks:
shared:
external:
name: shared
services:
nginx:
image: nginx:stable
container_name: nginx
# restart: always
ports:
- 80:80
- 443:443
- 8088:8088
volumes:
- ${PWD}/config/nginx.conf:/etc/nginx/nginx.conf
- ${PWD}/log/nginx:/var/log/nginx
- ${PWD}/../apps/html:/usr/share/nginx/html
- /etc/timezone:/etc/timezone:ro
- /etc/localtime:/etc/localtime:ro
networks:
- shared
2. nginx.conf配置
# 定义Nginx运行的用户和用户组
user nginx;
# nginx进程数,通常设置成和cpu的数量相等
worker_processes 4;
# 全局错误日志定义类型,[debug | info | notice | warn | error | crit]
error_log /var/log/nginx/error.log warn;
# 进程pid文件
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;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
server {
# 如果使用docker容器,必须暴露该端口
listen 8088;
server_name 172.20.228.138;
location / {
# 如果使用容器,可以使用容器本身的ip,可以使用docker0的ip,也可以使用宿主机ip
# proxy_pass http://172.18.0.14:9200/;
proxy_pass http://172.17.0.1:9200;
# proxy_pass http://172.20.228.138:9200;
index index.html index.htm index.jsp;
}
}
}
三、启动容器
四、查看结果