简体   繁体   中英

Error page is not rendered for proxied requests

I have a following configuration

location ~ ^\/content\/images\/(?<entity_name>\w+)\/(?<image_type>\w+\/)?(?<image_size>\w+)\/(?<add_image_id>\d+--)?([\w\-]+_)?(?<image_id>\d+)\.(?<image_extension>jpg|jpeg|png|gif) {
    proxy_intercept_errors  on;
    error_page              404 @app-generate-image;

    proxy_http_version      1.1;
    proxy_set_header        Authorization "";
    proxy_buffering         off;

    proxy_cache cache;
    proxy_cache_revalidate on;
    proxy_cache_min_uses 3;
    proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
    proxy_cache_background_update on;
    proxy_cache_lock on;
    # pass request to minio
    proxy_pass http://127.0.0.1:9100/project/web/content/images/$entity_name/$image_type$image_size/$add_image_id$image_id.$image_extension;
}

location @app-generate-image {
    add_header "Access-Control-Allow-Origin" "";

    fastcgi_intercept_errors on;
    error_page 404 @storefront;

    fastcgi_pass php-upstream;
    include fastcgi_params;
    
    fastcgi_param DOCUMENT_ROOT $realpath_root;
    fastcgi_param SCRIPT_FILENAME $realpath_root/index.php;
    #fastcgi_param HTTPS $http_x_forwarded_proto;
    fastcgi_param HTTPS on;
    fastcgi_param HTTP_HOST $request_host;
}

location @storefront {
    internal;
    proxy_set_header Host $http_host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "Upgrade";
    proxy_pass http://storefront-upstream;
}

The main idea is – try to get an image from the proxied server (minio). If the image does not exist on the minio, pass the processing to the @app-generate-image (that app knows how to generate an image if possible – miniatures, different size, etc.).

When backend app cannot generate the image, the 404 is returned. In that case, processing should be sent to @storefront to display appropriate 404 page (storefront already knows how to do it).

But with this configuration, I get standard nginx 404 page, instead of the one rendered by the @storefont

When I remove fastcgi_intercept_errors on; from the @app-generate-image location, I get the error page rendered by the backend app. So it seems to me, like the error_page 404 @storefront; is ignored.

Strangely enough, when I replace proxying ( proxy_pass http://127.0.0.1:9100/project/web/content/images/$entity_name/$image_type$image_size/$add_image_id$image_id.$image_extension; ) with the try_files directive, it works.

Is it possible, that second error_page directive does not take effect? Is any different approach I should do to achieve this?

What I need is:

  • get file from minio, if exists
  • if file does not exist on minio, let backend app generate it
  • if backend app cannot generate image, show storefront 404 page (backend and storefront are different applications)

Thanks!

In the end I have used the X-Accel-Redirect .

I changed my app, so when app cannot generate the image, returns the 404 response with the header X-Accel-Redirect: @storefront . Nginx then use the @storefront location to render the 404 page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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