gpt4 book ai didi

nginx 反向代理 - 如何为多个应用程序提供服务

转载 作者:行者123 更新时间:2023-12-05 03:51:31 24 4
gpt4 key购买 nike

我正在尝试使用 nginx 构建一个反向代理,使我的项目中的所有 Is 都可以从单个地址访问。对于单个服务,下面的配置没有问题

/etc/nginx/sites-enabled/reverse-proxy.conf

server {
listen 80;
listen [::]:80;
location / {

resolver 127.0.0.1;
allow "x.x.x.x";
deny all;
proxy_pass http://consul:8500;
}

}

因此,当我在浏览器中调用服务器的 ip x.x.x.x 时,我看到了 Consul UI 和显示 x.x.x.x/ui/dc1 的 URL。除此之外,我看到 UI 成功请求了 Assets 文件。

我的问题;是否有可能在同一台服务器上有两个托管不同的服务,只是在不同的位置引用它们?例如,如果我想包含 Vault UI,那么我会考虑这样做:

server {
listen 80;
listen [::]:80;
location /consul {

resolver 127.0.0.1;
allow "x.x.x.x";
deny all;
proxy_pass http://consul:8500;
}

location /vault {

resolver 127.0.0.1;
allow "x.x.x.x";
deny all;
proxy_pass http://vault:8200;
}

}

但是我不确定这是否可以通过这种方式完成。我得到的最多的是打开 Consul UI,但未找到所有其他子请求(即加载 Assets )。


更新

我认为我的问题是我错误地使用了 locationproxy_pass

观察第一个配置(正在运行)

server {
listen 80;
listen [::]:80;
location / {

resolver 127.0.0.1;
allow "x.x.x.x";
deny all;
proxy_pass http://consul:8500;
}

}

如果我查看 curl 命令 curl localhost -L -vvvv

