简体   繁体   English

Nginx | 当 proxy_pass 用于 S3 托管文件时,带有 add_header 的标头被忽略

[英]Nginx | headers with add_header ignored when proxy_pass is used for S3 hosted file

I've a Nginx configuration, where I get certain files from AWS S3 bucket, like call from *.my.api.com/file.js will get the file from X folder in S3.我有一个 Nginx 配置,我从 AWS S3 存储桶获取某些文件,例如来自*.my.api.com/file.js的调用将从 S3 中的 X 文件夹获取文件。

I've an exceptional domain (like xx.my.api.com ) for which I will add the我有一个特殊的域(如xx.my.api.com ),我将为其添加

  • Cache-Control "no-store, no-cache";缓存控制“无存储,无缓存”;
  • Pragma "no-cache";编译指示“无缓存”;

headers and for the rest of *.my.api.com the headers will be default (it's cache-control: public now).标头和*.my.api.com的 rest 的标头将是默认的(它的cache-control: public )。

On my local environment, the file is hosted on my machine, so the headers are set correctly.在我的本地环境中,该文件托管在我的机器上,因此标题设置正确。 However, on production, the headers come as default as cache-control: public .但是,在生产中,标头默认为cache-control: public

I've read answers like this saying there should be no trouble with it, but it's not working for me.我读过这样的答案,说应该没有问题,但这对我不起作用。

Is there anything I'm doing wrong?有什么我做错了吗? Is it related to the file being hosted on AWS?它与托管在 AWS 上的文件有关吗?

My Nginx configuration is as below:我的 Nginx 配置如下:

server {
    listen 80;
    root /var/xyz/public;
    index index.html index.htm;
    server_name my.api.com *.my.api.com;

    add_header Access-Control-Allow-Origin "*";

    if ($http_host ~* "^(.*).my.api.com$"){
        set $myName $1;
    }

    location ~ /myfile.js {
        resolver 8.8.8.8;
        proxy_buffering off;
        proxy_set_header Content-Length "";
        proxy_set_header Cookie "";
        proxy_method GET;
        proxy_pass_request_body off;
        proxy_max_temp_file_size 0;

        if ($myName = "mySpecialName") {
            proxy_pass http://path/to/aws/s3/bucket/file.js;

            add_header Cache-Control "no-store, no-cache";
            add_header Pragma "no-cache";
            add_header X-XSS-Protection "1";
            add_header X-Frame-Options "SAMEORIGIN";
            add_header X-Content-Type-Options nosniff;
        }

        if ($query_string !~* "myQueryString=([^&]*)") {
            proxy_pass http://path/to/aws/s3/bucket/file.js;
        }

        if ($query_string ~* "myQueryString=([^&]*)") {
            proxy_pass http://path/to/some/other/aws/s3/bucket/file.js;
        }
    }
}

I've tried:我试过了:

  • always
  • proxy_pass_request_headers on
  • proxy_set_header
  • copying the server code and adjusting for xx.my.api.com only复制服务器代码并仅针对xx.my.api.com进行调整
  • proxy_hide_header (can't be used because of if block) proxy_hide_header (由于if块而不能使用)
  • more_set_headers

but none of them worked.但他们都没有工作。

Any help would be appreciated, thanks in advance.任何帮助将不胜感激,在此先感谢。

We've solved it by adding the headers from our DNS panel, which was used for caching the file stored in S3.我们通过添加 DNS 面板中的标题解决了这个问题,该面板用于缓存存储在 S3 中的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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