1

给定以下 http 块,nginx 按预期执行。也就是说,它将重写一个诸如http://localhost/3ba48599-8be8-4326-8bd0-1ac6591c2041/to之类的URLhttp://localhost/modif/3ba48599-8be8-4326-8bd0-1ac6591c2041/并将其传递给uwsgi服务器。

http {    
    upstream frontend {
        server frontend:8000;
    }

    server {
        listen 8000;
        server_name localhost;

        root /www/;

        location ~* "^/([0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12})/?$" {
            include uwsgi_params;
            set $uuid $1;
            if ($cookie_admin) {
              # if cookie exists, rewrite /<uuid> to /modif/<uuid> and pass to uwsgi
              rewrite / /modif/$uuid break;
              uwsgi_pass frontend;
            }
            content_by_lua_block {
                ngx.say("Ping!  You got here because you have no cookies!")
            }
        }
    }
}

但是,当我以显示的方式添加另一个location块时,事情变得很明显,我得到了ERR_TOO_MANY_REDIRECTS

http {
    # access_log /dev/stdout;  # so we can `docker log` it.

    upstream frontend {
        server frontend:8000;
    }

    server {
        listen 8000;
        server_name localhost;

        root /www/;

        location / {  # THIS MAKES EVERYTHING FALL APART :(
            uwsgi_pass frontend;
            include uwsgi_params;
        }

        location ~* "^/([0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12})/?$" {
            include uwsgi_params;
            set $uuid $1;
            if ($cookie_admin) {
              # if cookie exists, rewrite /<uuid> to /modif/<uuid> and pass to uwsgi
              rewrite / /modif/$uuid break;
              uwsgi_pass frontend;
            }
            content_by_lua_block {
                ngx.say("Ping!  You got here because you have no cookies!")
            }
        }
    }
}

这里到底发生了什么?我怎样才能解决这个问题?

4

1 回答 1

0

我看到您的 Nginx 正在侦听端口 8000,但您的上游服务器位于“前端”,也在端口 8000 上。如果frontend解析到运行 Nginx 的同一台服务器,那么您就会发生代理请求循环。

于 2016-01-18T17:30:27.403 回答