proxy_pass CDN file in NGINX, but get 502 bad request
I am using S3 to store an xml file and use CloudFront to distribute this file. I want to use nginx proxy_pass to locate this file without change my domain name. such as www.mydomain.com/filename.xml The S3 url for that file is : https://s3-ap-southeast-2.amazonaws.com/bucketname/bucket-sub-name/filename.xml The CDN url is https://dev.example.com/filename.xml If I write nginx config like below, it works fine.
if($request_uri = "/filename.xml") {
rewrite .* /bucketname/bucket-sub-name$request_uri break;
proxy_pass https://s3-ap-southeast-2.amazonaws.com;
}
But If I changed nginx config like below, it gave me 502 bad gateway
if($request_uri = "/filename.xml"){
proxy_pass https://dev.example.com$request_uri;
}
or
if($request_uri = "/filename.xml"){
proxy_pass https://s3-ap-southeast-2.amazonaws.com/bucketname/bucket-sub-name$request_uri;
}
Is there any one could explain why the first one working and the second and third part not working? Is that because the CDN configuration has something wrong? Is that possiable that proxu_pass the url directly?
Solution 1:
you need
proxy_set_header Host "cdn domain" ;
to rewrite your nginx request host from nginx to cdn