]> git.vanrenterghem.biz Git - www2.vanrenterghem.biz.git/blob - source/posts/Implementing_Webmention_on_my_blog.org
Gebruik geen spaties in tags.
[www2.vanrenterghem.biz.git] / source / posts / Implementing_Webmention_on_my_blog.org
1 #+date: <2023-05-14 20:32:00 +0800>
2 #+filetags: :indieweb:blogging:open_web:
3 #+title: Implementing Webmention on my blog
5 Following on from my last [[file:Bring_Back_Blogging.org][post on joining the indieweb]]... Back in February, I implemented Webmentions on my website. I took a roll-my-own approach, borrowing from [[http://superkuh.com/blog/2020-01-10-1.html][an idea by superkuh]]. 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.
7 Technically, nginx logs the body of POST requests in its logfile. In the main configuration file `/etc/nginx/nginx.conf`, I've added
9 #+BEGIN_SRC sh
10 # Defined for Webmention logging support of www.vanrenterghem.biz
11 log_format postdata '$time_local,$remote_addr,"$http_user_agent",$request_body';
12 """]]
14 In the configuration for www.vanrenterghem.biz, the following lines enable logging webmention requests:
16 [[!format  sh """
17 # use proxying to self to get the HTTP post variables.
18     # https://stackoverflow.com/questions/4939382/logging-post-data-from-request-body
19     location = /webmention {
20         limit_req zone=webmention;
21         client_max_body_size 7k;
22         if ($request_method = POST) {
23                 access_log /var/log/nginx/postdata.log postdata;
24                 proxy_pass $scheme://www.vanrenterghem.biz/logsink;
25                 break;
26         }
27         return 204 $scheme://$host/serviceup.html;
28     }
29     location /logsink {
30         #return 200;
31         # use 204 instead of 200 so no 0 byte file is sent to browsers from HTTP forms.
32         return 204;
33     }
34 #+END_SRC
36 Before the `server` section in there, I'm reducing the risk of abuse by rate limiting requests:
38 #+BEGIN_SRC sh
39 limit_req_zone  $server_name  zone=webmention:1m   rate=2r/s;
40 #+END_SRC
42 The logfile is being monitored by a service calling a shell script:
44 #+BEGIN_SRC sh
45 #!/bin/sh
46 # Service starts on boot in /etc/systemd/system/webmention.service
47 TO=my@email.address
48 WEBMENTION_LOG=/var/log/nginx/postdata.log
49 inotifywait -s -m $WEBMENTION_LOG --format "%e %w%f" --event modify|
50 while read CHANGED;
51 do
52     echo "$CHANGED"
53     logger "Webmention received"
54     tail -n1 $WEBMENTION_LOG | /usr/bin/mail -a'From: niihau webmention service <webmaster@email.address>' -s 'Webmention received' $TO
55 done
56 #+END_SRC
58 This uses `inotifywait`, which is part of [[https://github.com/inotify-tools/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.
60 The systemd service is defined in `/etc/systemd/system/webmention.service`:
62 #+BEGIN_SRC sh
63 [Unit]
64 Description=Service to monitor nginx log for webmentions
65 After=multi-user.target
67 [Service]
68 ExecStart=/root/webmention_service.sh
70 [Install]
71 WantedBy=multi-user.target
72 #+END_SRC
74 Announcing I'm accepting webmentions is as simple as putting the endpoint in the header of the blog:
76 #+BEGIN_SRC html
77 <link rel="webmention" href="https://www.vanrenterghem.biz/webmention">
78 #+END_SRC
80 Clearly federating conversation as the final level of joining the indieweb is quite a bit more complicated than achieving 'level 2' status on [[https://indiewebify.me/][indiewebify.me]].