[英]Setup nginx to serve a static file if exists, or let the server create it if not
Pre NGINX 预NGINX
In my website, I set up images such that you can request a custom size. 在我的网站中,我设置了图像,以便您可以请求自定义尺寸。
file.jpg?s=wXh //w and h are numbers in pixels
If file.jpg exists in size wXh, I serve it from S3. 如果file.jpg的大小为wXh,我会从S3提供服务。
If file.jpd does not exist in that size. 如果file.jpd不存在该大小。 I create it in the correct size, stream it to the client, and save it to S3 so next time, it will exist. 我以正确的大小创建它,将其流式传输到客户端,然后将其保存到S3,以便下次存在。
Now this is a rather complex situation for NGINX I would guess. 现在这是NGINX的一个相当复杂的情况,我猜。
How can I tell nginx to serve it from S3 if the file exists, or to forward the request to my node server if the file doesn't exist? 如果文件存在,如何告诉nginx从S3提供它,或者如果文件不存在,将请求转发给我的节点服务器?
Thanks 谢谢
Update: 更新:
Tried to use the method suggested by @Alexey Ten, which seems promising, I trust it is the right way but I am still having trouble. 试图使用@Alexey Ten建议的方法,看起来很有希望,我相信这是正确的方法,但我仍然遇到麻烦。
This is the code I used inside the conf file: 这是我在conf文件中使用的代码:
# domain.com/pubstore is where we have node route to s3
# the format of the string we use is the path on the s3 server.
# domain.com/pubstore/folder1/folder2/file.ext will tell node to
# fetch the file from [bucket]/folder1/folder2/file.ext
# location /pubstore/ {
# proxy_pass http://bucketName.s3.amazonaws.com/;
# proxy_intercept_errors on;
# error_page 404 = @nodejs;
# }
# location @nodejs {
# proxy_pass http://localhost:8080/pubstore/;
# proxy_http_version 1.1;
# proxy_set_header Upgrade $http_upgrade;
# proxy_set_header Connection 'upgrade';
# proxy_set_header Host $host;
# proxy_cache_bypass $http_upgrade;
# gzip_disable msie6;
# gzip on;
# gzip_vary on;
# gzip_types image/svg+xml image/x-icon;
# gzip_min_length 256;
# }
When I tried to execute nginx I got this error: 当我尝试执行nginx时出现此错误:
Starting nginx: nginx:
[emerg] "proxy_pass" cannot have URI part in location given by
regular expression, or inside named location, or inside "if" statement,
or inside "limit_except" block
Ideas on correcting this? 纠正这个的想法?
You should pass request to S3 and if it returns 404 error, you should resend it to node. 您应该将请求传递给S3,如果它返回404错误,您应该将其重新发送到节点。 Something like this: 像这样的东西:
location /path/to/file/ {
proxy_pass http://s3.domain/path/to-file/on/s3/;
proxy_intercept_errors on;
error_page 404 = @nodejs;
}
location @nodejs {
proxy_pass http://node.server;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.