nginx容器创建与本地连接
拉取镜像
docker pull nginx
查看镜像是否拉取成功
docker images
创建nginx挂载目录
mkdir -p /home/zhangdan/nginx/conf
mkdir -p /home/zhangdan/nginx/html
mkdir -p /home/zhangdan/nginx/logs
cd conf
touch nginx.conf
vi nginx.conf
nginx.conf
user root;
worker_processes 1;
error_log logs/error.log;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
client_max_body_size 10000m;
#解决参数过长414报错
large_client_header_buffers 4 2048k;
client_header_buffer_size 2048k;
server {
listen 1111;
server_name 127.0.0.1;
location /system {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://systemUrl:8888/system;
}
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
说明
对Nginx所在服务器的本地服务进行反向代理
因为会给容器服务自动分配ip,所以proxy_pass后的服务地址不能写为127.0.0.1.需先自定义网络
创建容器
docker run -d --name nginx \
--net=mynet \
--restart=always \
--privileged=true \
-p 1111:1111 \
--link system:systemUrl \
--link device:deviceUrl \
--link community-manager:communityUrl \
--link community-service:serviceUrl \
--link nbmp:nbmpUrl \
-v /home/cmct/nginx/logs:/var/log/nginx \
-v /home/cmct/nginx/conf:/etc/nginx/conf.d \
-v /home/cmct/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
-v /home/cmct/nginx/logs:/etc/nginx/logs \
-v /home/cmct/nginx/logs/images.log:/usr/local/nginx/images.log \
-v /home/cmct/image:/home/cmct/image \
-v /home/cmct/nginx/html:/usr/share/nginx/html nginx
命令说明
1…理论上说创建容器只需要执行d
docker run --name nginx -d -p 1111:1111
-v /home/zhangdan/nginx/logs:/var/log/nginx
-v /home/zhangdan/nginx/conf/nginx.conf:/etc/nginx/nginx.conf
但因为我本地的Nginx配置文件里自定义了日志路径,所以,只执行这一句启动会报错,如:nginx: [emerg] open() “/etc/nginx/logs/error.log” failed (2: No such file or directory)与nginx: [emerg] open() “/usr/local/nginx/images.log” failed (2: No such file or directory)等,所以对自定义的日志也须挂载
2.执行了以上命令之后,会出现以下报错:nginx: [emerg] open() “/usr/local/nginx/images.log” failed (21: Is a directory)
是因为/home/zhangdan/nginx/logs/images.log自动生成的images.log是一个文件夹,需要将其删除,手动touch该文件