nginx:代理s3特殊文件模式(css)

我们正在用nginx提供一个web应用程序。 它将所有通过https传递给监听本地主机的服务器进行代理。

现在我想让nginx充当代理并从s3服务我们的css文件。

因为我想根据主机名来查询不同的文件(当总是提供相同的html内容时),所以不能直接从s3提供文件

这种情况下最好的做法是什么?

最好的做法是不通过您的服务器路由到S3的stream量,这只会减慢速度。 您可以使用像这样的URL结构从S3加载资产: https : //s3.amazonaws.com/example-bucket-name/example.jpg

还build议从一个单独的域提供静态内容,因为浏览器打开了一个给定域的有限数量的并行连接。

将静态内容从单独的域中提供的另一个原因是通过“无cookie”域来提高性能。 您的应用可能使用Cookie,并且浏览器在每次请求时都会为其设置Cookie标头。 在S3域中不会出现这种情况,它不会设置cookie。

为CDN使用备用域是常见的。 人们往往不会注意到,因为这些URL没有出现在地址栏中,只出现在源代码中。

我通过使用这个块做了工作。 问题是我没有使用parsing器。

  location ~ \.css { # Important to resolve host of s3 resolver 8.8.8.8; # we can only ever GET/HEAD these resources limit_except GET { deny all; } # cookies are useless on these static, public resources proxy_ignore_headers set-cookie; proxy_hide_header set-cookie; proxy_set_header cookie ""; proxy_hide_header x-amz-delete-marker; proxy_hide_header x-amz-id-2; proxy_hide_header x-amz-request-id; proxy_hide_header x-amz-version-id; # only rely on last-modified (which will never change) proxy_hide_header etag; # heavily cache results locally proxy_cache_valid 200 28d; proxy_cache_valid 403 24h; proxy_cache_valid 404 24h; # s3 replies with 403 if an object is inaccessible; essentially not found proxy_intercept_errors on; error_page 403 =404 /_error/http-404.html; # go get it from s3 proxy_pass https://s3.eu-central-1.amazonaws.com/bucket-name/$host/style.css; # annotate response about when it was originally retrieved add_header x-cache '$upstream_cache_status $upstream_http_date'; # heavily cache results downstream expires max; }