Redirect Nginx中多个代理请求的Catch 404

Redirect Nginx中多个代理请求的Catch 404,redirect,nginx,error-handling,proxy,reverse-proxy,Redirect,Nginx,Error Handling,Proxy,Reverse Proxy,我在Debian Jessie上使用Nginx 1.10.3,运行一个具有以下设置的自部署应用程序 Loadbalancer配置: upstream www { server [fda4::0:21]:8080 weight=80; server [fda4::0:22]:8080 weight=100; keepalive 100; } server { listen 80; server_name example.com; location /

我在Debian Jessie上使用Nginx 1.10.3,运行一个具有以下设置的自部署应用程序

Loadbalancer配置:

upstream www {
    server [fda4::0:21]:8080 weight=80;
    server [fda4::0:22]:8080 weight=100;
    keepalive 100;
}
server {
    listen 80;
    server_name example.com;
    location / {
        rewrite ^/(.+)/+$ /$1 permanent;
        include /etc/nginx/proxy_params;
        proxy_set_header X-Forwarded-Host $host;
        proxy_pass http://www;
        proxy_next_upstream error timeout invalid_header http_500 http_502 http_503;
    }
}
后端配置:

server {
    listen [::]:8080 default_server backlog=1024;
    root /var/www/$host/web;
    index index.php;

    try_files $uri /index.php?$query_string;
    location ~ ^/index\.php$ {
        include fastcgi_params;
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_buffers 16 16k;
        fastcgi_buffer_size 32k;
        fastcgi_read_timeout 60s;
    }
}
我们有一个基于此设置的项目
新应用程序
,其中来自旧CMS的内容(称之为
旧应用程序
)应一直提供,直到迁移完成。我的考虑是:

  • 当在
    新应用
    中请求一个不存在的URL时,我们想看看
    旧应用
    是否有它的内容,因此
    新应用
    抛出一个404
  • Nginx捕获此404并将请求代理给
    旧应用程序
  • 旧应用程序中有内容时
    应与2xx/3xx响应代码一起提供
  • 如果
    旧应用程序中也没有内容
    ,则会抛出额外的404
  • Nginx应再次捕获该404,并应提供新应用程序中的静态错误页面
这可以用Nginx完成吗?我认为

proxy_intercept_errors on;
error_page 404 = @404;
location @404 {
    proxy_pass http://old-app.domain;
}

有人可以做到这一点,但当
旧应用程序
也抛出404时,这难道不会引发一个无休止的捕获和代理循环吗?在
旧应用
后端捕获404以提供静态错误页面时,干净的解决方案看起来像什么?

如果您总是想首先为
新应用
提供服务器,除非出现错误,您可以使用两个位置块来执行此操作:

upstream new-app {
    server [fda4::0:21]:8080 weight=80;
    server [fda4::0:22]:8080 weight=100;
    keepalive 100;
}

upstream old-app {
    server [fda4::0:21]:8081 weight=80;
    server [fda4::0:22]:8081 weight=100;
    keepalive 100;
}

server {
    listen 80;
    server_name example.com;
    recursive_error_pages on;

    location @old-app {
        rewrite ^/(.+)/+$ /$1 permanent;
        include /etc/nginx/proxy_params;
        proxy_set_header X-Forwarded-Host $host;
        proxy_pass http://old-app;
    }

    location / {
        rewrite ^/(.+)/+$ /$1 permanent;
        include /etc/nginx/proxy_params;
        proxy_set_header X-Forwarded-Host $host;
        proxy_pass http://new-app;
        proxy_intercept_errors on;
        error_page 404 = @old-app;
    }
}

当我想让
新应用程序
处理来自
旧应用程序
的404个异常时,我只需要在
位置@old app
中添加一个额外的
错误页面
指令?纠正、拦截错误并添加一个
错误页面
指令。听起来不错。我将在本周尝试一下,并在接下来的几天内给出反馈。好的,在一些失败之后,我找到了建议在上启用
recursive\u error\u页面的方法。在
服务器
基座上执行此操作后,现在一切正常。因此,将此添加到您的答案中,否则多个
错误\u页面
指令将不起作用,我将接受它作为正确答案。@rabudde我已经添加了它