Openresty Nginx прокси настройка типа контента при загрузке файла - PullRequest
0 голосов
/ 11 февраля 2020

Я настроил Openresty в качестве прокси Nginx для загрузки файлов в хранилище, совместимое с S3. Прокси работает хорошо, за исключением того, что Content-Type не установлен должным образом.

Моя конфигурация выглядит следующим образом

        server {
        listen       80;
        server_name  upload.example.com;
        server_tokens off; 
        access_log /usr/local/openresty/nginx/logs/access.log remote;

        location ~* ^/(.*) {

            set $bucket     "mybucket";
            set $aws_access mykey;
            set $aws_secret mysecret;
            set $url_full   $1;
            set $aws_signature "";
            proxy_pass http://backendstorage;


          types {
            text/html   html htm shtml;
            text/css    css;
            text/xml    xml;
            image/gif   gif;
            image/svg+xml svg;
          }

            set_by_lua $now       "return ngx.cookie_time(ngx.time())";
            set $string_to_sign   "$request_method\n\n\n\nx-amz-date:${now}\n/$bucket/$url_full";
            set_hmac_sha1          $aws_signature $aws_secret $string_to_sign;
            set_encode_base64      $aws_signature $aws_signature;
            resolver               8.8.8.8 valid=300s;
            resolver_timeout       10s;
            proxy_http_version     1.1;
            #proxy_hide_header      Content-Type;
            proxy_set_header       Host $bucket.s3.amazonaws.com;
            proxy_set_header       x-amz-date $now;
            proxy_set_header       Authorization "AWS $aws_access:$aws_signature";
            proxy_buffering        off;
            proxy_intercept_errors on;
            rewrite .* /$url_full break;
            proxy_set_header Upgrade $http_upgrade;
        }

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

При использовании curl для загрузки файла тип содержимого не устанавливается

curl http://upload.example.com/ --upload-file test.css -vvv
Trying 10.10.10.10...
TCP_NODELAY set
Connected to upload.example.com (10.10.10.10) port 80 (#0)
PUT test.css HTTP/1.1
Host: upload.example.com 
User-Agent: curl/7.54.0
Accept: */*
Content-Length: 15
Expect: 100-continue

Done waiting for 100-continue
We are completely uploaded and fine
HTTP/1.1 100 Continue
HTTP/1.1 200 OK
Content-Length: 0
Date: Tue, 11 Feb 2020 18:24:48 GMT
Etag: "e19c1283c925b3206685ff522acfe3e6"
Server: openresty
Vary: Accept-Encoding
X-Amz-Version-Id: versionid...

Если я пытаюсь передать содержимое, введите заголовок в вызове curl. Я получаю сообщение об ошибке.

curl http://upload.example.com/ --upload-file test.css -H "content-type: text/css" -vvv
Trying 10.10.10.10...
TCP_NODELAY set
Connected to upload.example.com (10.10.10.10) port 80 (#0)
PUT test.css HTTP/1.1
Host: upload.example.com
User-Agent: curl/7.54.0
Accept: */*
content-type: text/css
Content-Length: 15
Expect: 100-continue

Done waiting for 100-continue
We are completely uploaded and fine
HTTP/1.1 100 Continue
HTTP/1.1 403 Forbidden
Content-Length: 309
Content-Type: application/xml
Date: Tue, 11 Feb 2020 18:27:37 GMT
Server: openresty
Vary: Accept-Encoding

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Error><Code>SignatureDoesNotMatch</Code><Message>The request signature we calculated does not match the signature you provided. Check your secret access key and signing method.</Message><Resource>uploadfiles/objectNameNotDecodedYet</Resource></Error>

Я ожидаю, что использование директивы types переопределит то, что отправляется, но это не так.

Есть ли способ установить тип содержимого на основе имени файла?

...