异世界百科配置文件的例子

www.isekai.cn isekai.cn {
    encode gzip
    #设置证书
    tls /xxx/isekai.pem /xxx/isekai.key
    #判定条件:是否要重定向到标准域名
    @should_redirect_index {
        not host www.isekai.cn
        not host static-www.isekai.cn
    }
    #判定条件:是否要重定向到https
    @can_redirect_https {
        protocol http
        not header User-Agent *CacheBot*
        not path /api/uc.php*
    }
    #路由内容,如果有一个已经执行,就不会继续执行下一个
    route {
        #重定向到指定页面
        redir @should_redirect_index https://www.isekai.cn{uri} permanent

        redir @can_redirect_https https://{host}{uri} permanent

        redir /IsekaiBook https://book.isekai.cn/ permanent
        #反向代理配置
        reverse_proxy {
            #upstream地址设定,如果是https要加上https://
            to localhost:114 localhost:514
            #使用第一个可以连接的地址,相当于把后面的当作backup
            lb_policy first
            #测试连通性的页面地址,必须设置,不然不会自动把down的主机从upstream列表中移除
            health_path /test.php
            #要传输给upstream的header
            header_up Host {host}
            header_up X-Real-IP {remote_host}
            header_up X-Forwarded-For {remote_host}
            header_up X-Forwarded-Proto {scheme}
            header_up X-NginX-Proxy true
            #编辑upstream返回的header,去掉server
            header_down -server
        }
    }
}

另:原先的nginx配置文件如下

upstream isekai_wiki {
	server localhost:114;
	server localhost:514 backup;
}

server {
	listen 80 default_server;
	listen 443 ssl http2 default_server;
	
	ssl_certificate /xxx/isekai.pem;
	ssl_certificate_key /xxx/isekai.key;

	server_name www.isekai.cn isekai.cn;

	location / {
		#轻小说
		rewrite ^/IsekaiBook$ https://book.isekai.cn/ permanent;
		# https跳转部分
		set $flag 0;
		if ($scheme = http) {
			set $flag "${flag}1";
		}
		
		if ($request_method = GET) {
			set $flag "${flag}2";
		}
		
		if ($http_user_agent !~ 'CacheBot') {
			set $flag "${flag}3";
		}
		
		if ($request_filename !~ 'api/uc\.php') {
			set $flag "${flag}4";
		}
		
		if ($flag = "01234"){
			rewrite ^/(.*)$ https://www.isekai.cn$request_uri permanent; 
		}
		
		# 域名不正确的跳转
		if ($http_host !~ '(www|static\-www)\.isekai\.cn') {
			rewrite ^/(.*)$ https://www.isekai.cn$request_uri permanent;
		}
		
		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;
		
		proxy_set_header Host www.isekai.cn;
		proxy_set_header X-NginX-Proxy true;
		proxy_redirect off;
		# Socket.IO Support
		proxy_http_version 1.1;
		proxy_set_header Upgrade $http_upgrade;
		proxy_set_header Connection "upgrade";
		
		proxy_pass http://isekai_wiki;
	}
	
	location /images/deleted {
		deny all;
	}
}

欢迎留言