Home > Mobile >  How to use Nginx Regex in the location
How to use Nginx Regex in the location

Time:06-28

I hate regex..

But in some cases I have to use regular expressions. How can I get the content in bold below?

https://proxyhost/bucketname/test/uid

location  ~* /regex/ {
    proxy_pass https://$1.s3.amazonaws.com/;
}

I need use this regex to extract the bucket name and fill the real url in Nginx S3 proxy

Thanks!

CodePudding user response:

Assuming you're using python.

import re

url='https://hostname/bucketname/test/uid'

m = re.match(pattern="(?:https://)([^/] )/([^/] )/([^/] )/([^/] )", string=url)

m.group(1)  # returns 'hostname'
m.group(2)  # returns 'bucketname'
m.group(3)  # returns 'test'
m.group(4)  # returns 'uid'

CodePudding user response:

location  ~ ([^/] ) {
        resolver 8.8.8.8;
        proxy_pass https://$1.s3.amazonaws.com/;
    }

use "return 200 $1;" get the correct result, but now I get another error named SignatureDoesNotMatch

researching...(google ing)

  • Related