1.Halo附件上传出现403/404

先是出现下面的问题:

The object storage service returned an error status code 403. Please check the storage policy configuration and make sure your account and service are working properly.

问题情况如下:

image-dfqk.png

如果直接改成IP+端口号访问,则没有这个问题出现。我这里使用的是NginxProxyManager+Traefik的反向代理,出现这个问题我一直以为是反向代理配置有什么问题。

查了很久,开始怀疑是Halo的S3插件有什么问题,把S3插件的创建S3 Client的代码Copy下来在本地测试,构建S3Client的代码是如下的代码:

    val s3Client = S3Client.builder()
        .region(Region.of(properties.region))
        .endpointOverride(
            URI.create(properties.endpointProtocol + "://" + properties.endpoint)
        )
        .credentialsProvider({
            AwsBasicCredentials.create(properties.accessKey, properties.accessSecret)
        })
        .serviceConfiguration(
            S3Configuration.builder()
                .chunkedEncodingEnabled(false)
                .pathStyleAccessEnabled(properties.enablePathStyleAccess)
                .build()
        )
        .build();

接着,我们再进行文件的上传/下载,发现都正常。

    var response = s3Client.listObjects(ListObjectsRequest.builder().bucket("halodata").build())

    s3Client.putObject(
        PutObjectRequest.builder().bucket("halodata").key("aaa.txt").build(),
        RequestBody.fromFile(File("/path/to/aaa.txt"))
    )

    val inputStream = s3Client.getObject(
        GetObjectRequest.builder()
            .bucket(properties.bucket)
            .key("aaa.txt")
            .build()
    )

但是Halo上传,一直报错403,基本上一样的代码,咋会出现我本地能运行,Halo能运行呢,我一直都觉得是反向代理配置ci了,但是这个现象来看,不像啊,我本地上传下载都是正常的。

问AI(ChatGPT,Gemini,豆包),都是说我反向代理的Header信息传的也问题,我一直尝试各种配置,都没有解决。

直到在Google上吵到了参考的ISSUE:

这个ISSUSE当中提到:OpenResty默认配置了反代缓存,而反代缓存会将HEAD请求转换为GET请求进行缓存,导致HEAD请求403从而引发问题点,只需要在反代站点加入以下配置即可解决。

proxy_cache_convert_head off;

在我修改完成之后,接着是下面的报错:

The object storage service returned an error status code 404. Please check the storage policy configuration and make sure your account and service are working properly.

是如下的情况:

image-ecbk.png

上面的ISSUE提醒了我,可能还有什么其他的缓存,尝试把相关的缓存的配置都关闭掉,又找到了如下的两个配置项。

proxy_buffering off; 
proxy_request_buffering off; 

2.解决方案

最终解决方案,在Nginx反向代理当中配置如下的三个选项配置:

proxy_buffering off; 
proxy_request_buffering off; 
proxy_cache_convert_head off;

我这里使用的是NginxProxyManager配置效果如下:

image-moab.png