Set Access-Control-Allow-Origin dynamically apache (regex) - regex

I want to allow cross domain request for several servers following this pattern:
https://en.example.com, https://de.example.com
It seems rather simple but I'm not good with regexes and don't know how to try them without shutting down the server itself. I have tried this one but it doesn't work.
Header set Access-Control-Allow-Origin "^http(s)?://(.+\.)?example.com$"

I finally followed another way, in the .htaccess:
SetEnvIf Origin "^http(s)?://(.+\.)?example\.com$" AccessControlAllowOrigin=$0
Header set Access-Control-Allow-Origin %{AccessControlAllowOrigin}e env=AccessControlAllowOrigin
Works fine.
Special thanks to Gumbo.

\bhttps?:\/\/.+?example\.com\b
DEMO

Related

How to set routing between nginx locations based on regex with wildcard

I have some http api behind nginx, and i want to make filter requests to API based on requests parameters value. Parameters are passed directly in url like
https://api.com/api/v1/action?param1=value1&param2=value2&etc...
Lets assume that i want to filter requests with some value of param2 to some other url.
I thought that it will be easy like
location ~* /api/.*param2=somevalue.* { #location; }
But nginx cant find the match even if there is no alternative location at all.
I'm confused. Are these wildcards are truly wildcards, or I miss something? But what?
I already tried escaping and different modifiers but no luck. :(

How do I use predefined apache ENV variable in .htaccess file within regular expression

Problem:
I have a number of subdomains like foo.website.com, bar.website.com and so on.
One of them is the resources.website.com and the resources from it need to be available for all other ***.website.com subdomains.
Obviously, the browser with foo.website.com (with a .js resource from resources.website.com) is running into CORS problem.
Working code:
I am trying to configure my .htaccess of the resources.website.com to determine whether the request is performed from ***.website.com and the following solution works perfectly fine
<FilesMatch "\.(js|css|ttf|otf|eot|woff|ico)$">
SetEnvIf Origin "http(s)?://(.*\.website\.com)$" AllowOrigin=*
Header add Access-Control-Allow-Origin %{AllowOrigin}e env=AllowOrigin
</FilesMatch>
What I would like to do:
Id like to be able to keep the .com (the zone) outside of the .htaccess in the apache configuration files. (I have multiple environments under different zones so when the code, including the htaccess files, gets deployed on other zones, it is totally neutral).
In apache configuration files I have
SetEnv domain_zone com
.. and I would like to use this variable in the code above`s regexp (if its possible) like:
SetEnvIf Origin "http(s)?://(.*\.website\.%{domain_zone}e)$" AllowOrigin=*
.. but I obviously lack .htaccess skills. Thank you.

How to write regex for apache ProxyPassMatch to reverse proxy API calls

I have an angular 4 web application which is hosted on apache 2.4. The application makes use of an API written in nodejs javascript running over express. Both the website and the API service are running on the same machine but on different ports. The website is on port 80 and the API service is listening on port 9000.
I would like to set up apache to do reverse proxy for all the API calls.
For example, any url that contains /api/ I want it rewritten by apache to point to the API url:port. If I use ProxyPass like the following lines, the redirect works fine:
ProxyPass "/api/V1/systeminfo" "http://localhost:9000/api/V1/systeminfo"
ProxyPassReverse "/api/V1/systeminfo" "http://localhost:9000/api/V1/systeminfo"
What I do not know how to do, is to use the ProxyPassMatch directive and create a regular expression so that any url that contains /api/ is redirected to http://localhost:9000/api/.....
I tried the following but it does not work:
ProxyPassMatch "^/api.*$" "http://localhost:9000/$1"
ProxyPassReverse "^/api.*$" "http://localhost:9000/$1"
Neither does the following:
ProxyPassMatch "^/.*?/api.*?/v[0-9]+/(.*)$" "http://localhost:9000/$1"
ProxyPassReverse "^/.*?/api.*?/v[0-9]+/(.*)$" "http://localhost:9000/$1"
Any help would be appreciated. My regex skills are lacking!
Note: obviously 'localhost' can be an IP address or a domain, I am using it in the example for simplicity.
Many thanks!
Edit: I corrected the first example to use .* instead of just * as per Alex's comment.
I solved the problem. The correct way to do reverse proxy with apache on the above example is the following:
ProxyPassMatch "/api(.*)" "http://localhost:9000/api$1"
ProxyPassReverse "/api(.*)" "http://localhost:9000/api$1"
I knew the multiple regex examples I was trying were correct, as I was testing them with https://regex101.com/, but I was hard coding the second part of to a particular route in order to eliminate the issue of the second part being incorrect, but for some reason it does not like that. Once I understood that the (.*) part of the regex is the first capture group and used it as $1 in the second part, it all worked.
I hope I clarified the answer enough and it is useful to someone else.

Avoiding double caching of items available from different URIs using Varnish

In the Varnish Cache wiki it states an example of how to regsub to avoid caching request to www.example.com and example.com separately. The example from https://www.varnish-cache.org/trac/wiki/RedirectsAndRewrites is:
set req.http.host = regsub(req.http.host, "^www\.example\.com$","example.com");
"Requests to www.example.com and example.com will all go to the backend as "example.com" and end up cached by that string." This means duplicate caching does not occur.
I have multiple sites using the same varnish server (VCL) so am looking to replace "example.com" with a statement that will work on multiple URLs. eg:
www.example1.co.uk > example1.co.uk
www.example2.com > example2.com
What would be the appropriate regex (if that is the correct term) for this?
There are multiple separate domains (different sites with different content on different domains) using this VCL I am hoping to avoid having to alter the vcl when new sites are added/removed. Therefore a generic solution is what I am after, something that can be applied to any domain to remove the possibility of a duplicate with/without the WWW alias being store/served by Varnish. (Having trouble phrasing this, hope it is clearer!!)
I am aware that redirecting can be done outside of varnish, in Apache etc, but not looking for that as a solution.
set req.http.host = regsub(req.http.host,
"^www\.(.*)$",
"\1");
This will strip www off any domain. (I do feel reluctant to give you this answer, as it goes against my religion)
You might get penalized by search engines for serving the same content on multiple URLs, but SEO is a different topic.
Instead of what Chris suggested, you can just remove the www part:
set req.http.host = regsub(req.http.host, "^www\.", "");
Should be a teeny tiny bit faster, too

Not logging GET requests on Apache based on a query string parameter presense

I am working on a PCI project and I have been asked to not log any GET request on an Apache 2 server as long as there is a query string parameter present on the URL.
I have been trying to use SetEnvIf and SetEnvIfNoCase as noted below with no success at all, the requests keep getting logged on the access.log for apache.
I am at a lost here, so any help would be appreciated. by the way, I am not versed on Apache by any means.
SetEnvIfNocase Referer "^(SECRET=)$" dontlog
CustomLog /path/to/logs combined env=!dontlog
The anchors ^ and $ prevent the referrer from matching. Right now, you only match referrers that are exactly "SECRET=", and I doubt that's the idea. Remove the anchors and it should work.