Timothy O'Connell

  Media Production, Art Projects and Information Systems

Use Nginx as a Proxy for a Mojolicious App

We've got this mission-critical legacy webapp at the office that uses Mojolicious.

As part of a year-end clean-up project, we had to move it to our central application server, which basically meant moving it from a dev server where it was the only thing running to a multi-tenant tools server where pretty much everything else lives.

Once we got it ported (i.e. dependencies handled, executing user updated, etc.) and running on port 3000 (i.e. the default port for Mojolicious apps), it fell to me to set up the nginx proxy.

Normally, when we use nginx to proxy to CGI apps, we just use generic/vanilla proxy setups:

location ~ /whatever/ {
  include     /usr/local/nginx/conf/proxy.conf;

...where /usr/local/nginx/conf/proxy.conf is just a generic proxy config like the one on the Nginx example site.

Setting up the proxy to the Mojo app, however, ended up being a little bit more complex:

location ~ /application_name/(?<app_request>.*) {
  include     /usr/local/nginx/conf/fastcgi.conf;

Basically, there's three things to notice here:

  1. The capture group app_request is used to take whatever URL is coming from the user and pass everything after /application_name/ to the Mojo server as the actual requested URL: the user's browser sends '/application_name/view?param=whatever' and we pass '/view?param=whatever' to the Mojo server.

  2. We have to include /usr/local/nginx/conf/fastcgi.conf (again, see the Nginx example site: we use the vanilla one) because we need to get some additional http request elements proxied over to the Mojo server for our application to work correctly.

  3. Finally, the $is_args and $args variables we plug into to the proxy_pass get those special http request elements we need proxied over to our Mojo server.

2015-11-20 - mojolicious, nginx, servers, sysadmin