3
votes

How to serve static files (images, javascript, stylesheets) for a PSGI / Plack based web app?

The answer would probably depend on what web server one uses, be it CGI, FastCGI, mod_psgi, or pure-Perl like Starman. I have heard that using Plack::Middleware::Static or Plack::App::File (together with Plack::App::URLMap) is to be used only for development...

1

1 Answers

6
votes

As far as live deployment goes, a very uncomplicated (and fast) setup is if you let the web server deal with the static content and let the Plack app deal with the dynamic content. That would generally require at least 2 proxies in your web server config. Proxy A to your static files (assuming they're all generally in the same place) and proxy B to the port which your Plack app is deployed on.

For example, part of an nginx config might look like the following. Assume that the Plack app is running on port 5001 locally and that your static files are available under the url http://mydomainname.com/static

server {
    listen 80;
    server_name mydomainname.com;

    location / {
        proxy_pass http://localhost:5001/;
        proxy_redirect off;
        proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Port $server_port;
        proxy_set_header   X-Forwarded-Host $host;
    }

    location /static {                                                                                                                                             
        root /path/to/static/files;                                                                                                                         
    }

}