Nginx反向代理具有不同的上下文路径 [英] Nginx reverse proxy with different context path

查看:217
本文介绍了Nginx反向代理具有不同的上下文路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用nginx反向代理同一主机/端口上的多个Web应用程序,并使用不同的路径来区分应用程序.

I'm trying to use nginx to reverse proxy multiple web applications on the same host / port, using a different path to distinguish between applications.

我的nginx配置如下所示:

My nginx config looks like the following:

proxy_set_header Host $http_host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $proxy_connection;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $proxy_x_forwarded_proto;
proxy_set_header X-Forwarded-Ssl $proxy_x_forwarded_ssl;
proxy_set_header X-Forwarded-Port $proxy_x_forwarded_port;

upstream app1 {
    server 192.168.0.1:8080;
}
upstream app2 {
    server 192.168.0.2:8080;
}

server {
    server_name my-application-server;
    listen 80;

    location /app1/ {
        proxy_pass http://app1/;
    }
    location /app2/ {
        proxy_pass http://app2/;
    }

}

这可以正确地代理对我的应用上的各个页面的任何请求-例如http://my-application-server/app1/context/login,但我的应用程序中的所有超链接均已损坏,因为它们缺少路径的app1部分-例如他们将我定向到http://my-application-server/context/login-success而不是http://my-application-server/app1/context/login-success.

This correctly proxies any requests to individual pages on my app - e.g. http://my-application-server/app1/context/login, but any hyperlinks in my app are broken because they're missing the app1 part of the path - e.g. they direct me to http://my-application-server/context/login-success rather than http://my-application-server/app1/context/login-success.

我尝试为proxy_redirectrewrite添加各种值,但是我什么也不能说服这些链接正确呈现.

I've tried adding in various values for proxy_redirect and rewrite, but nothing I do can convince these links to be rendered correctly.

我的应用程序是在tomcat中运行的Java Web应用程序,如果有区别的话.我已经看到了其他解决方案,可以在其中更改我的Web应用程序的上下文路径,但是我需要nginx透明地代理请求,而不必将tomcat配置为了解nginx路径.

My app is a java webapp running in tomcat, if that makes any difference. I've seen other solutions where I can change the context path of my webapp, but I need nginx to transparently proxy the requests without the tomcat having to be configured to know about the nginx path.

推荐答案

首先,没有什么比将后端从根域透明地代理到具有附加基本URL的域更容易了.

Well first of all there is nothing like transparently proxying a backend from a root domain to a domain with a added base url.

如果要将http://xyz/abc代理到http://def,则无法百分百保证一切正常.您需要特定于应用程序的更改

If you want to proxy http://xyz/abc to http://def then there is no way to have a 100% guarantee to have everything work. You need application specific changes

如果您的后端API不能返回访问当前URL的URL,那么您就不必担心proxy_pass.但是,如果您使用的是html,则需要修复所有问题.

If you backend API is something which doesn't return url accessing current url then you don't need to worry about the proxy_pass. But if you have a html then you need to fix everything that comes your way.

查看我为泛滥后端创建的简单配置

See a simple config I created for deluge backend

如何将调用代理到特定URL用NGINX泛滥?

您可以做所有的sub_filter来修复CSS,JavaScript和HTML中的URL.而且我必须运行它,查找问题,然后实施修复程序.以下是供您参考的配置

As you can all the sub_filter were done to fix urls in CSS, JavaScript and HTML. And I had to run it, find issues and then implement fixes. Below is the config for your reference

location ~* /deluge/(.*) {
    sub_filter_once off;
    sub_filter_types text/css;
    sub_filter '"base": "/"' '"base": "/deluge/"';
    sub_filter '<head>' '<head>\n<base href="/deluge/">';
    sub_filter 'src="/' 'src="./';
    sub_filter 'href="/' 'href="./';
    sub_filter 'url("/' 'url("./';
    sub_filter 'url(\'/' 'url(\'./';

    set $deluge_host 192.168.33.100;
    set $deluge_port 32770;
    proxy_pass http://$deluge_host:$deluge_port/$1;
    proxy_cookie_domain $deluge_host $host;
    proxy_cookie_path / /deluge/;
    proxy_redirect  http://$deluge_host:$deluge_port/ /deluge/;
}

您可以根据您的应用程序自定义以上内容.但是下面是您需要的东西

You can customize the above based on your app. But below is what you would need

location /app1/ {
    sub_filter_once off;
    sub_filter '<head>' '<head>\n<base href="/app1/">';
    sub_filter 'src="/' 'src="./';
    sub_filter 'href="/' 'href="./';
}

这篇关于Nginx反向代理具有不同的上下文路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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