【问题标题】:NGINX - redirect proxied responseNGINX - 重定向代理响应
【发布时间】:2014-05-23 21:47:45
【问题描述】:

我的 nginx 在 8080 端口运行,它只有我的 html 和 javascript 文件,我在 8000 端口有另一个网络服务器,它基本上是一个 json 休息服务器。在我的 nginx.conf 文件中(在 8080 端口)我有这样的东西:

location /xyz {
   proxy_pass http://localhost:8000;
}

因此,当用户浏览器在 /xyz 路径中请求某些内容时,nginx 按预期将请求传递给我的服务器。现在我想做的是当服务器返回 http_response 时将该响应重定向到另一个 url。有可能吗?

【问题讨论】:

    标签: nginx proxypass


    【解决方案1】:

    是的。在端口 8000 json rest 服务器中添加重定向,它会将用户重定向到您希望他们访问的 url。

    更新:

    您还可以return 来自 REST 服务器的重定向。例如:

    location / {
        return 302 http://myotherserver.com;
    }
    

    停止处理并将指定的代码返回给客户端。非标准代码 444 关闭连接而不发送响应头。

    syntax:     return code [text];
                return code URL;
                return URL;
    default:    —
    context:    server, location, if
    

    来源:http://nginx.org/en/docs/http/ngx_http_rewrite_module.html#return

    【讨论】:

    • 好的,但是在我的网络应用程序的 nginx 中没有办法做到这一点?
    • 是的。只需使用return 指令。请查看更新后的答案。
    • 嗯,return 指令在 proxy_pass 中不起作用,因为我的想法是做这样的事情:location / { proxy_pass http://myrestserver.com; return 302 http://frontendserver.com/some_page.html; } 所以经过一些研究,我发现我想做的事情这种方法:location / { post_action @post; return 302 http://frontendserver.com/some_page.html; }location @post { proxy_pass http://myrestserver.com; } 但是即使这种方法也存在响应cookie没有重定向到前端服务器的问题。
    猜你喜欢
    • 1970-01-01
    • 2018-02-20
    • 2019-03-12
    • 2015-11-29
    • 1970-01-01
    • 1970-01-01
    • 2012-01-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多