修改Nginx默认的配置文件

# dict for host
lua_shared_dict _upstreams 1m;

resolver 114.114.114.114 8.8.8.8;

server {
    listen       80  default_server;
    server_name  _;

    location = /$/cfg/upstream {
        content_by_lua '
            local ups = ngx.req.get_uri_args()["backend"]
            if ups == nil then
                ngx.say("usage: curl /$/cfg/upstream?backend=nginx.org")
                return
            end

            schema_idx = string.find(ups, "://")
            if schema_idx == nil then
                ups = "http://" + ups
            end

            local host = ngx.var.http_host
            local ups_src = ngx.shared._upstreams:get(host)
            ngx.log(ngx.WARN, host, " change upstream from ", ups_src, " to ", ups)
            ngx.shared._upstreams:set(host,  ups)
            ngx.say(host, " change upstream from ", ups_src, " to ", ups)
        ';

    }

    location / {
        set_by_lua $backend '
            local ups = ngx.shared._upstreams:get(ngx.var.http_host)
            if ups ~= nil then
               ngx.log(ngx.ERR, "get [", ups,"] from ngx.shared")
               return ups
            end
            return ""
        ';


        proxy_redirect off;
        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_pass $backend;
        client_max_body_size 500m;
    }

    access_log /var/log/nginx/access.log;
}

更新upstream

curl http://yourdomain.com/$/cfg/upstream?backend=nginx.org

[ 编辑 | 历史 ]
最近由“jilili”在“2018-03-24 15:04:09”修改