AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 1015934
Accepted
user2586441
user2586441
Asked: 2020-05-07 05:42:49 +0800 CST2020-05-07 05:42:49 +0800 CST 2020-05-07 05:42:49 +0800 CST

带有嵌套代理的 Nginx 自定义 502 页面

  • 772

我知道如何为上游错误创建自定义 502 页面,并且多次没有问题,但是在这种特殊情况下,一个代理响应被传递给另一个代理,我找不到正确的配置。

一般的工作流程是这样的:用户请求图片的缩略图,nginx 将此请求代理到图片存储。如果 storage 返回 404,则 nginx 将此请求代理到应用服务器,应用服务器生成带有请求 URI 的缩略图并返回代码 200,通过 nginx 传递给用户。

但是,如果用户请求完全不存在图片的缩略图,则应用服务器返回 502。在这种情况下,我想显示一个自定义页面,而不是内置的 nginx“502 Bad Gateway”。

正如我在开始时提到的,我知道如何创建自定义 502 页面,但我认为这种具有双重代理的特殊复杂设置需要一些我找不到的额外配置 :( 我尝试了一个带有内部选项的位置,我我尝试了一个带有指向 html 页面的直接 URL 的位置,我尝试了“@”位置,但 nginx 总是显示其内置页面。文件 502.html 和 502x.html 都存在于 /var/www/html 中并且是可读的通过 nginx:

ll /var/www/html
total 20
drwxr-xr-x 2 root root 4096 May  3 11:21 ./
drwxr-xr-x 4 root root 4096 Mar 24 11:50 ../
-rw-r--r-- 1 root root   36 Apr 28 10:49 502.html
-rw-r--r-- 1 root root   36 May  3 11:21 502x.html

Nginx 配置:

server {
listen 443 ssl http2;
server_name cdn.domain.tld;

ssl_certificate /etc/letsencrypt/live/cdn.domain.tld/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/cdn.domain.tld/privkey.pem;

client_max_body_size 10m;

client_body_buffer_size 128k;
proxy_connect_timeout 75;
proxy_send_timeout 300;
proxy_read_timeout 500;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
proxy_http_version 1.1;
proxy_buffering on;
proxy_redirect off;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;

#locations for redirection of 502 response received from thumbnail generator. Not working :(
#error_page 502  @502;
#error_page 502  https://cdn.domain.tld/502x.html;
error_page 502  /502.html;
location =/502.html {
   root /var/www/html;
   internal;
}
location @502 {
   root /var/www/html;
   try_files /502x.html 502;
}
location = /502x.html {
   root /var/www/html;
   index 502x.html;
}

location  / {
    proxy_pass http://192.168.10.5/thumbs$request_uri;

    #Sets caching time for different response codes. If only caching time is specified  then only 200, 301, and 302 responses are cached.
    proxy_cache_valid 30m;

    add_header X-Proxy-Thumbs-Cache $upstream_cache_status;

    #intercept 404 from backend
    #from nginx docs:
    #If an error response is processed by a proxied server or a FastCGI/uwsgi/SCGI/gRPC server,.
    #and the server may return different response codes (e.g., 200, 302, 401 or 404), it is possible to respond with the code it returns (equal sign does that)
    #So we proxy 404 to app which generates thumbnail and returns it with 200 code
    proxy_intercept_errors on;
    error_page 404 = @not-found;

    access_log /var/log/nginx/cdn.access.log cachelog;
    error_log /var/log/nginx/cdn.error.log;
}

location @not-found {
   proxy_pass http://192.168.10.12/thumbgen?key=$request_uri;

   add_header X-Proxy-Thumbs-Cache2 $upstream_cache_status;

   proxy_intercept_errors on;

   access_log /var/log/nginx/cdn-404.access.log cachelog;
   error_log /var/log/nginx/cdn-404.error.log;
}

}

nginx 502-error custom-errors
  • 1 1 个回答
  • 560 Views

1 个回答

  • Voted
  1. Best Answer
    user2586441
    2020-05-08T08:14:54+08:002020-05-08T08:14:54+08:00

    我在这个线程中找到了答案:http: //mailman.nginx.org/pipermail/nginx/2011-July/027966.html

    我不得不添加recursive_error_pages ;进入服务器部分,所以我的最终配置如下所示:

    server {
        listen 443 ssl http2;
        server_name cdn.domain.tld;
    
        ssl_certificate /etc/letsencrypt/live/cdn.domain.tld/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/cdn.domain.tld/privkey.pem;
    
        client_max_body_size 10m;
    
        client_body_buffer_size 128k;
        proxy_connect_timeout 75;
        proxy_send_timeout 300;
        proxy_read_timeout 500;
        proxy_buffer_size 4k;
        proxy_buffers 4 32k;
        proxy_busy_buffers_size 64k;
        proxy_temp_file_write_size 64k;
        proxy_http_version 1.1;
        proxy_buffering on;
        proxy_redirect off;
    
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    
        #we need this to enable redirection of 502 code inside @not-found location
        recursive_error_pages on;
    
        #location for redirection of 502 responses received from thumbnail generator.
        location =/502.html {
           root /var/www/html;
           internal;
        }
    
        location  / {
            proxy_pass http://192.168.10.5/thumbs$request_uri;
    
            #Sets caching time for different response codes. If only caching time is specified  then only 200, 301, and 302 responses are cached.
            proxy_cache_valid 30m;
    
            add_header X-Proxy-Thumbs-Cache $upstream_cache_status;
    
            #intercept 404 from backend
            #from nginx docs:
            #If an error response is processed by a proxied server or a FastCGI/uwsgi/SCGI/gRPC server,.
            #and the server may return different response codes (e.g., 200, 302, 401 or 404), it is possible to respond with the code it returns (equal sign does that)
            #So we proxy 404 to app which generates thumbnail and returns it with 200 code
            proxy_intercept_errors on;
            error_page 404 = @not-found;
    
            access_log /var/log/nginx/cdn.access.log cachelog;
            error_log /var/log/nginx/cdn.error.log;
        }
    
        location @not-found {
           proxy_pass http://192.168.10.12/thumbgen?key=$request_uri;
    
           add_header X-Proxy-Thumbs-Cache2 $upstream_cache_status;
    
           proxy_intercept_errors on;
           error_page 502  /502.html;
    
           access_log /var/log/nginx/cdn-404.access.log cachelog;
           error_log /var/log/nginx/cdn-404.error.log;
        }
    }
    
    • 0

相关问题

  • Gzip 与反向代理缓存

  • nginx 作为代理的行为

  • Nginx 学习资源 [关闭]

  • 提供 70,000 个静态文件 (jpg) 的最佳方式?

  • 在 Apache、LightTPD 和 Nginx Web 服务器上提供 PHP 5.x 应用程序的现状?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve