如何在 NGINX 中的代理响应中重写 URL [英] How do I rewrite URLs in a proxy response in NGINX

查看:46
本文介绍了如何在 NGINX 中的代理响应中重写 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我习惯于将 Apache 与 mod_proxy_html 一起使用,并且正在尝试使用 NGINX 实现类似的目标.具体用例是,我在 Tomcat 中的根上下文服务器上的端口 8080 上运行了一个管理 UI:

I'm used to using Apache with mod_proxy_html, and am trying to achieve something similar with NGINX. The specific use case is that I have an admin UI running in Tomcat on port 8080 on a server at the root context:

http://localhost:8080/

我需要在端口 80 上显示它,但我在此主机上运行的 NGINX 服务器上有其他上下文,因此想尝试在以下位置访问它:

I need to surface this on port 80, but I have other contexts on the NGINX server running on this host, so want to try and access this at:

http://localhost:80/admin/

我希望以下超级简单的服务器块可以做到,但它并不完全:

I was hoping that the following super simple server block would do it, but it doesn't quite:

server {
    listen  80;
    server_name screenly.local.akana.com;

    location /admin/ {
        proxy_pass http://localhost:8080/;
    }
}

问题是返回的内容 (html) 包含所有在根上下文中访问的脚本和样式信息的 URL,因此我需要重写这些 URL 以从/admin/而不是/开始.

The problem is that the returned content (html) contains URLs to scripts and style info that is all accessed at the root context, so I need to get these URLs rewritten to start with /admin/ instead of /.

如何在 NGINX 中执行此操作?

How do I do this in NGINX?

推荐答案

我们应该先阅读 有关 proxy_pass 的文档 仔细而完整.

We should first read the documentation on proxy_pass carefully and fully.

传递给上游服务器的 URI 是根据proxy_pass"指令是否与 URI 一起使用来确定的.proxy_pass 指令中的尾部斜杠表示 URI 存在且等于 /.没有尾部斜杠意味着没有帽子 URI.

The URI passed to upstream server is determined based on whether "proxy_pass" directive is used with URI or not. Trailing slash in proxy_pass directive means that URI is present and equal to /. Absense of trailing slash means hat URI is absent.

Proxy_pass with URI:

location /some_dir/ {
    proxy_pass http://some_server/;
}

有了上面的,有以下代理:

With the above, there's the following proxy:

http:// your_server/some_dir/ some_subdir/some_file ->
http:// some_server/          some_subdir/some_file

基本上,/some_dir/ 被替换为 / 以将请求路径从 /some_dir/some_subdir/some_file 更改为 /some_subdir/some_file.

Basically, /some_dir/ gets replaced by / to change the request path from /some_dir/some_subdir/some_file to /some_subdir/some_file.

没有 URI 的 Proxy_pass:

location /some_dir/ {
    proxy_pass http://some_server;
}

第二个(没有尾部斜线):代理是这样的:

With the second (no trailing slash): the proxy goes like this:

http:// your_server /some_dir/some_subdir/some_file ->
http:// some_server /some_dir/some_subdir/some_file

基本上,完整的原始请求路径无需更改即可传递.

Basically, the full original request path gets passed on without changes.

因此,在您的情况下,您似乎应该删除尾部斜杠以获得所需的内容.

So, in your case, it seems you should just drop the trailing slash to get what you want.

警告

请注意,只有在 proxy_pass 中不使用变量时,自动重写才有效.如果你使用变量,你应该自己重写:

Note that automatic rewrite only works if you don't use variables in proxy_pass. If you use variables, you should do rewrite yourself:

location /some_dir/ {
  rewrite    /some_dir/(.*) /$1 break;
  proxy_pass $upstream_server;
}

在其他情况下重写不起作用,这就是为什么必须阅读文档.

There are other cases where rewrite wouldn't work, that's why reading documentation is a must.

再次阅读您的问题,我似乎错过了您只想编辑 html 输出.

Reading your question again, it seems I may have missed that you just want to edit the html output.

为此,您可以使用 sub_filter 指令.像……

For that, you can use the sub_filter directive. Something like ...

location /admin/ {
    proxy_pass http://localhost:8080/;
    sub_filter "http://your_server/" "http://your_server/admin/";
    sub_filter_once off;
}

基本上就是你要替换的字符串和替换字符串

Basically, the string you want to replace and the replacement string

这篇关于如何在 NGINX 中的代理响应中重写 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