简体   繁体   English

nginx反向代理:如何实现?

[英]nginx reverse proxy: How to implement?

I'm trying to do a reverse proxy with Nginx based on the URL. 我正在尝试使用基于URL的Nginx进行反向代理。 I want http://mydomain.example.com/client1/... to be redirected to http://127.0.0.1:8193/... . 我想将http://mydomain.example.com/client1/...重定向到http://127.0.0.1:8193/... I have tried many ways, and none of them worked. 我尝试了很多方法,但没有一个方法奏效。 Please note that the application can make redirections. 请注意,该应用程序可以进行重定向。 These are the configuration files of my last solution : 这些是我上一个解决方案的配置文件:

default 默认

server {
    listen                  80;
    server_name             mydomain.example.com;

    location / {
            set $instance none;
            if ($request_uri ~ ^/(.*)/$) {
                    set $instance $1;
            }

            set $no_cookie true;
            if ($http_cookie ~ "instance=([^;] +)(?:;|$)") {
                    set $instance $1;
                    set $no_cookie false;
            }

            if ($no_cookie = true) {
                    add_header Set-Cookie "instance=$instance;Domain=$host;Path=/";
                    rewrite ^ / break;
            }

            include instances.conf;
    }

instances.conf instances.conf

proxy_redirect                 off;
proxy_set_header               Host $host;
proxy_set_header               X-Real-IP $remote_addr;
proxy_set_header               X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_connect_timeout          90;
proxy_send_timeout             60;

# Installation of language packs, etc. can take a long time
proxy_read_timeout             10800;

if ($instance = client1) {
    proxy_pass http://127.0.0.1:8193;
}

if ($instance = client2) {
    proxy_pass http://127.0.0.1:8194
}

...

When the browser requests http://mydomain.example.com/client1/ , Nginx should set a cookie named instance with the value client1 then redirect the traffic to the appropriate proxy. 当浏览器请求http://mydomain.example.com/client1/ ,Nginx应设置一个名为instance的cookie,其值为client1然后将流量重定向到相应的代理。 For subsequent queries, it should use this cookie to make redirection. 对于后续查询,它应该使用此cookie进行重定向。 The problem I have is it never sets the $instance variable to client1 . client1的问题是它永远不会将$instance变量设置为client1 Don't forget that the application has no idea of the prefix /client1 . 不要忘记应用程序不知道前缀/client1

Do you have an idea? 你有想法吗? Do you know of a better solution? 你知道更好的解决方案吗?

The regex used to get the cookie was wrong. 用于获取cookie的正则表达式是错误的。 I have changed this to 我把它改成了

"instance=([^;][^ ]+)(?:;|$)"

and it works now. 它现在有效。

Edit: It's only a part of the solution finally. 编辑:它最终只是解决方案的一部分。 I'm sorry. 对不起。 There is still a problem. 还有一个问题。 See my comment below. 请参阅下面的评论。

It is not related to your problem but "proxy_connect_timeout" 它与您的问题无关,但“proxy_connect_timeout”

"This directive assigns a timeout for the connection to the upstream server. It is necessary to keep in mind that this time out cannot be more than 75 seconds." “该指令为上游服务器的连接分配了一个超时。有必要记住,这个超时时间不能超过75秒。”

See Nginx' map module 请参阅Nginx的地图模块

map $uri $proxy {
    /client1     http://127.0.0.1:8193/client1;
    /client2     http://127.0.0.1:8194/client2;
}

server {
    server_name my.domain.com;
    proxy_pass $proxy;
}

Note that appending /clientX to the end of the proxy_pass URI strips that portion of the URI from the request (which seems rational to me, but may not be what you want). 请注意,将/ clientX附加到proxy_pass URI的末尾会从请求中删除URI的那部分(这对我来说似乎是理性的,但可能不是您想要的)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM