Docker 安装 Nginx
GuoJun 2020-07-24 Docker
# 安装nginx
- 下载nginx
docker pull nginx:latest
- 创建目录
mkdir /project/nginx/data
mkdir /project/nginx/config
启动nginx
docker run --detach \ --name web-nginx \ -p 443:443\ -p 80:80 \ -v /project/nginx/data:/usr/share/nginx/html:rw\ -v /project/nginx/config/nginx.conf:/etc/nginx/nginx.conf/:rw\ -v /project/nginx/config/conf.d/default.conf:/etc/nginx/conf.d/default.conf:rw\ -v /project/nginx/logs:/var/log/nginx/:rw\ -v /project/nginx/ssl:/ssl/:rw\ -d nginx:v1
nginx.conf文件
#运行nginx的用户 user nginx; #启动进程设置成和CPU数量相等 worker_processes 1; #全局错误日志及PID文件的位置 error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; #工作模式及连接数上限 events { #单个后台work进程最大并发数设置为1024 worker_connections 1024; } http { #设定mime类型 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压缩 #gzip on; include /etc/nginx/conf.d/*.conf; }
default.conf
server { listen 80; #侦听80端口,如果强制所有的访问都必须是HTTPs的,这行需要注销掉 listen 443 ssl; server_name www.gj.myylc.cn; #域名 # 增加ssl #ssl on; #如果强制HTTPs访问,这行要打开 ssl_certificate /ssl/server.crt; ssl_certificate_key /ssl/server.key; ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m; # 指定密码为openssl支持的格式 ssl_protocols SSLv2 SSLv3 TLSv1.2; ssl_ciphers HIGH:!aNULL:!MD5; # 密码加密方式 ssl_prefer_server_ciphers on; # 依赖SSLv3和TLSv1协议的服务器密码将优先于客户端密码 # 定义首页索引目录和名称 location / { root /usr/share/nginx/html; index index.html index.htm; } #重定向错误页面到 /50x.html error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } }