提供isp服務的都知道,託管用戶的網站,用戶須要將域名解析過來,通常提供個別名地址給用戶解析。可是有的域名解析平臺不帶www的不能作cname解析,這時候須要告知用戶將不帶www的域名解析到特定的服務器上,這裏稱爲301重定向服務器。python
301重定向服務器將不帶www的訪問永久從定向到www上。nginx
1,環境準備
我這裏使用tengine,主配製文件爲flask
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這裏。
centos
那麼這個配置如何生成呢。我寫了一個腳本bash
#!/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' $conffile 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
這個文件裏
2,按照301重定向配置的格式將全部域名寫入到/root/301/301.txt文件裏
3,重命名配置文件301.conf
4,從新生存新的配置文件301.conf,將server配置寫進去服務器
server { }
5,將/root/301/301.txt裏的全部內容寫入到301.conf的server裏
6,重啓服務器app
編寫一個監控腳本,監控到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,寫個程序,經過在頁面上輸入域名,點擊提交,而後更新到這個文件裏去。添加一個更新按鈕,主動去執行這個監測腳本。(這個寫好在貼上來)tcp
執行啓動腳本python main.py &
ide
報錯以下:
File "main.py", line 3, in <module> from flask import Flask ImportError: No module named flask
目標服務器上確實框架flask
yum install python-pip -y
pip install flask
執行啓動腳本繼續報錯
通過測試是Werkzeug版本差別形成
python2.6 對應的Flask (1.0.1) ,Werkzeug (0.12)
使用0.12pip install Werkzeug==0.12
成功運行,好坑啊。程序不是在這臺機器上寫的。