NGINX中的proxy_pass CDN文件,但得到502错误的请求

我正在使用S3来存储xml文件并使用CloudFront来分发此文件。 我想使用nginx的proxy_passfind这个文件,而不用改变我的域名。 如www.mydomain.com/filename.xml该文件的S3 url是: https ://s3-ap-southeast-2.amazonaws.com/bucketname/bucket-sub-name/filename.xml CDNurl是https://dev.example.com/filename.xml如果我写下面的nginxconfiguration,它工作正常。

if($request_uri = "/filename.xml") { rewrite .* /bucketname/bucket-sub-name$request_uri break; proxy_pass https://s3-ap-southeast-2.amazonaws.com; } 

但是,如果我改变像下面的nginxconfiguration,它给了我502坏的网关

 if($request_uri = "/filename.xml"){ proxy_pass https://dev.example.com$request_uri; } 

要么

 if($request_uri = "/filename.xml"){ proxy_pass https://s3-ap-southeast-2.amazonaws.com/bucketname/bucket-sub-name$request_uri; } 

有没有人可以解释为什么第一个工作和第二个和第三个部分不工作? 那是因为CDNconfiguration有什么问题吗? 那可以直接proxu_pass url吗?

ifrewrite指令都是ngx_http_rewrite_module模块和你得到的行为的一部分,取决于它们周围的指令。 if可以的if ,避免是一个好主意。 在你的情况下,你可能可以使用嵌套locations

您的第二和第三部分可能不起作用,因为它们指定了确切的URL而不重写,而且这些URL 是错误的 。 当URL错误时,可以在Nginx的error_log中进行validation,也可以通过使用nginx二进制版本( nginx-debug )的debugging版本和切换Nginx的error_log进行debug