#!/bin/bash echo "部署 LNMP 架构需要安装依赖包......." yum -y install make gcc gcc-c++ flex bison file libtool libtool-libs autoconf kernel-devel libjpeg libjpeg-devel libpng libpng-devel gd freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glib2 glib2-devel bzip2 bzip2-devel libevent ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5-devel libidn libidn-devel openssl openssl-devel gettext gettext-devel ncurses-devel gmp-devel unzip libcap lsof echo "安装epel扩展yum源......." yum install epel-release -y echo "安装nginx所需依赖包...." yum -y install gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre pcre-devel echo "正在创建nginx运行用户...." useradd -s /sbin/nologin -M -u 2000 nginx echo "正在下载cdn系统源码...." cd /usr/local/src echo "正在解压文件..." rm -rf nginx-* wget --no-check-certificate http://129.146.243.180:33001/shell/cdn_install/cdn-nginx.tar.gz && tar zxf cdn-nginx.tar.gz && rm -rf cdn-nginx.tar.gz echo "下载并解压缓存清除插件..." rm -rf ngx_cache_purge-* wget https://github.com/FRiCKLE/ngx_cache_purge/archive/2.3.tar.gz && tar zxf 2.3.tar.gz && rm -rf 2.3.tar.gz echo "下载并解压字符串替换插件..." rm -rf ngx_http_substitutions* wget https://github.com/yaoweibin/ngx_http_substitutions_filter_module/archive/v0.6.4.tar.gz && tar zxf v0.6.4.tar.gz && rm -rf v0.6.4.tar.gz echo "下载并解压头部参数设置插件..." rm -rf headers-more-nginx* wget https://github.com/openresty/headers-more-nginx-module/archive/v0.33.tar.gz && tar zxf v0.33.tar.gz && rm -rf v0.33.tar.gz echo "进入源码目录编译安装..." cd /usr/local/src/nginx-* bash ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_v2_module --with-http_ssl_module --with-http_realip_module --with-http_gzip_static_module --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module --with-pcre --add-module=/usr/local/src/ngx_cache_purge-2.3 --add-module=/usr/local/src/ngx_http_substitutions_filter_module-0.6.4 --add-module=/usr/local/src/headers-more-nginx-module-0.33 sleep 5 make && make install sleep 5 cd /root echo "开放防火墙端口" firewall-cmd --zone=public --add-port=80/tcp --permanent firewall-cmd --zone=public --add-port=443/tcp --permanent firewall-cmd --reload echo "修改环境变量" echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh chmod +x /etc/profile.d/nginx.sh sleep 2 source /etc/profile echo "创建所需文件夹" mkdir -p /usr/local/nginx/ssl mkdir -p /usr/local/nginx/tmp ##IP read -p "请输入您的网站域名(不带http://或者https://):" website read -p "请输入您的源网站IP:" websiteip read -p "请输入您的源网站端口:" websiteport read -p "请输入您的CDN节点名称:" cdnname read -p "请输入您的缓存磁盘大小带单位G:" disk echo "$websiteip $website" >> /etc/hosts mkdir -p /usr/local/nginx/cache mkdir -p /usr/local/nginx/conf/vhosts echo "正在上传ssl证书" cd /usr/local/nginx/ssl wget http://129.146.243.180:33001/shell/cdn_install/greekclub.net.crt wget http://129.146.243.180:33001/shell/cdn_install/greekclub.net.key chmod -R 755 /usr/local/nginx/ssl/$website.crt chmod -R 755 /usr/local/nginx/ssl/$website.key echo "替换原有nginx.conf文件" cd /usr/local/nginx/conf echo 'user nginx; worker_processes 1; events { use epoll; worker_connections 51200; multi_accept on; } http { include mime.types; default_type application/octet-stream; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; #gzip压缩设置 gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.1; gzip_comp_level 6; gzip_types text/plain application/javascript application/x-javascript text/javascript text/css application/xml application/x-ico application/font-woff font/ttf image/svg+xml application/vnd.ms-fontobject; gzip_vary on; gzip_disable "MSIE [1-6]\."; server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } } include vhosts/*.conf; } ' > /usr/local/nginx/conf/nginx.conf echo "下载vhosts配置文件" cd /usr/local/nginx/conf/vhosts echo "proxy_cache_path /usr/local/nginx/cache levels=1:2 keys_zone=cdn-cache:50m max_size=$disk inactive=1d; proxy_temp_path /usr/local/nginx/tmp; #负载均衡 upstream node { server $website:$websiteport; } server { listen 80; listen 443 ssl http2; server_name $website; #SSL 配置 ssl_certificate /usr/local/nginx/ssl/$website.crt; ssl_certificate_key /usr/local/nginx/ssl/$website.key; ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3; #指定SSL服务器端支持的协议版本 ssl_ciphers 'TLS13-AES-256-GCM-SHA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES-128-GCM-SHA256:TLS13-AES-128-CCM-8-SHA256:TLS13-AES-128-CCM-SHA256:EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+ECDSA+AES128:EECDH+aRSA+AES128:RSA+AES128:EECDH+ECDSA+AES256:EECDH+aRSA+AES256:RSA+AES256:EECDH+ECDSA+3DES:EECDH+aRSA+3DES:RSA+3DES:!MD5'; ssl_prefer_server_ciphers on; #在使用SSLv3和TLS协议时指定服务器的加密算法要优先于客户端的加密算法 ssl_session_cache shared:SSL:10m; ssl_session_timeout 10m;" >> /usr/local/nginx/conf/vhosts/$website.conf echo ' # SSL 跳转。 if ($server_port !~ 443){ rewrite ^(/.*)$ https://$host$1 permanent; }' >> /usr/local/nginx/conf/vhosts/$website.conf echo " # 添加响应头信息。 more_set_headers \"CDN-Node: $cdnname\";" >> /usr/local/nginx/conf/vhosts/$website.conf echo " location ~ /clear(/.*) { #include allowip.conf; #deny all; proxy_cache_purge cdn-cache \$host\$1\$is_args\$args; error_page 405 =200 /clear\$1; }" >> /usr/local/nginx/conf/vhosts/$website.conf echo '###自定义错误页面 location /error.html { root /usr/local/nginx/html; } #必须要添加:proxy_intercept_errors on; 如果这个选项没有设置,即使创建了404.html和配置了error_page也没有效果。 proxy_intercept_errors on; error_page 403 404 502 500 /error.html; # 反代配置正式开始。 location ~ \.(asp|aspx|tar|rar|zip|gz)$ { return 404; } #禁止访问admin目录 location ~ /admin(/.*) { return 404; } ##缓存静态文件 location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css|ico|woff|woff2|ttf|eot|svg)$ { proxy_pass https://node; proxy_redirect off; # 向源站传送主机头、客户端真实 IP 等特定信息。 proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme;' >> /usr/local/nginx/conf/vhosts/$website.conf echo " #proxy_set_header Accept-Encoding ''; #我们使用proxy_hide_header来屏蔽后端主机信息 proxy_hide_header X-Powered-By; # 忽略源站响应的 Cache-Control 头信息 proxy_ignore_headers Expires; proxy_ignore_headers Cache-Control; # 与源站建立连接的超时时间,通常不要超过 75s。 proxy_connect_timeout 60; # 与源站通信时节点发送请求的超时时间,超时只在两次连续的写入操作之间作用,而不是用于传输整个请求,如果源站在此时间内没有收到任何内容,则连接将关闭。 proxy_send_timeout 60; # 与源站通信时源站响应数据的超时时间,超时只在两次连续的读操作之间起作用,而不是用于传输整个响应,如果源站在此时间内没有传输任何内容,则连接将关闭。 proxy_read_timeout 60; # 开启代理缓冲区。 proxy_buffering on; # 响应头的缓冲区大小 proxy_buffer_size 128k; # 网页内容缓冲区大小(4*256k)。 proxy_buffers 4 256k; # Nginx 会在没有完全读完后端响应的时候就开始向客户端传送数据,所以它会划出一部分缓冲区来专门向客户端传送数据,然后它继续从后端取数据,缓冲区满了之后就写到磁盘的临时文件中。 proxy_busy_buffers_size 512k; # 指定当响应内容大于 proxy_buffers 指定的缓冲区时,写入硬盘的临时文件的大小 proxy_max_temp_file_size 128m; # 一次访问能写入的临时文件的大小。 proxy_temp_file_write_size 512k; # 源站返回错误信息自动重试 proxy_next_upstream error timeout invalid_header http_500 http_503; # 若后端返回错误则返回已缓存的。 proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504; # 指定缓存使用的空间。 proxy_cache cdn-cache; # 缓存支持的方法。 proxy_cache_methods GET HEAD; # 添加 If-Modified-Since 头信息,如果客户端的请求项已经被缓存过了 proxy_cache_revalidate on; # 多个 MISS 只有第一个会连接服务器。 proxy_cache_lock on;" >> /usr/local/nginx/conf/vhosts/$website.conf echo ' #客户端设置 Pragma:no-cache 时节点应当直接请求源站,添加此配置可使 Nginx 支持该请求(默认忽略) proxy_cache_bypass $http_pragma $cookie_nocache; # 指定对 200、301 或者 302 等有效代码缓存的时间长度,特定参数 any 表示对任何响应都缓存一定时间长度。 proxy_cache_valid 200 302 304 24h; proxy_cache_valid 301 24h; proxy_cache_valid 403 1h; proxy_cache_valid any 1s; # 指定缓存使用的 key 值,方便定位清除缓存 proxy_cache_key $host$uri$is_args$args; # 响应头信息添加当前请求资源的缓存状态,如命中 HIT、没有缓存 MISS、过期 UPDATING 等。 add_header CDN-Cache $upstream_cache_status; # 指定请求多少次才开始缓存。 proxy_cache_min_uses 1; # 统一添加请求的资源过期时间头信息 add_header Strict-Transport-Security "max-age=10800; includeSubDomains;preload" always; add_header X-Frame-Options DENY; add_header X-Xss-Protection 1; expires 3h; } location / { proxy_pass https://node; proxy_redirect off; # 向源站传送主机头、客户端真实 IP 等特定信息。 proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme;' >> /usr/local/nginx/conf/vhosts/$website.conf echo " #proxy_set_header Accept-Encoding '';" >> /usr/local/nginx/conf/vhosts/$website.conf echo ' #我们使用proxy_hide_header来屏蔽后端主机信息 proxy_hide_header X-Powered-By; # 忽略源站响应的 Cache-Control 头信息 proxy_ignore_headers Expires; proxy_ignore_headers Cache-Control; proxy_connect_timeout 60; proxy_send_timeout 60; proxy_read_timeout 60; ##缓冲区 proxy_buffer_size 128k; proxy_buffers 4 256k; proxy_busy_buffers_size 512k; proxy_temp_file_write_size 512k; proxy_max_temp_file_size 128m; # 一次访问能写入的临时文件的大小。 #安全防护 add_header Strict-Transport-Security "max-age=10800; includeSubDomains;preload" always; add_header X-Frame-Options DENY; add_header X-Xss-Protection 1; } }' >> /usr/local/nginx/conf/vhosts/$website.conf chmod -R 644 /usr/local/nginx/conf/vhosts/$website.conf echo "下载nginx网页..." cd /usr/local/nginx/html rm -rf 50x.html error.html wget http://129.146.243.180:33001/shell/cdn_install/error.html chmod -R 644 /usr/local/nginx/html/error.html echo "Nginx相关命令: 启动nginx:nginx 关闭nginx:nginx -s stop 重载配置文件:nginx -s reload" sleep 2 rm -rf $0