配置301转发服务器

提供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这里。
配置301转发服务器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

配置301转发服务器

目标服务器上确实框架flask

yum install python-pip -y
pip install flask

执行启动脚本继续报错
配置301转发服务器

通过测试是Werkzeug版本差别形成
python2.6 对应的Flask (1.0.1) ,Werkzeug (0.12)

配置301转发服务器

使用0.12
pip install Werkzeug==0.12
配置301转发服务器

配置301转发服务器

成功运行,好坑啊。程序不是在这台机器上写的。

配置301转发服务器

配置301转发服务器

相关文章
相关标签/搜索