Regular expressions in GCLB Url Map - google-cloud-platform

I need to create the following rules in Google cloud load balancer:
/en/example
/ar/example
/.../example
Such as the 2 letters represents a locale.
I couldn't find a way to setup regular expressions in the load-balancer url-map.
How could this be achieved?

I was able to create fallowing environment:
Domain: example.com
Three buckets: www.example.com, example-en and example-ar. All three publicly accessible for read;
www.example.com contains index.html file in the main directory
example-en contains index.html file in the /en/ directory
example-ar contains index.html file in the /ar/ directory
Each bucket required setting up index page to work:
gsutil web set -m index.html gs://www.example.com/
Setting website configuration on gs://www.example.com/...
gsutil web set -m index.html gs://example.en/
Setting website configuration on gs://example.en/...
gsutil web set -m index.html gs://example.ar/
Setting website configuration on gs://example.ar/...
I configured HTTPTS load balancer (with GCP's managed SSL certificate) for example.com and 'www.example.com`.
Backend services & backend buckets contain three above buckets.
Host and path rules are configured that:
if you go to www.example.com you will get index.html from www.example.com bucket
if you go to www.example.com/en/ you will get index.html from example-en bucket
if you go to www.example.com/ar/ you will get index.html from example-ar bucket
You can redirect any path to any bucket you want, the only requirement is that the redirected path must match the path in the bucket (otherwise it won't work). You can also use subdomains and redirect them to other buckets. It's possible to add new services and paths to existing LB so you can make changes later on.
Let me know if this answers your question.
Last thing is setting up a front-end (one for port 80 and one for 443 with SSL encryption).
You can also have a look at the Path Matcher documentation which will give you some more ideas.

Regex is not available in GLB at this time
We can only use path based request route to the backend services(instance group / bucket)
Reference:
Path rules do not use regular expression or substring matching.
Source: https://cloud.google.com/load-balancing/docs/url-map-concepts#pm-constraints

Related

Google cloud, how to rewrite load balancer host & path for bucket service so I don't have to nest my files under, example: /files/public?

I know there are a few threads on this issue out there but all of them have answers from 2019 where other behaviours and features were available, for example, at the time you couldn't even add a bucket as a backend service on a load balancer.
I'm trying to serve files from bucket A from mydomain.com/storage/public and bucket B from mydomain.com/storage/private which isn't an issue.
What I haven't successfully done is to rewrite the path so that I don't have to add all my files in bucket A nested in the folder /storage/public and for bucket B in /storage/private -- but rather just serve them from the root of the bucket.
I have added the following path & host rewrite rule that doesn't work:
host:
path: /*
I'm assuming this tells it to rewrite mydomain.com/storage/public/myfile.png to mydomain.com/myfile.png, but again this doesn't seem to work.
As I understand that you might have one file eg xyz, .png in public folder so your URL looks like
mydomain.com/storage/public/xyz.png
And would like to serve the file(s) from the root folder ie., mydomain.com/storage/xyz.png. You have also attempted to rewrite the paths and were not able to do so. Let me know if I captured your information correctly.
I would recommend you to modify your configuration with below steps:
Click edit on your Load balancer
Change the host and path rules :
In the left column of the screen, click Host and path rules.
Select Advanced host and path rule (URL redirect, URL rewrite).
Click the row that contains the non-default path rule,
Click the pencil icon edit for the /storage/* Route traffic to a single backend:backend name
Under Paths, delete /storage/* and add /*.
Under Action, select Route traffic to a single backend.
Click Add-on action (URL rewrite).
Leave Host rewrite blank.
Under Path prefix rewrite, enter /storage/.
Under Backend, select backend name and Click Save and Click Done.
If everything looks correct, click Update to update your HTTP load balancer.
Refer the documentation for more information.

How does one serve multiple static pages from a single S3 bucket?

Let's assume one has an Amazon S3 bucket example.com configured for static hosting. In the configuration, the console allows setting an index file and an optional error file. But I'm struggling to figure out how to add another page to the site. I thought this would be straight forward but I cannot find the answer in the official documentation or on the internet.
If I want to add one more page to the static site (e.g. example.com/page2) and there is a page2.html file already in the S3 bucket at the root, where is the correct place to make this routing configuration? Can it be done through the S3 console? Or does it need to be configured through some kind of a DNS record? As a further complication, this needs to also work with and without the www in the URL.
On the DNS side I currently have the following configuration:
CNAME | WWW | www.example.com.s3-website-east-1.amazonaws.com | TTL 30 min
URL Redirect Record | # | http://www.example.com unmasked
Are you trying to access the page at example.com/page2.html or example.com/page2
If you want to access the page at example.com/page2 then create a 'folder' called page2 off the root and in that folder put a file called index.html
If you want to use example.com/page2.html, then create a file called page2.html and put it in the 'root' of the bucket.
Simply create a file called page2.html. It will be accessible via example.com/page2.html.
No routing configuration is required.
The index file alias is only used if no page is specified (eg they go to example.com/).
As for mapping www.example.com to example.com, you would create another bucket with the name www.example.com and use "Redirect requests" to point back to example.com. (If using a CNAME works for you, that's probably easier, but test it first to see if it functions as expected. See: Mapping naked domain (www.domain.com) to static website which is saved in S3)
See: Configuring a static website using a custom domain registered with RouteĀ 53 (Follow the manual steps rather than automating via CloudFormation, so you can better understand what has been configured)

How to redirect a naked URL in an AWS S3-powered web site?

I will be hosting a static web site on S3. The problem is that the web engine behind S3-as-a-web-server does not transform http://example.com/hello/ into http://example.com/hello/index.html.
When configuring the web site, there is a provision for the root document (the one which will be displayed when calling http://example.com), but not any deeper URLS (such as my example).
Is it possible to use the redirect rules to achieve that?
I actually have a solution for this problem, but is is really convoluted:
host the web site on an S3 bucket
deploy a CloudFront instance which origins in that bucket
use a Lambda#Edge which will rewrite the call once it hits CloudFront
I hope there is something more straightforward (I have hope in the redirect rules, though "redirect" suggests that something was already attained, which is not the case in my problem as S3 does not seem to understand what http://example.com/hello/ is.
When you specify the default index file and wants to serve index.html in a subpath,
You need to have the index.html in every level.
The documentation for S3 specifies the following
If you create such a folder structure in your bucket, you must have an
index document at each level. When a user specifies a URL that
resembles a folder lookup, the presence or absence of a trailing slash
determines the behavior of the website. For example, the following
URL, with a trailing slash, returns the photos/index.html index
document.
http://example-bucket.s3-website-region.amazonaws.com/photos/ However,
if you exclude the trailing slash from the preceding URL, Amazon S3
first looks for an object photos in the bucket. If the photos object
is not found, then it searches for an index document,
photos/index.html. If that document is found, Amazon S3 returns a 302
Found message and points to the photos/ key. For subsequent requests
to photos/, Amazon S3 returns photos/index.html.
Alternatively, If you want ALL paths to server index.html, this thread might be useful

Nginx - redirect specific requests on root folder to subdomain

I currently have an hosting website that previously hosted images locally.
Before the current system, image links were referenced from the root folder
EG:
https://example.com/image.jpg
We have since moved to AWS S3 storage and in order to direct legacy image requests to S3, we added the following to our nginx conf file.
location ~* ^/.*\.(jpg|gif|png|jpeg) {
rewrite ^/(.*)$ "$scheme://i.example.com/$1" permanent;
}
However this captures all image files on the domain.
Is there a way to only redirect requests for image requests on the root domain and ignore other folders?
For example, users avatars are stored locally in
https://example.com/content/images/user/qwerty.jpg
These get redirected to the subdomain due to the regex.
I also attempted this with the S3 proxy_pass. However results were the same.
Thanks for your help
To select only image files in the top level directory, you should replace the .* element of your regex with something that does not match /. Such as:
location ~* ^/[^/]*\.(jpg|gif|png|jpeg)$ {
return 301 $scheme://i.example.com/$request_uri;
}

How to redirect non-www domain.com to www domain.com(WordPress blog) in AWS Route 53?

I have my non-www domain.com with GoDaddy and my WordPress Blog is hosted in AWS EC2. I'm using Route 53 to handle DNS requests. The existing solution for my question, seen in many places(including SOF) is to create two S3 buckets in the name of non-www domain and www domain for redirection of static websites. This is not my case.
I've my WordPress installed in EC2 and not using S3 for holding my Data. I hope this is not a static website and cannot follow the general solutions available.
I tried the following solution around and did not work
I tried changing the C-NAME record to www.domain.com but it did not worked.
I tried domain forward feature available with GoDaddy.com and didn't work.
I tried modifying .htaccess file and that too didn't work.
This is what my record sets in Route 53 look like
Name Type Value TTL
------ ----- ----- ----
domain.com. A xx.xx.xxx.xxx (EIP) 300
domain.com. MX 1 ASPMX.L.GOOGLE.COM 3600
5 ALT1.ASPMX.L.GOOGLE.COM
5 ALT2.ASPMX.L.GOOGLE.COM
10 ALT3.ASPMX.L.GOOGLE.COM
10 ALT4.ASPMX.L.GOOGLE.COM
domain.com. NS ns-27.awsdns-03.com. 172800
ns-1190.awsdns-20.org.
ns-2028.awsdns-61.co.uk.
ns-855.awsdns-42.net.
domain.com. SOA xxxxxxxxx 900
How can I redirect my domain.com to www.domain.com?
I was hesitant to post my comment as an answer because there are a gazillion ways to setup and configure Wordpress it seems. Anyway, to keep in the spirit of keeping this question in the amazon-web-services tag I ran a test case deploying from the AWS Wordpress Cloudformation template. I'm not sure if this is how you actually installed Wordpress but here is one way to redirect:
Make sure that your Cloudformation template completes successfully.
Here is what my Hosted Zone looked like - I have not added A records yet.
Get the instance IP address. Note that in this example I did not setup an Elastic IP. Since I knew that I would not need to stop the instance temporarily I opted to just stick with the automatically assigned, random pubic IP.
Next I made an A record for the domain apex of that IP and then an A record for www. I also changed the TTL to 60 seconds.
Once DNS propagation completed I tried accessing my domain name. As you can see, the AWS Cloudformation Wordpress installation defaults to a different path and URL.
Using the URL, http://example.com/wordpress did the trick.
I didn't go through the steps but when you go to http://example.com/wordpress it starts a setup screen. Enter all the information like DB name and password, etc. and then login to the admin panel. Once you go through all of that you go to the General settings screen. This is where your configuration will probably be different but for mine, the URLs were listed as http://example.com/wordpress. I simply changed these URLs to http://www.example.com/wordpress. (As an aside, I also tried changing and saving the permalink section to generate an .htaccess file but one was not generated due to the inability to write to the file. I tried making my own but I kept running into "too many redirect" messages so this might not be a route you want to take depending on your install.)
You will need to make a change in the index.php file. For my installation it was located at /var/www/html/wordpress/index.php. Make sure to make a copy before changing it. I simply added /wordpress/ in front of wp-blog-header.php. Again, this install puts the Wordpress files in the directory /wordpress - your install will probably be different.
Next you need to copy that modified index.php file to /var/www/html/ and then restart the httpd service.
To test the change I cleared out my DNS cache and opened up the network section of developer tools in Chrome.
I then opened a new tab (have to open developer tools again) and then typed in the naked domain name.
As you can see, the URL redirected to www.example.com with a 301 permanent redirect.
I'll through another suggestion out here while I'm at it. You can use the free version of Cloudflare to just do the redirect for you. Cloudflare offers a bunch of other free and useful services like CDN so if you don't mind depending on a 3rd party service (a reputable one by the way) it might be easier with more value add. As I highlighted in the screenshot however, note that if you use forwarding you cannot use some of the other advanced rule sets.
Anyway, I hope this helps!