【问题标题】:Nginx is redirecting proxy_pass to root path automaticallyNginx 自动将 proxy_pass 重定向到根路径
【发布时间】:2022-02-07 04:32:31
【问题描述】:

我有两个 nextjs 应用在端口 3000 和 3001 上运行,我的 Nginx 配置如下

events {
    worker_connections  1024;
}

http {
    server {
        listen       8080;
        server_name  http://localhost:8080;
        proxy_buffering off;
        proxy_http_version 1.1;

        location / {
            proxy_pass    http://localhost:3000/;
            proxy_redirect off;
        }
        location ^~ /app {
            proxy_pass    http://localhost:3001/;
            proxy_redirect off;
        }
    }
}

NextJS 应用在 3000 端口上运行良好。

  1. 当我访问 localhost:8080 - 它按预期从端口 3000 加载应用程序。

但是通过 nginx URL 访问 3001 端口上的第二个应用是不可能的

  1. 当我访问 localhost:8080/app - 它从端口 3000 加载应用程序。
  2. 当我访问 localhost:8080/app/ 时,它会将 URL 更改为 localhost:8080 并从端口 3000 加载应用程序。
  3. 当我访问 localhost:8080/app/login - 它将 URL 更改为 localhost:8080/login 并加载 404,因为端口 3000 上的应用程序没有登录路由。

我在这个配置中有什么遗漏吗?

【问题讨论】:

    标签: nginx next.js nginx-ingress


    【解决方案1】:

    ^~ /app 块放在 / 块之前。较高的块具有优先权,/ 块就像...对于一切。

       location ^~ /app {
            proxy_pass    http://localhost:3001/;
            proxy_redirect off;
        }
    
       location / {
            proxy_pass    http://localhost:3000/;
            proxy_redirect off;
        }
       
    

    除 /app 之外的任何内容都将进入第二个块。

    【讨论】:

      猜你喜欢
      • 2019-10-04
      • 1970-01-01
      • 1970-01-01
      • 2017-02-04
      • 2019-12-11
      • 1970-01-01
      • 2020-06-28
      • 2016-04-12
      • 1970-01-01
      相关资源
      最近更新 更多