Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to server static files + proxy context

I am wondering how to configure my httpd server to serves the following pages:

My need is to serve static content located in my /var/www/static when url is /context/static and to proxy the remaining to a tomcat server

In this order:

/context/static/* --> files served by httpd
/context/*        --> resources served by tomcat

I have tried to rewrite /context/static/* to a folder pointing to my /var/www/static and added the ProxyPath directive for the remaining but I can't get it working.

What are the best practices and how to achieve that ?

Thanks in advance

like image 732
poussma Avatar asked Dec 12 '14 12:12

poussma


People also ask

What does it mean to serve static files?

Definition. Static content is any content that can be delivered to an end user without having to be generated, modified, or processed. The server delivers the same file to each user, making static content one of the simplest and most efficient content types to transmit over the Internet.

What is ProxyPass and ProxyPassReverse?

ProxyPassReverse will intercept those headers, and rewrite them to match the Apache proxy server. ProxyPass will create a reverse proxy. A reverse proxy (or gateway), appears to the client just like an ordinary web server. The client makes ordinary requests for content in the namespace of the reverse proxy.

How do I serve static content from Apache Web server?

Create a directory (also known as a folder) called static in the location of the script that runs the web.py server. Then place the static files you wish to serve in the static folder. For example, the URL http://localhost/static/logo.png will send the image ./static/logo. png to the client.


2 Answers

Well, in fact it is quiet easy...

Having such folders configured:

/var/www/static/
               |-  css/*
               |-  js/*
                \  medias/*

The following httpd configuration will redirect static/* to the /var/www and the rest will be proxied

# first rewrite for statics
RewriteEngine On
RewriteRule ^/context/static/(.+)$ /static/$1

# then proxy remaining...
ProxyPass               /context  http://127.0.0.1:8080/context
ProxyPassReverse        /context  http://127.0.0.1:8080/context
like image 191
poussma Avatar answered Oct 12 '22 20:10

poussma


I've found the following approach that works and is quite general. (4/12/2018)

Location/Proxypass expressions always take priority over any other location block, so you have to Exclude the paths that you don't want to be proxied. the "?!" does that in the regex. Since static content is, um, static, it is not so bad to require that the apache configuration be updated if another directory is needed to be served directly for a different media type.

The following was taken from a server that was proxying a Python Flask application.

<LocationMatch  "^/(?!js|css|media)" >
  ProxyPass   http://127.0.0.1:5000
  ProxyPassReverse   http://127.0.0.1:5000
</LocationMatch>

<Location "/">
  Require all granted
</Location>
like image 32
sdw Avatar answered Oct 12 '22 20:10

sdw