apache – How to Block Certain URLs in Apache HTTPD When Using Proxy Pass

apache-2.4httpdnode.js

My site is using apache httpd to do the reverse proxy to an app running in Express (Node.js app). Have 2 express servers, one for backend, another for frontend hosting.
Right now I'm trying to block malicious requests that is coming to my site so it returns a 404 or bad request.

My some-site-ssl.conf sample is below, but it doesnt look like its blocking the malicious websites, any help would be awesome!! Thank you.


<VirtualHost _default_:443>
      ServerAdmin webmaster@localhost

      ErrorLog ${APACHE_LOG_DIR}/error.log
      CustomLog ${APACHE_LOG_DIR}/access.log combined

      SSLEngine on

      SSLCertificateFile      /etc/letsencrypt/live/some-site.com/fullchain.pem
      SSLCertificateKeyFile /etc/letsencrypt/live/some-site.com/privkey.pem
      Include /etc/letsencrypt/options-ssl-apache.conf

      RewriteEngine On
      RewriteCond %{HTTP_REFERER} ^http://(www\.)?.*(-|.)?bad-word(-|.).*$  [NC]
      RewriteCond %{HTTP_REFERER} ^https://(www\.)?.*(-|.)?bad-word(-|.).*$  [NC]
      RewriteRule ^(.*)$ - [F,L]

      ProxyRequests On

      ProxyPass /api http://some-site.com:3000/api
      ProxyPassReverse /api http://some-site.com:3000/api

      ProxyPass / http://some-site.com:4226/
      ProxyPassReverse / http://some-site.com:4226/
</VirtualHost>

Best Answer

You can control access to any resource using the Location tag, and with it, you can disable access from certain hosts to your resources, like this:

<Location "/denied/resource">
    <RequireAll>
        Require not ip 1.2.3.4
        Require not host spammer.domain
        Require all granted
    <RequireAll>
</Location>

The access rights are inherited from top to bottom, so denying access to / will practically lock out that client from your site (unless, of course, access is granted to a lower level resource, so it is possible not to have access to /, but being able to access /this/one/). See the Apache HTTP server documentation for more info.

And like @Freddy said, you really should remove that ProxyRequests On line.