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

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

问题描述

我习惯将Apache与mod_proxy_html一起使用,并且试图通过NGINX实现类似的功能.特定的用例是,我在根上下文的服务器上的8080端口上的Tomcat中运行了一个管理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都可以在根上下文中访问,因此我需要重写这些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"指令与URI一起使用来确定传递给上游服务器的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.

具有URI的Proxy_pass :

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天全站免登陆