Apache mod_proxy ProxyPassMatch Regex - regex

I've setup Apache as a reverse proxy for a non public backend server, using mod_proxy. However one of my ProxyPassMatch directives always returns 404:
ProxyPassMatch ^/app/files/public/orders/06_production/jobs/([a-zA-Z0-9\-]+)/Preview%20PNG/(V[0-9]+)/([a-zA-Z0-9_\-]+.png)$ http://192.168.2.42/app/files/public/orders/06_production/jobs/$1/Preview%20PNG/$2/$3
This should match an incoming request like this:
/app/files/public/orders/06_production/jobs/P116087/Preview%20PNG/V1/bla.png
Any idea what's wrong with my Regex?

Figured it out:
ProxyPassMatch "\A/app/files/public/orders/06_production/jobs/([a-zA-Z0-9\-]+)/Preview PNG/(V[0-9]+)/([a-zA-Z0-9_\-]+.png)\z" "http://192.168.2.42/app/files/public/orders/06_production/jobs/$1/Preview PNG/$2/$3"

Related

API redirect with ProxyPassMatch?

I am trying to do the following
setup a proxy so i can go to this url
http://localhost/shopify-api-myshopifystore
that points to https://myshopifystore.myshopify.com
this code below works BUT I want to be able to go to
http://localhost/shopify-api-myshopifystore/admin/api/2021-01/orders/4371107971258.json
that points to
https://myshopifystore.myshopify.com/admin/api/2021-01/orders/4371107971258.json
so anything after
/admin/api/2021-01/orders/4371107971258.json
can be a wildcard
and the below code doesnt do that my webpage just shows 404 Not Found
ProxyPassMatch "/shopify-api-$/" "https://$1.myshopify.com"
ProxyPassReverse "/shopify-api-$/" "https://$1.myshopify.com"

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.

How to perform proxypassreverse with regex

I am stuck in this problem for past few days, I am using reverse proxy of Apache server and I managed to run the reverse proxy by making the following changes in config file:
ProxyPass: /server-01/ server-01.{My server}
ProxyPassReverse: /server-01/ server-01.{My server}
ProxyPass: /server-02/ server-02.{My server}
ProxyPassReverse: /server-02/ server-02.{My server}
ProxyPass: /server-03/ server-03.{My server}
ProxyPassReverse: /server-03/ server-03.{My server}
ProxyPass: /server-04/ server-04.{My server}
ProxyPassReverse: /server-04/ server-04.{My server}
All this is working but I want to make this dynamic so that I dont have to add new code and restart Apache when a new proxy server is added!
In order to use a regex you must use ProxyPassMatch:
ProxyPassMatch ^/server-(\d+)/$ server-$1.{My server}
The purpose of ProxyPassReverse is to fix any Location headers that the proxy server issues for itself. For example if I proxy mywebsite.com to bluewebsite.com and bluewebsite.com issues a redirect to bluewebsite.com/1, ProxyPassReverse will intercept it and redirect me to mywebsite.com/1 instead.
If your proxy server issues redirects to the proper front-end URLs (or does not use redirects) then ProxyPassReverse is not needed. If you do need it, you are out of luck in this example because it does not accept regexes.
Here is another question that might help you:
ProxyPassMatch with ProxyPassReverse

Set Access-Control-Allow-Origin dynamically apache (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

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.