Apache ProxyPass - Regex to Exclude Files

15,529

Solution 1

Use ProxyPassMatch. ProxyPass expects fully written path elements, it does not accept regexes.

As ProxyPassMatch takes a regex, this means you must also anchor it:

ProxyPassMatch ^/dgg-[^.]+\.xml$ !

Solution 2

I had a situation where I wanted few images to be picked from Apache webserver and few images to be included from the application server (In my case Jboss). So I wanted one regex that had to both exclude and include. Here is what I added to httpd.conf file under VirtualHost tag.

There are some css and js files which are in jsf jars and jenia popup jars which we will not find on webserver. So reach out to app server.The regexp is looking for all *.js and *.css urls but exclude any urls that have /jenia4faces and /faces in it. This is to make sure scripts like this /MYWEBAPP/jenia4faces/popup/popupFrame/js/popupFrame.js and /MYWEBAPP/faces/myFacesExtensionResource/tabbedpane.HtmlTabbedPaneRenderer/11302665/dynamicTabs.js are still pulled from app server . Rest all .js and .css will be served by webserver.

  ProxyPassMatch ^(/MYWEBAPP/(?!jenia4faces).*\.js)$ !
  ProxyPassMatch ^(/MYWEBAPP/(?!faces).*\.css)$ !
  ProxyPassMatch ^(/MYWEBAPP/(?!jenia4faces).*\.js)$ !
  ProxyPassMatch ^(/MYWEBAPP/(?!faces).*\.css)$ !

where /MYWEBAPP is my web apps root context. Also (?!faces) is to tell if the url doesnt not have "faces" in the url path.

Share:
15,529
Pedro Lobito
Author by

Pedro Lobito

[root@lob ~]# man life No manual entry for life "If you can’t fly, run. If you can’t run, walk. If you can’t walk, crawl, but by all means, keep moving.” - Martin Luther King Jr.: Keep Moving

Updated on June 28, 2022

Comments

  • Pedro Lobito
    Pedro Lobito almost 2 years

    I'm trying to exclude all files starting with "dgg-" and ending in ".xml", example: dgg-file-1.xml from using the apache proxy.

    This works:

    ProxyPass /myfile.xml ! # single file
    ProxyPass /directory ! # all files inside dir
    

    This doesn't work:

    ProxyPass /dgg-(.*)\.xml !

    How can I achieve this ?

    ps- I'm using this code inside the httpd.conf->virtualhost not .htaccess.

  • Pedro Lobito
    Pedro Lobito over 12 years
    That's exactly what I needed. Thanks!
  • casafred
    casafred over 9 years
    Thank you! Thank you! Thank you! I'm using Apache 2.4 with php5-fpm and a ProxyPassMatch to fcgi. However, excluding a folder from being passed was proving increasingly frustrating. After searching some time for a solution, this finally did the trick. Just in case someone else needs to see how it works together (e.g. for a Drupal installation where the files folder should not allow php execution): ProxyPassMatch ^(/sites/default/files/(.*\.php(/.*)?))$ ! ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:8000/var/www AddHandler php5-fcgi .php