提供isp服務(wù)的都知道,托管用戶的網(wǎng)站,用戶需要將域名解析過來(lái),一般提供個(gè)別名地址給用戶解析。但是有的域名解析平臺(tái)不帶www的不能做cname解析,這時(shí)候需要告知用戶將不帶www的域名解析到特定的服務(wù)器上,這里稱為301重定向服務(wù)器。
301重定向服務(wù)器將不帶www的訪問永久從定向到www上。
1,環(huán)境準(zhǔn)備 我這里使用tengine,主配制文件為
user nginx;
worker_processes auto;
worker_cpu_affinity auto;
error_log logs/error.log info;
pid /var/run/nginx.pid;
worker_rlimit_nofile 65535;
events {
use epoll;
worker_connections 8192;
}
http {
server_tokens off;
gzip on;
include mime.types;
default_type application/octet-stream;
log_format proxy '$remote_addr $server_addr $remote_user [$time_local] $status $body_bytes_sent '
'$request_time "$request" "$http_referer" "$http_user_agent" "$request_body" '
'$upstream_addr $upstream_status $upstream_response_time $upstream_cache_status $http_host';
access_log logs/access.log proxy;
#cache
client_header_buffer_size 512k;
large_client_header_buffers 8 128k;
client_max_body_size 128m;
sendfile on;
tcp_nopush on;
keepalive_timeout 300;
#proxy
proxy_connect_timeout 60;
proxy_read_timeout 60;
proxy_send_timeout 60;
proxy_temp_path temp;
proxy_buffer_size 64k;
proxy_buffers 16 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 256k;
proxy_max_temp_file_size 0;
proxy_cache_path cache levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=3g;
proxy_ignore_client_abort on;
#naxsi
#include naxsi/naxsi_core.rules;
#waf
#lua_package_path "/usr/local/tengine/conf/waf/?.lua";
#lua_shared_dict limit 50m;
#init_by_lua_file /usr/local/tengine/conf/waf/init.lua;
#access_by_lua_file /usr/local/tengine/conf/waf/waf.lua;
#include vhost
include vhost/*.conf;
}
可以看到我們的配置是在vhost/*.conf這里。

那么這個(gè)配置如何生成呢。我寫了一個(gè)腳本
#!/bin/bash
#
# Description: This is sysytem optimization scripts about centos !
################################################################
# Author:tommy xiao
# QQ: 610000107
# Date: 2019.06.28
################################################################
for i in `cat /root/301/301list.txt`;do
cat >> /root/301/301.txt <<EOF
###${i}###
if (\$http_host ~ "^${i}\$") {
rewrite ^(.*) https://www.${i}\$1 permanent;
}
EOF
if [ $? -eq 0 ];then
echo -e "\033[40;32m"$i" Configuration Successful!!!\n\033[40;37m"
else
echo -e "\033[40;31m"$i" configuration failed!!!\n\033[40;37m"
fi
done
# Variable settings
conffile="/usr/local/tengine/conf/vhost/301.conf"
date=`date %Y%m%d%H%M%S`
/bin/mv $conffile $conffile.$date
cat > $conffile <<EOF
server {
}
EOF
sed -i '1 r /root/301/301.txt' /usr/local/tengine/conf/vhost/301.conf
if [ $? -eq 0 ];then
service nginx reload && rm -rf /root//301/301.txt
else
echo -e "\033[40;31mUpdate configuration failed!!!\n\033[40;37m"
exit 1
fi
1,將需要添加301重定向的域名一行一行的寫在/root/301/301list.txt 這個(gè)文件里 2,按照301重定向配置的格式將所有域名寫入到/root//301/301.txt文件里 3,重命名配置文件301.conf 4,重新生存新的配置文件301.conf,將server配置寫進(jìn)去
server {
}
5,將/root//301/301.txt里的所有內(nèi)容寫入到301.conf的server里 6,重啟服務(wù)器
編寫一個(gè)監(jiān)控腳本,監(jiān)控到301list.txt有新增域名,則重新生成行配置。
#!/bin/bash
md5new=`md5sum /root/301/301list.txt | awk '{print $1}'`
md5old=`cat /root/301/301.md5`
date=`date %Y%m%d%H%M%S`
if [ $md5new == $md5old ];then
echo "$md5new $date" >> /root/301/chmd5.txt
else
/bin/bash /root/301/301.sh
echo "$md5new" > /root/301/301.md5
fi
1,301list.txt這文件可以直接編輯添加 2,寫個(gè)程序,通過在頁(yè)面上輸入域名,點(diǎn)擊提交,然后更新到這個(gè)文件里去。添加一個(gè)更新按鈕,主動(dòng)去執(zhí)行這個(gè)監(jiān)測(cè)腳本。(這個(gè)寫好在貼上來(lái)) 來(lái)源:https://www./content-4-320351.html
|