简体   繁体   中英

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

Pre 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.

If file.jpd does not exist in that size. I create it in the correct size, stream it to the client, and save it to S3 so next time, it will exist.

Now this is a rather complex situation for NGINX I would guess.

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?

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.

This is the code I used inside the conf file:

   # 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:

 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. 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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