1 在root目录下创建一个nginx目录;
创建conf目录并创建一个nginx的主配置文件nginx.conf;
创建conf.d目录并创建一个default.conf文件
创建html目录
创建logs目录并创建两个文件:access.log、error.log
nginx.conf的内容如下:
user nginx; #设置nginx服务的系统使用用户
worker_processes 1; #工作进程数(和cpu核心数保持一致)
error_log /var/log/nginx/error.log warn; #nginx的错误日志存放位置
pid /var/run/nginx.pid; #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"';
# nginx访问日志存放位置
access_log /var/log/nginx/access.log main;
# 是否隐藏版本号 on不隐藏,off隐藏
server_tokens off;
# 开启高效传输模式
sendfile on;
# 减少网络报文段的数量
#tcp_nopush on;
# 提高I/O性能
tcp_nodelay on;
# 保持连接的时间,也叫超时时间
keepalive_timeout 65;
# 读取客户端请求头数据的超时时间 默认60秒
client_header_timeout 15;
# 读取客户端请求主体的超时时间 默认60秒
client_body_timeout 15;
# 响应客户端的超时时间 默认60秒
send_timeout 25;
# 上传文件大小50M 默认1m
client_max_body_size 50m;
# 开启gzip压缩
gzip on;
# 开始压缩的最小长度(再小就不要压缩了,意义不在)
gzip_min_length 1k;
# 缓冲(压缩在内存中缓冲几块? 每块多大?)
gzip_buffers 4 8k;
# 开始压缩的http协议版本(可以不设置,目前几乎全是1.1协议)
gzip_http_version 1.1;
# 压缩级别([1-9]级别越高,压的越小,越浪费CPU计算资源)
gzip_comp_level 4;
# 对哪些类型的文件用压缩 如txt,xml,html ,css
gzip_types text/plain application/javascript application/x-javascript text/javascript text/css application/xml;
# 是否传输gzip压缩标志
gzip_vary on;
# 设置请求者代理服务器,该如何缓存内容
gzip_proxied expired no-cache no-store private auth;
#配置禁用gzip条件,支持正则。此处表示ie6及以下不启用gzip(因为ie低版本不支持)
gzip_disable "MSIE [1-6]\.";
# 包含的子配置项位置和文件
include /etc/nginx/conf.d/*.conf;
charset utf-8;
}
default.conf内容如下:
# 默认配置项
server {
listen 80;
listen [::]:80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
2 拉取nginx
docker pull nginx
3 创建并启动nginx容器,注意,$PWD这里为/root/nginx
docker run -id --name=c_nginx \
-p 80:80 \
-p 443:443 \
-v $PWD/nginx.conf:/etc/nginx/nginx.conf \
-v $PWD/conf.d:/etc/nginx/conf.d \
-v $PWD/logs:/var/log/nginx \
-v $PWD/html:/usr/share/nginx/html \
-v $PWD/ssl:/etc/ssl \
-v $PWD/static:/usr/local/src \
--restart=always
nginx