我有以下情况:一个nginx为客户端提供静态文件。 对于请求,将在本地目录中search文件,如果找不到,则通过proxy_pass指令将请求转发到Amazon S3。
问题是,如果没有find文件,Amazon S3会返回HTTP 403,我想将该代码更改为客户端的404。
( 作为一个方面说明,运行nginx的这台机器有权从S3中检索文件而没有任何凭据,并且工作正常 )
我的configuration如下所示:
user nobody; worker_processes auto; pid /run/nginx.pid; events { worker_connections 2048; multi_accept on; use epoll; } http { reset_timedout_connection on; types_hash_max_size 2048; include /etc/nginx/mime.types; default_type application/octet-stream; gzip off; open_file_cache off; server { listen 8000 backlog=1000; server_name blablabla; # # Requests made to this server are in the form of GET /file/<sha256> and # for aabbc1111111111...11 the file will be located in: # * /files/aa/bb/c/aabbc111111...11.bin on the disk # * s3://my-bucket-name/aa/bb/c/aabbc1111111...11.bin in S3 # location ~ "^/file/(?<a>[0-9a-fA-F]{2})(?<b>[0-9a-fA-F]{2})(?<c>[0-9a-fA-F])(?<d>[0-9a-fA-F]{59})$" { root /files; rewrite ^ /$a/$b/$c/$a$b$c$d.bin break; # If 404 results from trying to serve the file from the local directory, # move to S3 error_page 404 = @try_s3; } location @try_s3 { rewrite ^(.*)\.bin$ /my-bucket-name$1 break; return 404; proxy_http_version 1.1; proxy_pass https://s3-eu-west-1.amazonaws.com; proxy_set_header Connection ""; break; proxy_intercept_errors on; # This is not working, I still get 403, but the difference is that # with it, I get 403 from nginx (with its 403 HTML template), without # it I get the 403 sent from S3 (with their HTML in it) error_page 403 =404; } # This is used for the load balancer location /ping { return 200; } location / { return 404; } } }
error_page指令是你想在这里使用,所以你在正确的轨道上。 该文档表明uri参数不是可选的。 你可能会被绊倒。 尝试这样的事情:
error_page 403 =404 /404.html;
确保创build一个有用的404.html 。
你也可以“欺骗”nginx,把403错误页面指向一个不存在的文件:
error_page 403 /this-does-not-exist.flibettyjibbety;
这可能会起作用,因为nginx返回URI处理过程中遇到的最后一个错误代码。
如果所有这些都不起作用,那么您将希望获得正在处理的请求的一些debugging日志logging,如果您无法弄清楚,请将其编辑到您的问题中以供进一步分析。