How to forbid direct IP visit to discourse?

AI-generated summary

The discussion addresses how to prevent direct IP access to a self-hosted Discourse instance, returning an error instead of the site content. doudou asks how to configure Nginx to return a 500 error for direct IP visits, inquiring if modifying nginx/conf.d/discourse.conf within the Docker container is permanent. sam suggests using the SSL template to demonstrate how to create a Pups replacement rule that amends the Nginx configuration. doudou follows up, asking for the specific location of the template within the Docker container and noting that manually adding a return 500 rule blocks both domain and IP access. Lee_Ars questions the choice of a 500 error over a 403 Forbidden and recommends handling blocking at the firewall or via iptables to avoid interfering with Discourse’s core configuration. However, sam clarifies that iptables cannot parse the Host header required to distinguish between domain and IP requests. Lee_Ars jokingly responds to this limitation.

If just only discourse installed on a server, and expose 80 to host, how to forbid direct IP visit to discourse?

If someone visit the direct ip address, how to make it return 500 error.

like:

server {
listen 80 default;
server name _;
return 500;
}

If the nginx/conf.d/discourse.conf in the docker changed, is it permanent?

@codinghorror
@sam

look at our ssl template it demonstrates how to make a replace rule in pups that amends nginx config.

where, in the docker?

but when I add ‘return 500’ in the docker nginx configure file, it shows error either visit IP or domain name.

How to do?

Why do you want to return a 500 error and not a 403 forbidden?

If you control the firewall this server is behind, dropping the offending IP address there might be easier. Or, you could use iptables on the server itself and drop the IP addresses there without having to bother with Discourse’s configuration—just add a rule onto the input chain, substituting the address or CIDR range you want to block in place of “1.2.3.4”:

iptables -A INPUT -s 1.2.3.4 -j DROP

IMO, if you’re not going to use Discourse’s built-in IP address blacklisting, it’s better to do this kind of thing outside of the Discourse docker container. You get more control and you don’t have to screw with the base DIscourse configuration in ways that might conflict with the DIscourse team’s future developments.

host header needs to be parsed though, so its not doable in iptables.

I prefer the nuclear option! “No more Discourse! NO MORE ANY WEB SITES AT ALL!”