Nginx没有正确地重定向到路径

时间:2018-04-16 12:22:26

标签: nginx

我有这个nginx配置

events {
    worker_connections  1024;
}

http {


    server {
        listen              443 ssl;
        server_name         192.168.52.89;
        ssl on;
        ssl_certificate     /path/config/certfile.crt;
        ssl_certificate_key /path/config/keyfile.key;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

        location /auth-foo {
            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_header       Set-Cookie;
            proxy_pass http://192.168.52.89:2085;
        }

        location / {
            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_header       Set-Cookie;
            proxy_pass http://192.168.51.90:3000;
        }
    }
}

我已经多次查看过这个文件,并在线查看资源,但不知怎的,这仍然无效。我希望当请求发出时:192.168.52.89/auth-foo nginx应该将请求推送到http://192.168.52.89:2085,但我找不到。有什么我想念的吗?

我错过了重要的事情吗?

谢谢。

1 个答案:

答案 0 :(得分:1)

尝试以下方法:

server {
    listen              443 ssl;
    server_name         192.168.52.89;
    ssl on;
    ssl_certificate     /path/config/certfile.crt;
    ssl_certificate_key /path/config/keyfile.key;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

    location = /auth-foo {
        return 302 /auth-foo/;  # Note the trailing slash
    }

    location /auth-foo/ {  # Note the trailing slash
        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_header       Set-Cookie;
        proxy_pass http://192.168.52.89:2085/;  # Note the trailing slash
    }

注意额外的location,并注意3个地方的尾部斜杠(添加注释以突出显示)。