Angular and Spring Boot served by an Apache HTTPD server

The virtual host configuration

In Ubuntu, it is in the sites-enabled/000-default.conf 

<VirtualHost *:89>
        LogLevel trace8
        LogLevel rewrite:trace8
 
        ServerName localhost:89
        DocumentRoot /var/www/quizz
 
        RewriteEngine on
 
        # don't rewrite and stop the chain
        RewriteRule ^(/api|/processLogin|/logout-b)($|/) - [L]
 
        # If an existing asset or directory is requested go to it as it is and stop the chain
        RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
        RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
        RewriteRule ^ - [L]
 
        # If the requested resource doesn't exist, use index.html
        RewriteRule ^ /index.html
 
        ErrorLog ${APACHE_LOG_DIR}/site/error.log
        CustomLog ${APACHE_LOG_DIR}/site/access.log combined
 
</VirtualHost>

Some notes :
– /var/www/quizz is the location where the angular application source code is.
– the rules are executed sequentially and but if a rule with a L suffix matches (we can understand L as (L)ast rule if matches), the next rules are applied.
– we don’t want to rewrite url that requests the backend
– And finally as default rule if not one was applied, all remaining requests are mapped to index.html. Otherwise Apache would return a 404 response for requested angular route locations as these are not an physical resources.  

The proxy configuration

In Ubuntu, it is in the mods-enabled/proxy.conf .
The current configuration uses AJP like seen in a previous post but that is not mandatory to set up a proxy between apache and tomcat, that is just a (at least theorical) optimization.

<IfModule mod_proxy.c>
  LogLevel proxy:trace5
  ProxyRequests Off
  ProxyPassMatch .+\.html$ !
  ProxyPassMatch .+\.js$ !
  ProxyPass / ajp://localhost:13080/
  ProxyPassReverse / ajp://localhost:13080/
</IfModule>

The idea is simple : we don’t use the proxy to the backend for js and html files.
Similarly to rewrite rules, ProxyPass and PoxyPassMatch are processed sequentially.
And here, at the first match the proxy chain is finished for the current request (no need to add a special flag for)

Ce contenu a été publié dans Non classé. Vous pouvez le mettre en favoris avec ce permalien.

Laisser un commentaire

Votre adresse de messagerie ne sera pas publiée. Les champs obligatoires sont indiqués avec *