#!/bin/bash echo "正在添加网站......." ##IP read -p "请输入您的网站域名(不带http://或者https://):" website read -p "请输入您的源网站IP:" websiteip read -p "请输入您的源网站端口:" websiteport read -p "请输入您的CDN节点名称:" cdnname echo "$websiteip $website" >> /etc/hosts echo "正在上传ssl证书" cd /usr/local/nginx/ssl wget http://129.146.243.180:33001/shell/cdn_install/img.mycdn.tk.crt wget http://129.146.243.180:33001/shell/cdn_install/img.mycdn.tk.key chmod -R 755 /usr/local/nginx/ssl/$website.crt chmod -R 755 /usr/local/nginx/ssl/$website.key echo "下载vhosts配置文件" cd /usr/local/nginx/conf/vhosts echo "#负载均衡 upstream img-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 http://img-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 http://img-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配置文件" nginx -s stop nginx sleep 2