]> git.vanrenterghem.biz Git - www2.vanrenterghem.biz.git/blob - posts/Implementing_Webmention_on_my_blog.mdwn
Voeg alle posts toe.
[www2.vanrenterghem.biz.git] / posts / Implementing_Webmention_on_my_blog.mdwn
1 [[!meta date="2023-05-14 20:32:00 +0800"]]
2 [[!opengraph2 ogimage="https://indieweb.org/File:indiewebcamp-logo-lockup-color@3x.png"]]
3 [[!tag indieweb blogging open_web]]
5 Following on from my last [[post on joining the indieweb|Bring_Back_Blogging]]...
7 Back in February, I implemented Webmentions on my website. I took a roll-my-own approach, borrowing from [an idea by superkuh](http://superkuh.com/blog/2020-01-10-1.html). It's a semi-automated solution which listens for webmentions using nginx. When (if) one is received, an email is generated that tells me about this, allowing me to validate it's a genuine comment.
9 Technically, nginx logs the body of POST requests in its logfile.
11 In the main configuration file `/etc/nginx/nginx.conf`, I've added
13 [[!format  sh """
14 # Defined for Webmention logging support of www.vanrenterghem.biz
15 log_format postdata '$time_local,$remote_addr,"$http_user_agent",$request_body';
16 """]]
18 In the configuration for www.vanrenterghem.biz, the following lines enable logging webmention requests:
20 [[!format  sh """
21 # use proxying to self to get the HTTP post variables.
22     # https://stackoverflow.com/questions/4939382/logging-post-data-from-request-body
23     location = /webmention {
24         limit_req zone=webmention;
25         client_max_body_size 7k;
26         if ($request_method = POST) {
27                 access_log /var/log/nginx/postdata.log postdata;
28                 proxy_pass $scheme://www.vanrenterghem.biz/logsink;
29                 break;
30         }
31         return 204 $scheme://$host/serviceup.html;
32     }
33     location /logsink {
34         #return 200;
35         # use 204 instead of 200 so no 0 byte file is sent to browsers from HTTP forms.
36         return 204;
37     }
38 """]]
40 Before the `server` section in there, I'm reducing the risk of abuse by rate limiting requests:
42 [[!format  sh """
43 limit_req_zone  $server_name  zone=webmention:1m   rate=2r/s;
44 """]]
46 The logfile is being monitored by a service calling a shell script:
48 [[!format sh """
49 #!/bin/sh
50 # Service starts on boot in /etc/systemd/system/webmention.service
51 TO=my@email.address
52 WEBMENTION_LOG=/var/log/nginx/postdata.log
53 inotifywait -s -m $WEBMENTION_LOG --format "%e %w%f" --event modify|
54 while read CHANGED;
55 do
56     echo "$CHANGED"
57     logger "Webmention received"
58     tail -n1 $WEBMENTION_LOG | /usr/bin/mail -a'From: niihau webmention service <webmaster@email.address>' -s 'Webmention received' $TO
59 done
60 """]]
62 This uses `inotifywait`, which is part of [inotify-tools](https://github.com/inotify-tools/inotify-tools). Unfortunately, `logrotate` will remove the log file on a regular basis, which is done in 3 steps. The first 2 steps results in a MODIFY event, before a DELETE event is created. That results in 2 emails being sent every time logs are rotated if using the above script. I've not tried to ignore these yet - checking for `logrotate` running at the time an event is triggered could be a solution.
64 The systemd service is defined in `/etc/systemd/system/webmention.service`:
66 [[!format  sh """
67 [Unit]
68 Description=Service to monitor nginx log for webmentions
69 After=multi-user.target
71 [Service]
72 ExecStart=/root/webmention_service.sh
74 [Install]
75 WantedBy=multi-user.target
76 """]]
78 Announcing I'm accepting webmentions is as simple as putting the endpoint in the header of the blog:
80 [[!format  html """
81 <link rel="webmention" href="https://www.vanrenterghem.biz/webmention">
82 """]]
84 Clearly federating conversation as the final level of joining the indieweb is quite a bit more complicated than achieving 'level 2' status on [indiewebify.me](https://indiewebify.me/).