*   Trying 127.0.0.1:80...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 80 (#0)
> GET / HTTP/1.1
> Host: localhost
> User-Agent: curl/7.68.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 301 Moved Permanently
< Server: nginx/1.18.0 (Ubuntu)
< Date: Fri, 10 Jul 2020 16:24:38 GMT
< Content-Type: text/html; charset=utf-8
< Content-Length: 39
< Connection: keep-alive
< Location: /ui/
<
* Ignoring the response-body
* Connection #0 to host localhost left intact
* Issue another request to this URL: 'http://localhost/ui/'
* Found bundle for host localhost: 0x557b754549e0 [serially]
* Can not multiplex, even if we wanted to!
* Re-using existing connection! (#0) with host localhost
* Connected to localhost (127.0.0.1) port 80 (#0)
> GET /ui/ HTTP/1.1
> Host: localhost
> User-Agent: curl/7.68.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Server: nginx/1.18.0 (Ubuntu)
< Date: Fri, 10 Jul 2020 16:24:38 GMT
< Content-Type: text/html; charset=utf-8
< Content-Length: 7806
< Connection: keep-alive
< Accept-Ranges: bytes
< Last-Modified: Fri, 10 Jul 2020 07:37:44 GMT
<
<!DOCTYPE html>
<html lang="en" class="ember-loading">
...

我已经可以看到 html 了。但是,如果我将 conf 文件更改为:

server {
listen 80;
listen [::]:80;
location /consul/ {

resolver 127.0.0.1;
allow "x.x.x.x";
deny all;
proxy_pass http://consul:8500;
}

}

然后尝试像 curl localhost/consul -L -vvvv 那样调用它,我得到以下信息:

*   Trying 127.0.0.1:80...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 80 (#0)
> GET /consul HTTP/1.1
> Host: localhost
> User-Agent: curl/7.68.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 301 Moved Permanently
< Server: nginx/1.18.0 (Ubuntu)
< Date: Fri, 10 Jul 2020 16:32:35 GMT
< Content-Type: text/html
< Content-Length: 178
< Location: http://localhost/consul/
< Connection: keep-alive
<
* Ignoring the response-body
* Connection #0 to host localhost left intact
* Issue another request to this URL: 'http://localhost/consul/'
* Found bundle for host localhost: 0x55ba7959f9e0 [serially]
* Can not multiplex, even if we wanted to!
* Re-using existing connection! (#0) with host localhost
* Connected to localhost (127.0.0.1) port 80 (#0)
> GET /consul/ HTTP/1.1
> Host: localhost
> User-Agent: curl/7.68.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 404 Not Found
< Server: nginx/1.18.0 (Ubuntu)
< Date: Fri, 10 Jul 2020 16:32:35 GMT
< Content-Length: 0
< Connection: keep-alive

如果有任何关于这个问题的想法,我将不胜感激

最佳答案

你是对的,你正在使用locationproxy_pass一个错误的方式。当您使用

location /vault {
proxy_pass http://vault:8200;
}

构造,您按原样将 URI 传递给上游,而您很可能想去除 /vault从它的前缀。为此,您应该使用这个:

location /vault/ {
proxy_pass http://vault:8200/;
}

你可以阅读更多关于第一个和第二个的区别here .但是,这仍然会阻止 Assets 正确加载。

这个问题——如何在一些 URI 前缀下代理一些 webapp——在 stackoverflow 上被一次又一次地问到。唯一正确的方法是让你的代理应用程序仅通过相对 URL 请求其 Assets (考虑 assets/script.js 而不是 /assets/script.js )或使用正确的前缀( /vault/assets/script.js )。一些编写良好的应用程序能够检测它们是否在这样的 URI 前缀下使用,并在生成 Assets 链接时使用它,一些应用程序允许通过某些设置指定它,但有些应用程序根本不适合这种用途.如果不满足这些要求,webapp 将无法运行的原因非常明显——任何不以 /vault 开头的 URL。与您的 location /vault/ { ... } 不匹配 block 并将通过 main location 提供服务阻止。因此,最好的方法是修复您的 Web 应用程序,但是如果您真的做不到,可以使用几种解决方法。

  • 一些网络框架已经使用相对 URL 构建了他们的网络应用程序,但使用了 <base href="/">index.html 的头部部分.例如,React 或 Angular 使用这种方法。如果您的 webapp 根目录中有这样一行 index.html , 只需将其更改为 <base href="/vault/"> .

  • 使用基于 HTTP 的条件路由 Referer header 值。这种方法对于用于加载 Assets 的单页应用程序非常有效,但如果 Web 应用程序包含多个页面,则这种方法将不起作用,正确的上游检测逻辑会在第一次从一个页面跳转到另一个页面后中断。这是一个例子:

    map $http_referer $prefix {
    ~https?://[^/]+/vault/ vault;
    # other webapps prefixes could be defined here
    # ...
    default base;
    }

    server {

    # listen port, server name and other global definitions here
    # ...

    location / {
    try_files "" @$prefix;
    }
    location /vault/ {
    # proxy request to the vault upstream, remove "/vault" part from the URI
    proxy_pass http://vault:8200/;
    }
    location @vault {
    # proxy request to the vault upstream, do not change the URI
    proxy_pass http://vault:8200;
    }
    location @base {
    # default "root" location
    proxy_pass http://consul:8500;
    }

    }

    更新@2022.02.19

    这是使用条件重写的另一种可能方法:

    server {

    # listen port, server name and other global definitions here
    # ...

    if ($http_referer ~ https?://[^/]+/vault/)
    # rewrite request URI only if it isn't already started with '/vault' prefix
    rewrite ^((?!/vault).*) /vault$1;
    }

    # locations here
    # ...

    }
  • 使用 sub_filter 重写响应正文中的链接来自 ngx_http_sub_module 的指令.这是最丑陋的一个,但仍然可以用作最后一个可用选项。这种方法具有明显的性能影响。重写模式应根据您的上游响应主体确定。通常那种类型的配置看起来像

    location /vault/ {
    proxy_pass http://vault:8200/;
    sub_filter_types text/css application/javascript;
    sub_filter_once off;
    sub_filter 'href="/' 'href="/vault/';
    sub_filter "href='/" "href='/vault/";
    sub_filter 'src="/' 'src="/vault/';
    sub_filter "src='/" "src='/vault/";
    sub_filter 'url("/' 'url("/vault/';
    sub_filter "url('/" "url('/vault/";
    sub_filter "url(/" "url(/vault/";
    }

更新@2022.02.19

ServerFault 的相关线程:How to handle relative urls correctly with a nginx reverse proxy .

使用 sub_filter 的可能注意事项在 JavaScript 代码上:Nginx as reverse proxy to two nodejs app on the same domain .

关于nginx 反向代理 - 如何为多个应用程序提供服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62836801/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com