本文共 4372 字,大约阅读时间需要 14 分钟。
1.搜索安装的 nginx 镜像
# docker search nginx
2.在docker hub 中选择合适的版本后进行 镜像拉取
# docker pull nginx:1.17.5
3.拉取完成后运行 nginx 容器
使用 xftp 上传静态页面到服务器的/usr/local/app/nginx/www 目录下
在usr/local/app/nginx/conf目录下新建配置文件nginx.conf:
user nginx;worker_processes 1;error_log /var/log/nginx/error.log warn;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;}
以配置文件的方式运行nginx:
/data/nignx/conf 挂载容器里面的配置,即nginx.conf/data/nignx/conf.d 挂载容器里面的子配置,即nginx.conf里面include的配置文件/data/nignx/logs 挂载容器里面的代理的日志文件/data/nignx/html 挂载容器里面的界面的访问docker run -di -p 80:80 -p 443:443 --name=nginx-web \-v /usr/local/app/nginx/www:/usr/share/nginx/html \-v /usr/local/app/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \-v /usr/local/app/nginx/conf.d:/etc/nginx/conf.d \-v /usr/local/app/nginx/logs:/var/log/nginx nginx:1.17.5# -d 后台运行# -i 交互方式运行# --name 自定义容器名称# -p 端口号映射 90 自定义为外部访问端口:80 为nginx容器对外暴露的端口# -v 目录挂载 冒号前为 外部目录,冒号后为 容器内目录;相当于外部目录中的内容会映射同步到容器内
4.访问运行好的容器
# ip:90 ip为当前服务器ip地址
5.进入到容器命令
# docker exec -it container-id/container-name /bin/bash# container-id 容器id# container-name 自定义容器名称
6.进入到容器的指定位置查看配置
# cd /etc/nginx/conf.d/可以看到默认的配置文件:# cat default.conf server { # 默认监听 80 端口 listen 80; # localhost 为外部访问该地址的域名 域名解析指向---> NGINX 配置文件所在服务器 server_name localhost; # 这里为本地代理,当外部访问 server_name 域名的时候 就会转到以下代理地址 #1ocation / { # proxy_pass http://192.168.0.243:8778; #} #charset koi8-r; #access_log /var/log/nginx/host.access.log main; # nginx 的默认访问文件夹为 root /usr/share/nginx/html # nginx 的默认访问页面为 index index.html index.htm location / { root /usr/share/nginx/html; index index.html index.htm; }}
nginx.conf扩展说明知识点:
user nginx;#nginx 进程数,建议按照cpu 数目来指定,一般为它的倍数。worker_processes 2;error_log /var/log/nginx/error.log warn;pid /var/run/nginx.pid;events { #单个后台worker process进程的最大并发链接数 worker_connections 1024;}http { #设定mime类型,类型由mime.type文件定义 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; #开启gzip压缩 gzip on; gzip_disable "MSIE [1-6]."; #设定请求缓冲 client_header_buffer_size 128k; large_client_header_buffers 4 128k; #sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件, #对于普通应用,必须设为 on, #如果用来进行下载等应用磁盘IO重负载应用,可设置为 off, #以平衡磁盘与网络I/O处理速度,降低系统的uptime. sendfile on; #用于设置客户端连接保持活动的超时时间,在超过这个时间之后服务器会关闭该链接。 #keepalive_timeout 0; keepalive_timeout 120; #允许客户端请求的最大单文件字节数 client_max_body_size 50m; #服务器名字的hash表大小 server_names_hash_bucket_size 128; #header中自定义变量时支持下划线 underscores_in_headers on; #down 表示当前的server暂时不参与负载 #weight 加权轮询权重,默认为1。weight越大,负载的权重就越大。 #backup 备用服务器, 当其他所有的非backup机器出现故障或者忙的时候,才会请求backup机器,因此这台机器的压力最轻。 #max_fails 允许请求失败的次数默认为1。当超过最大次数时,返回proxy_next_upstream 模块定义的错误 #fail_timeout max_fails次失败后,暂停的时间。 #apiServer不能用下划线,否则访问不到 upstream apiServer { server 172.17.0.1:18081 weight=10; server 172.17.0.1:28081 weight=10; } # http 转 https server{ listen 80; server_name www.xxxx.com 二级域名.xxxx.com; rewrite ^ https://$http_host$request_uri? permanent; } server{ listen 80; server_name www.xxxx.com; location / { root /html/xxxx; index index.html index.htm; } } server{ listen 80; server_name xx.xxxx.com; location / { try_files $uri $uri/ @router; root /html/xx; index index.html index.htm; } location /api { rewrite ^/api/?(.*)$ /$1 break; include uwsgi_params; proxy_pass http://apiServer; } proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }}
转载地址:http://aqxxi.baihongyu.com/