Redirect/Rewrite (in nginx) Requests to Subdirectory to S3-Compatible Bucket

人盡茶涼 提交于 2019-12-13 03:46:09

问题


I am attempting to get nginx to redirect/rewrite requests to a specific subdirectory so that they are served by an S3-compatible bucket instead of the server. Here is my current server block:

{snip} (See infra.)

Despite fiddling with this for some time now, I've only been able to get it to return 404s.

Additional Information

https://omnifora.com/t/redirect-rewrite-in-nginx-requests-to-subdirectory-to-s3-compatible-bucket/402

Attempts Solutions So Far

  1. rewrite

    rewrite ^/security-now/(.*) $scheme://s3.us-west-1.wasabisys.com/bits-podcasts/security-now/$1;

  2. return

    return 302 $scheme://s3.us-west-1.wasabisys.com/bits-podcasts/security-now/$1;

  3. proxy_pass

    proxy_set_header Host s3.us-west-1.wasabisys.com; proxy_pass $scheme://s3.us-west-1.wasabisys.com/bits-podcasts/security-now/$1;


回答1:


You can't use rewrite for cross-domain redirections, for this case you must use proxy_pass, for example:

    location ~ ^/directory1/(.*) {
        proxy_set_header Host s3.us-west-1.wasabisys.com;
        proxy_pass $scheme://s3.us-west-1.wasabisys.com/target-bucket/security-now/$1;
    }

Note that if you specify your server with domain name instead or IP address, you'll need to specify additional parameter resolver in your server configuration block, for example:

server {
    ...
    resolver 8.8.8.8;
    ...
}

Update.

It seems I was wrong stating that you can't use rewrite for cross-domain redirections. You can, but in this case your user got HTTP 301 redirect instead of "transparent" content delivery. Maybe you got 404 error because you missed a $ sign before scheme variable?



来源:https://stackoverflow.com/questions/53547815/redirect-rewrite-in-nginx-requests-to-subdirectory-to-s3-compatible-bucket

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!