繁体   English   中英

设置nginx以提供静态文件(如果存在),或者让服务器创建它(如果不存在)

[英]Setup nginx to serve a static file if exists, or let the server create it if not

预NGINX

在我的网站中,我设置了图像,以便您可以请求自定义尺寸。

file.jpg?s=wXh //w and h are numbers in pixels

如果file.jpg的大小为wXh,我会从S3提供服务​​。

如果file.jpd不存在该大小。 我以正确的大小创建它,将其流式传输到客户端,然后将其保存到S3,以便下次存在。

现在这是NGINX的一个相当复杂的情况,我猜。

如果文件存在,如何告诉nginx从S3提供它,或者如果文件不存在,将请求转发给我的节点服务器?

谢谢

更新:

试图使用@Alexey Ten建议的方法,看起来很有希望,我相信这是正确的方法,但我仍然遇到麻烦。

这是我在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;
   # }

当我尝试执行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 

纠正这个的想法?

您应该将请求传递给S3,如果它返回404错误,您应该将其重新发送到节点。 像这样的东西:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM