How to configure NGINX to returns 404 for non hosted domains

Sergi Rodríguez  
11-08-2021 11:49  
2 minutos de lectura  

I had to solve this problem: someone pointed his domain name to the IP of my server, and then by default NGINX returns the 200 code and Success default page for new domain names. So i searched the way to configure NGINX to return only 404 error code when the domain is not hosted in my server. After some attempts i got it run.

Edit the NGINX config file with the IP of your server:

nano  /etc/nginx/conf.d/123.123.123.123.conf

And then put something like this, replacing 123.123.123.123 with your server IP, and the correct SSL certificate files paths. But this paths are setted already in the default file that it was created when installed/configured your NGINX for your hosted domains:

server {
    listen       123.123.123.123:80 default;
    server_name  _;
    return 404;
}

server {
    listen      123.123.123.123:443 ssl http2;
    server_name _;
    ssl_certificate      /usr/local/hestia/ssl/certificate.crt;
    ssl_certificate_key  /usr/local/hestia/ssl/certificate.key;
    return 404;
}

And then restart NGINX service:

sudo service nginx restart

Then if someone configure his domain name to points to your server IP address (it was my case), now your NGINX server will return a 404 code instead of the default 200 "Success" HTML page for new domain names hosted on your server.

Etiquetas : server | linux

Comentarios 0   Visitas 1552  

  Comentarios


Añada su comentario:

Comentario:
Nombre:
(pregunta anti-robots)

Enviar

DÍGANOS QUÉ NECESITA

¡¡ Le respondemos antes de 24h !!
Puede dejarnos su teléfono.

Puede rellenar el formulario (pulsar botón derecha) o bien aquí tiene nuestros datos de contacto.

(pregunta anti-robots)

  Enviar

Revisar consentimientos a cookies