Nginx 重定向适用于浏览器,但使用 curl 时,它只会输出 301 moved permanently

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

我有一个 default.conf 文件显示:

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

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

当我在浏览器中输入 http:///ip_address/file.txt 时,它会将我重定向到 https://objectstorage.ca-city.oraclecloud.com/n/randomhash/b/bucketname/o/file.txt 并下载文件

当我尝试使用 curl:curl -X GET http://ip_address/testfile.txt 时,我得到了这个:

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

提前致谢

301 就是我在这里所期望的。

使 curl 遵循带有 -L 标志的重定向(为了安全可能添加 –max-redirs

因此您的 curl 命令将如下所示:

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

默认情况下,cURL 不遵循重定向。您需要 -L 标志:

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

请参阅有关标志的 man 页部分

   -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.