Yak, strangling PHP with Phoenix and NGINX
At the time of writing this blogpost, my site is written in PHP, and most of the code is many years old. One could call it a legacy application: I was not very good at organising code back when I started it, so it is a bit of a mess.
Like any legacy application, rewriting it would be costly. There are many features I don’t want to lose, but at the same time I don’t want to lock myself up and rewrite them all before I can show and use any work. This kind of ‘Big Bang release’ would eat up all my free time for the next months and my motivation probably won’t make it all the way through.
A Yak to shave
Yesterday I attended the first IndieWebCamp since ‘March 2020’ and I didn’t want to come empty handed. I forced myself to start a new project called Yak.
This is actually the fifth project called ‘Yak’ on my system, but it’s the first one I actually deployed. All the Yak-projects focus on a different part of the app, since there is always something you think you should do first before the rest can start.
The newest and deployed Yak consists of a new IndieAuth endpoint. I was able to shave off this part since IndieAuth tries to be decoupled from the rest of the site: its endpoint might even be on a different domain.
I didn’t go that route though: I decided to strangle.
The simple strangle
With ‘strangle’ I refer to the ‘Strangler Pattern’, after the now more friendly(?) Named term ’Strangler Fig Application’ by Martin Fowler. This plant grows around another tree, slowly taking over its shape and killing it in the process, so that eventually, all that remains is the fig.
In terms of Seblog.nl, I would like Yak to take over more and more features from the PHP-version, until finally Yak is my new CMS. I say ‘PHP-version’, be because yes, Yak is written in Elixir now. How would I go about taking over features then? My first attempt was this:
index index.html index.php;
location /auth {
# ... proxy headers
proxy_pass http://localhost:4001;
}
location / {
try_files $uri $uri/ index.php$is_args$query_string;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_index /var/www/seblog.nl/index.php;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_param PATH_INFO $fastcgi_script_name;
include fastcgi_params;
}
That’s a pretty normal PHP setup, but with one path as a proxy. Yak is listening to port 4001 and NGINX sends all traffic for /auth
and sub-folders to it. (A firewall keeps visitors for 4001 from outside away, don’t worry.) This way I can have my IndieAuth endpoint be in a different application while still maintaining one domain to the outside.
But since this is my hobby project, I can get niftier.
Proxy all the things
The IndieAuth route is fairly well scoped and easily separated from the rest of the app. For creating posts via Micropub, I can also separate a route and then store the created posts in the same file folder (or database) since both apps run on the same machine. But not all features are that easily separated.
To make it myself easier to add new routes to Yak, I created the following monster:
index index.html;
location @yak {
# ... proxy headers
proxy_pass http://localhost:4001;
proxy_intercept_errors on;
error_page 404 = /index.php$is_args$query_string;
error_page 502 = /index.php$is_args$query_string;
}
location / {
try_files $uri @yak;
}
location ~ \.php$ {
# same as previous PHP
}
Since Yak is pretty fast (the Phoenix framework logs its response times in microseconds instead of milliseconds), I decided to just route all traffic through Yak. If Yak has the answer, that answer will be the final answer. But all routes that get a 404 Not Found
error from Yak, are given to PHP afterwards.
This way the old site still functions: PHP is not even aware that someone has looked at the request before it. I also added the 502 Bad Gateway
just in case Yak is down, which I expect only to happen for brief moments during big configuration changing deployments.
A few details to note: it all starts by adding @yak
as a final location to try_files
. Only the last location can be a virtual one, otherwise I would have put PHP directly in that list. I did remove the $uri/
and removed index.php
from the index
directive at the top: without this the homepage was not delivered to Yak.
The other crucial bit is the proxy_intercept_errors on
, because that traps the errors in the proxy and enables you to add cases for certain errors. With error_page 404 = /index.php$is_args$query_string
I send them to PHP. Other errors from Yak, like 401 and 403, are still just rendered as they came from Yak.
Fixing my POST to Micropub
While the above does work for most of my site, it broke my Webmentions and Micropub. (Yes, I tried posting this a few hours ago.) The previously described method makes all requests in PHP come in as GET, which makes sense for rendering an error page.
After a lot of trial and error, reading NGINX documentation and even trying to get Yak to proxy, I decided to localise the few POST requests I needed to be PHP and added those explicit in NGINX.
location /webmention {
try_files $uri $uri/ /index.php$is_args$args;
}
location /micropub {
try_files $uri $uri/ /index.php$is_args$args;
}
Once I moved these endpoints to Yak, I can remove them from my NGINX.