Can I forbid download from external links?

It turns out that the config Discourse uses for nginx includes something for hotlinking already, albeit commented out. So what you can do is add a couple of replaces to your app.yml file to uncomment those lines.

Edit /var/discourse/containers/app.yml with your preferred editor. At the end you’ll find a run section and at the end of that you’ll find an exec to echo “end of custom commands”.

Before that exec, add the following lines:

  - replace:
      filename: "/etc/nginx/conf.d/discourse.conf"
      from: "#valid_referers none blocked mysite.com *.mysite.com;"
      to: valid_referers none blocked mysite.com *.mysite.com;

  - replace:
      filename: "/etc/nginx/conf.d/discourse.conf"
      from: "#if ($invalid_referer) { return 403; }"
      to: if ($invalid_referer) { return 307 /; }

In the first replace section you’ll see the “to:” line with mysite.com *.mysite.com, substitute those for your own domain name, e.g. discourse.org *.discourse.org. The second replace assumes that you want visitors to be redirected to the Discourse home page.

After saving the changes, run the following commands to rebuild Discourse with the updated nginx config.

cd /var/discourse
./launcher rebuild app
4 Likes