安装部署手册
1. 配置网络拉取镜像
-
使用以下命令创建一个名为nginx_net的Docker网络:
docker network create nginx_net
-
然后使用以下命令拉取nginx镜像:
docker pull nginx
2. 配置docker-compose启动文件
-
进入工作目录,并创建一个名为nginx的文件夹,然后在该文件夹中创建一个名为nginx.yml的文件:
cd $DOCKER_HOME cd datas/ mkdir nginx vim nginx.yml
-
在nginx.yml文件中,可以配置nginx容器的相关参数,例如映射端口、挂载卷等。
vim nginx.conf
version: '3'
services:
nginx:
restart: always
privileged: true
image: nginx:latest
container_name: nginx
hostname: nginx
ports:
- '81:81'
volumes:
- /data/docker/datas/nginx/nginx.conf:/etc/nginx/nginx.conf
- /data/docker/datas/nginx/vhosts:/etc/nginx/conf.d
- /data/docker/datas/nginx/cert:/etc/nginx/cert
- /home/apps:/usr/share/nginx/html
- /data/docker/datas/nginx/logs:/var/log/nginx
environment:
- TZ=Asia/Shanghai
networks:
- nginx_net
networks:
nginx_net:
driver: bridge
3. 创建相关配置文件
-
在nginx文件夹中创建以下文件夹:
mkdir vhosts mkdir cert mkdir logs mkdir -p /home/apps/
-
这些文件夹用于存放NGINX配置文件、SSL证书和日志文件。
-
创建nginx.conf
#运行用户
user nginx;
#==worker进程数,通常设置等同于CPU数量,auto为自动检测
worker_processes auto;
worker_cpu_affinity auto;
#worker_processes 2;
#worker_cpu_affinity 0101 1010;
#==worker进程打开最大文件数,最好与ulimit -n的值保持一致
worker_rlimit_nofile 65535;
#全局错误日志
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
#==worker进程同时打开的最大连接数,可CPU*2048设置
worker_connections 20480;
#==告诉nginx收到一个新链接通知后接受尽可能多的链接
multi_accept on;
#==设置用于复用客户端线程的轮训方法
use epoll;
}
http {
#==优化磁盘IO设置,指定nginx是否调用sendfile函数来输出文件,普通应用设为on,下载等磁盘IO高的应用,可设为off
sendfile on;
#去掉版本号
server_tokens off;
#==给客户端分配keep-alive链接超时时间
keepalive_timeout 30;
#激活 TCP_CORK socket 选择
tcp_nopush on;
#防止网络阻塞,不过要包涵在keepalived参数才有效
tcp_nodelay on;
#媒体类型,include 只是一个在当前文件中包含另一个文件内容的指令。
include mime.types;
#默认媒体类型足够。
default_type application/octet-stream;
#default_type text/html;
#设置默认字符集
charset UTF-8;
#==设置nginx采用gzip压缩的形式发送数据,减少发送数据量,但会增加请求处理时间及CPU处理时间,需要权衡
gzip on;
#允许或禁止压缩基于请求和相应的响应流,any代表压缩所有请求
gzip_proxied any;
#==设置对数据启用压缩的最少字节数,如果请求小于2048字节则不压缩,会影响请求速度
gzip_min_length 2k;
#默认值是0,不管页面多大都进行压缩。
gzip_buffers 4 32k;
#==设置数据压缩等级,1-9之间,9最慢压缩比最大
gzip_comp_level 4;
#设置需要压缩的数据格式
gzip_types text/plain text/css text/xml text/javascript application/json application/x-javascript application/xml application/xml+rss video/mp4;
#varyheader支持,改选项可以让前端的缓存服务器缓存经过GZIP压缩的页面,例如用Squid缓存经过nginx压缩的数据
gzip_vary on;
#==开发缓存的同时也指定了缓存文件的最大数量,20s如果文件没有请求则删除缓存
open_file_cache max=10240 inactive=20s;
#==指多长时间检查一次缓存的有效信息
open_file_cache_valid 60s;
#==文件缓存最小的访问次数,只有访问超过3次的才会被缓存
open_file_cache_min_uses 3;
#当搜索一个文件时是否缓存错误信息
open_file_cache_errors on;
#设置请求头的超时时间。我们也可以把这个设置低些,如果超过这个时间没有发送任何数据,nginx将返回request time out的错误。
client_header_timeout 15;
#设置请求体的超时时间。我们也可以把这个设置低些,超过这个时间没有发送任何数据,和上面一样的错误提示。
client_body_timeout 30;
#告诉nginx关闭不响应的客户端连接。这将会释放那个客户端所占有的内存空间
reset_timedout_connection on;
#响应客户端超时时间,这个超时时间仅限于两个活动之间的时间,如果超过这个时间,客户端没有任何活动,nginx关闭连接。
send_timeout 20;
#==允许客户端请求的最大单文件字节数
client_max_body_size 8m;
#==冲区代理缓冲用户端请求的最大字节数
client_header_buffer_size 32k;
#指定连接到后端FastCGI的超时时间
fastcgi_connect_timeout 600;
#向FastCGI传送请求的超时时间
fastcgi_send_timeout 600;
#指定接收FastCGI应答的超时时间
fastcgi_read_timeout 600;
#指定读取FastCGI应答第一部分需要用多大的缓冲区,默认的缓冲区大小为。fastcgi_buffers指令中的每块大小,可以将这个值设置更小
fastcgi_buffer_size 64k;
#指定本地需要用多少和多大的缓冲区来缓冲FastCGI的应答请求,如果一个php脚本所产生的页面大小为256KB,那么会分配4个64KB的缓冲区来缓存,如果页面大小大于256KB,那么大于256KB的部分会缓存到fastcgi_temp_path指定的路径中,但是这并不是好方法,因为内存中的数据处理速度要快于磁盘。一般这个值应该为站点中php脚本所产生的页面大小的中间值,如果站点大部分脚本所产生的页面大小为256KB,那么可以把这个值设置为“8 32K”、“4 64k”等。
fastcgi_buffers 4 64k;
#建议设置为fastcgi_buffers的两倍,繁忙时候的buffer
fastcgi_busy_buffers_size 128k;
#在写入fastcgi_temp_path时将用多大的数据块,默认值是fastcgi_buffers的两倍,该数值设置小时若负载上来时可能报502BadGateway
fastcgi_temp_file_write_size 128k;
#这个指令指定是否传递4xx和5xx错误信息到客户端,或者允许nginx使用error_page处理错误信息。注:静态文件不存在会返回404页面,但是php页面则返回空白页!
fastcgi_intercept_errors on;
port_in_redirect off;
#proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_next_upstream error timeout invalid_header http_502 http_503 http_504;
#==nginx跟后端服务器连接超时时间(代理连接超时)
proxy_connect_timeout 300;
#==连接成功后,后端服务器响应时间(代理接收超时)
proxy_read_timeout 600;
#==后端服务器数据回传时间(代理发送超时)
proxy_send_timeout 300;
#==设置代理服务器(nginx)保存用户头信息的缓冲区大小
proxy_buffer_size 256k;
#==proxy_buffers缓冲区,网页平均在32k以下的设置
proxy_buffers 4 256k;
#==高负荷下缓冲大小(proxy_buffers*2)
proxy_busy_buffers_size 256k;
#==设定缓存文件夹大小,大于这个值,将从upstream服务器传
proxy_temp_file_write_size 256k;
log_format main '[$status] [$body_bytes_sent] $request_time $upstream_response_time "$http_x_forwarded_for" $upstream_addr $host $remote_addr $remote_user "$request" "$uri?$args" "$http_referer" [$time_local] "$http_user_agent" $upstream_cache_status "in: $http_cookie"';
#access_log logs/access.log main;
include "/etc/nginx/conf.d/*.conf";
#限制IP访问,参考:
#https://www.cnblogs.com/liujiacai/p/8249763.html
#https://blog.youkuaiyun.com/senlin1202/article/details/50974115
# allow 10.0.0.0/24;
# allow 10.0.0.0/16;
# allow 10.0.0.0/8;
# deny all;
#防网络爬虫
server{
#不记录不需要的日志
location ~ .*\.(js|jpg|JPG|jpeg|JPEG|css|bmp|gif|GIf)$ {
access_log off;
}
#这里可以写浏览器的名称,表示禁用某些浏览器的访问
if ($http_user_agent ~* "qihoobot|Baiduspider|Googlebot|Googlebot-Mobile|Googlebot-Image|Mediapartners-Google|Adsbot-Google|Feedfetcher-Google|Yahoo! Slurp|Yahoo! Slurp China|YoudaoBot|Sosospider|Sogou spider|Sogou web spider|MSNBot|ia_archiver|Tomato Bot")
{
return 403;
}
}
map $upstream_http_content_length $flag_cache_empty {
default 0;
0 1;
}
}
- 配置htpp相关conf
vim n-upstream.conf
upstream upstream_ss {
least_conn;
#快速重启和日志查看配置本应用端口信息serverPort:"6108"(多个端口用空格分隔)
server 192.168.0.111:6108;
check interval=3000 rise=2 fall=5 timeout=1000 type=http;
check_http_send "HEAD /schoolSL/sys/health/anonymous/heart HTTP/1.0\r\n\r\n";
check_http_expect_alive http_2xx http_3xx;
}
upstream upstream_wf {
least_conn;
#快速重启和日志查看配置本应用端口信息serverPort:"8009"(多个端口用空格分隔)
server 192.168.0.111:8009;
check interval=3000 rise=2 fall=5 timeout=1000 type=http;
check_http_send "HEAD /wfengine/sys/health/anonymous/heart HTTP/1.0\r\n\r\n";
check_http_expect_alive http_2xx http_3xx;
}
vim n-http.conf
server {
listen 81;
charset utf-8;
server_name 192.168.0.111,localhost;
port_in_redirect off;
#客户端发送超时时间
send_timeout 10s;
#==设置客户端请求header最大超时时间
client_header_timeout 5s;
client_body_timeout 5s;
# client_header_buffer_size 4k
large_client_header_buffers 4 8k;
# 设置请求体的大小
client_max_body_size 100M;
#gzip config
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_comp_level 9;
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
gzip_vary on;
gzip_disable "MSIE [1-6]\.";
#健康状态
location /ngStatus {
check_status;
access_log off;
#allow SOME.IP.ADD.RESS;
#deny all;
access_log off;
}
#日志分割
if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})") {
set $year $1;
set $month $2;
set $day $3;
}
access_log /var/log/nginx/access-$year-$month-$day.log main;
location /ss {
add_header Cache-Control "no-cache, no-store,private, no-store, max-age=0";
add_header Access-Control-Allow-Headers 'Cookie,Set-Cookie,x-requested-with,content-type';
add_header Access-Control-Allow-Origin $http_origin;
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS';
proxy_pass http://upstream_ss;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Bytes-Sent $body_bytes_sent;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Connection "";
proxy_http_version 1.1;
limit_except GET POST OPTIONS{
deny all;
}
}
location /ssadmin {
root /usr/share/nginx/html/ss;
index index.html;
proxy_http_version 1.1;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Connection "";
add_header Nginx-Cache $upstream_cache_status;
add_header Cache-Control "no-cache, no-store,private, no-store, max-age=0";
limit_except GET POST OPTIONS{
deny all;
}
}
}
4. 配置快捷路径
-
编辑/etc/profile文件,添加以下内容:
vim /etc/profile export NGINX_HOME=/data/docker/datas/nginx
-
然后保存并执行以下命令使配置生效:
source /etc/profile
5.使用YAML启动
- 执行yaml 启动Nginx
docker-compose -f $NGINX_HOME/nginx.yml up -d
- 赋权
chmod -R 777 $NGINX_HOME