当旧式服务器也有404时,在Nginx中返回原始的404响应

我有这个Nginxconfiguration大多数工作,我想要的方式,它:

  1. 试图从节点获取页面,
  2. 如果存在404,则尝试从不同主机上的旧版网站获取页面
  3. 如果网站的旧版本返回404,则显示来自新网站(而不是旧网站的404页面)的404页面。

注意:这是一个envsubst模板。

 location / { 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_set_header Host $http_host; proxy_redirect off; proxy_read_timeout 60; proxy_next_upstream error timeout http_500 http_502 http_503 http_504; proxy_buffers 16 64k; proxy_buffer_size 2k; proxy_temp_file_write_size 64k; proxy_pass http://nodejs; proxy_intercept_errors on; recursive_error_pages on; error_page 404 = @legacy; location /404 { # serve up /404 from the node server internal; proxy_intercept_errors off; recursive_error_pages off; proxy_pass http://nodejs; } } location @legacy { # when we can't find a page on the new server, try to find it on the # old web server. If still not found, show new site's 404 page. # NOTE: unfortunately, this can result in a 2nd call to our node # servers, even though we already have the response from them. proxy_pass ${LEGACY_URL}; proxy_redirect default; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_intercept_errors on; recursive_error_pages on; error_page 404 = /404; } 

问题是:

  1. 如果遗留网站上有404,则会向我的节点后端发出第二个请求。
  2. 那第二个请求不会有原始的path信息,它会有/ 404。

有什么办法让Nginx在这一点上返回原始的404节点响应呢?