简体   繁体   English

Nginx 多后端一服务器

[英]Nginx multiple backend one server

I have a server without domain name: 10.23.33.251.我有一个没有域名的服务器:10.23.33.251。 On this server I have two backends (spring boot): 10.23.33.251:8888/apis/a/lot/of/apis 10.23.33.251:9999/apis/a/lot/of/apis在此服务器上,我有两个后端(弹簧启动): 10.23.33.251:8888/apis/a/lot/of/apis 10.23.33.251:9999/apis/a/lot/of/apis

Now I want to configure nginx to handle any request that:现在我想配置 nginx 来处理任何请求:

/be1/employees/1?very_long_params will be proxy_pass (or proxy_redirect) to 10.23.33.251:8888/apis/employees/1?very_long_params /be1/employees/1?very_long_params将是 proxy_pass(或 proxy_redirect)到10.23.33.251:8888/apis/employees/1?very_long_params

/be2/products/10/very/long/params will be proxy_pass (or proxy_redirect) to 10.23.33.251:9999/apis/products/10/very/long/params /be2/products/10/very/long/params将是 proxy_pass(或 proxy_redirect)到10.23.33.251:9999/apis/products/10/very/long/params

But I could not make it work.但我无法让它工作。 Here is my configuration:这是我的配置:

server {
    listen 8443;
    server_name localhost;

    location /be1/* {
        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_set_header        X-Forwarded-Proto $scheme;

        proxy_pass              http://localhost:8888/apis;
        proxy_read_timeout      90;
    }

    location /be2/* {
        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_set_header        X-Forwarded-Proto $scheme;

        proxy_pass              http://localhost:9999/apis;
    }
}

Your location s are defined wrong.您的location定义错误。 You're trying to use a regular expression but are missing the ~ (or ~* ) operator for that.您正在尝试使用正则表达式,但缺少~ (或~* )运算符。

It is however not necessary to use regular expressions, you could just use prefix locations:然而,没有必要使用正则表达式,您可以只使用前缀位置:

…
location /be1/ {
    …
}

location /be2/ {
    …
}

See also: Location Directive Documentation另请参阅: 位置指令文档

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

相关问题 Wildfly 用于后端,NGINX 用于前端 - Wildfly for backend and NGINX for frontend 如何使用带有 postgresql 和 jdbc 的 Spring Cloud 配置服务器作为具有多个配置文件的后端? - How to use spring cloud config server with postgresql and jdbc as backend with multiple profiles? 在一个请求中从 Spring Cloud Config Server 获取多个配置 - Fetching multiple configs from Spring Cloud Config Server in one request Spring Integration TCP客户端上的多个套接字到一个服务器地址 - Spring Integration TCP Multiple sockets on client side to one server address 带有 Azure KeyVault 后端的 Spring Cloud Config Server - Spring Cloud Config Server with Azure KeyVault backend 静态资源(图像)Tomcat或Nginx服务器 - Static resources (images) Tomcat or Nginx server 是否可以在后端使用多个框架(Spring boot + Django)? - Is it possible to use more than one framework at the backend(Spring boot + Django)? Spring JPA - 更新一个表正在更新后端中的另一个表 - Spring JPA - Updating one table is updating another table in the backend Spring Cloud 配置服务器未从 Git 后端拉取 - Spring cloud config server not pulling from Git backend Spring 云配置服务器 - 可以使用 Git 后端引用文件 - Spring Cloud Config Server - Possibility to reference files with Git backend
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM