Nginx将所有请求传递到特定的S3文件 [英] Nginx pass all requests to specific S3 file
问题描述
我有一个S3存储桶,其中包含网站的静态内容,并且我有一个EC2实例,该实例接收到该网站的所有流量.
I have an S3 bucket with the static contents of my site and I have an EC2 instance that receives all of the traffic to the site.
我想让对EC2的每个请求都从我的S3返回一个特定的文件,但是我想保持URL与用户插入它时相同.
I want to have every request to the EC2 return a specific file from my S3, but I want to keep the URL the same as the user inserted it.
示例:假设我的文件位于/path/index.html中 如果用户向www.mydomain.com发出请求,则我希望提供该文件;如果用户向www.mydomain.com/some/random/path/发出请求,我仍然希望提供相同的文件.最后一个要求是位置将保持不变.也就是说,即使提供了相同的文件,用户仍将在浏览器中看到www.mydoamin.com和www.mydoamin.com/some/random/path/.
Example: Let's assume that my file is located in /path/index.html If the user makes a request to www.mydomain.com, I want to serve that file, and if the user makes a request to www.mydomain.com/some/random/path/ I still want to serve the same file. The last requirement is that the location will stay the same. That is, the user will still see www.mydoamin.com and www.mydoamin.com/some/random/path/ in the browser, even though the same file was served.
这是我到目前为止拥有的nginx配置文件,该文件似乎不起作用:
Here's the nginx config file I have so far, which doesn't seem to work:
server {
listen 80;
ssl off;
location / {
proxy_http_version 1.1;
proxy_set_header Host 'some-host.s3.amazonaws.com';
proxy_set_header Authorization '';
proxy_hide_header x-amz-id-2;
proxy_hide_header x-amz-request-id;
proxy_hide_header Set-Cookie;
proxy_ignore_headers "Set-Cookie";
proxy_buffering off;
proxy_intercept_errors on;
resolver 8.8.4.4 8.8.8.8 valid=300s;
resolver_timeout 10s;
proxy_pass http://some-host.s3.amazonaws.com/front-end/index.html;
}
}
关于如何进行这项工作的任何想法?
Any thoughts on how to make this work?
谢谢!
推荐答案
我最终使用了一个变量.这样就解决了问题:
I ended up using a variable. This solved the problem:
server {
listen 80;
ssl off;
location / {
proxy_http_version 1.1;
proxy_set_header Host 'some-host.s3.amazonaws.com';
proxy_set_header Authorization '';
proxy_hide_header x-amz-id-2;
proxy_hide_header x-amz-request-id;
proxy_hide_header Set-Cookie;
proxy_ignore_headers "Set-Cookie";
proxy_buffering off;
proxy_intercept_errors on;
resolver 172.16.0.23 valid=300s;
set $indexfile "some-host.s3.amazonaws.com/front-end/index.html";
proxy_pass http://$indexfile;
}
}
这篇关于Nginx将所有请求传递到特定的S3文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!