chocokoala
chocokoala

Reputation: 351

Nginx redirect works on the browser but when using curl, it only outputs 301 moved permanently

I have a default.conf file that shows:

server {
    listen       80;
    server_name  localhost;
    rewrite ^/(.*)$ https://objectstorage.ca-city.oraclecloud.com/n/randomhash/b/bucketname/o/$1 permanent;

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

When i hit my browser with http://ip_address/file.txt, it will redirect me to https://objectstorage.ca-city.oraclecloud.com/n/randomhash/b/bucketname/o/file.txt and download the file

When i try to use curl: curl -X GET http://ip_address/testfile.txt, i get this:

<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.18.0</center>
</body>
</html>

Thanks in advance

Upvotes: 3

Views: 5446

Answers (2)

Frederik Spang
Frederik Spang

Reputation: 3454

cURL does not, by default, follow redirects. You'll need the -L flag:

$ curl -L -X GET http://ip_address/testfile.txt

See the man-page section regarding the flag

   -L, --location
          (HTTP) If the server reports that the requested page has moved to a different location (indicated with a Location: header and a 3XX response code), this option will make
          curl  redo  the request on the new place. If used together with -i, --include or -I, --head, headers from all requested pages will be shown. When authentication is used,
          curl only sends its credentials to the initial host. If a redirect takes curl to a different host, it won't be able to intercept the user+password. See also  --location-
          trusted on how to change this. You can limit the amount of redirects to follow by using the --max-redirs option.

          When curl follows a redirect and the request is not a plain GET (for example POST or PUT), it will do the following request with a GET if the HTTP response was 301, 302,
          or 303. If the response code was any other 3xx code, curl will re-send the following request using the same unmodified method.

          You can tell curl to not change the non-GET request method to GET after a 30x response by using the dedicated options for that: --post301, --post302 and --post303.

Upvotes: 3

Dominik
Dominik

Reputation: 6313

A 301 would be what I'd expect here.

Make curl follow the redirects with the -L flag (and maybe add –max-redirs for safety)

So your curl command would look like this:

curl -L –max-redirs 10 -X GET http://ip_address/testfile.txt

Upvotes: 2

Related Questions