How do I rewrite URLs in a proxy response in NGINX

前端 未结 3 1274
庸人自扰
庸人自扰 2020-11-28 19:42

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

3条回答
  •  隐瞒了意图╮
    2020-11-28 20:33

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

    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
    

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

    Proxy_pass without URI:

    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.


    Caveat

    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.


    Edit

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

    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

提交回复
热议问题