skip to Main Content

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!

2

Answers


  1. Chosen as BEST ANSWER
    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)


  2. 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'
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search