0

My Nginx config file is included several server blocks that it's shown as below. With the following configuration, it redirects non-existing domains to the first server block in the config file. However I would like to redirect non-existing domains to 404 page.

 server 
{    
listen      443 ssl http2;
server_name     test-domain.no;
access_log  /var/log/nginx/nginx.access.log;
error_log   /var/log/nginx/nginx.error.log;
ssl    on;
ssl_certificate    /etc/nginx/ssl/certificates.pem;
ssl_certificate_key    /etc/nginx/ssl/server.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256';
ssl_session_timeout 1d;
ssl_session_cache builtin:1000 shared:SSL:10m;
ssl_session_tickets off;

location /
{
    proxy_pass         http://test-domain-sps.3fcf2715.cont.dockerapp.io:80/;
    proxy_redirect     off;

    ##proxy_set_header   Host             $host;
    proxy_set_header   X-Real-IP        $remote_addr;
    proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;

    client_max_body_size       10m;
    client_body_buffer_size    128k;

    proxy_connect_timeout      90;
    proxy_send_timeout         90;
    proxy_read_timeout         90;

    proxy_buffer_size          4k;
    proxy_buffers              4 32k;
    proxy_busy_buffers_size    64k;
    proxy_temp_file_write_size 64k;
}
}

# Redirect from HTTP to HTTPS
server { 
 listen 80;
 server_name test-domain.no; 
 return 301 https://test-domain.no$request_uri;
 #rewrite ^/(.*) https://test-domain.no/$1 permanent; 
}

#others server blocks...

I appreciate if you let me know how I can routes non-existing domain in the config file to 404 page.

Edited:

Default nginx.conf:

 # cat /etc/nginx/nginx.conf                                                                                                                                              


user  nginx;                                                                                                                                                             

worker_processes  1;                                                                                                                                                     

error_log  /var/log/nginx/error.log warn;                                                                                                                                

pid        /var/run/nginx.pid;                                                                                                                                           


events {                                                                                                                                                                 
  worker_connections  1024;                                                                                                                                            

}                                                                                                                                                                        


http {                                                                                                                                                                   
 include       /etc/nginx/mime.types;                                                                                                                                 
 default_type  application/octet-stream;                                                                                                                              

 log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '                                                                                            
                  '$status $body_bytes_sent "$http_referer" '                                                                                                        
                  '"$http_user_agent" "$http_x_forwarded_for"';                                                                                                      

 access_log  /var/log/nginx/access.log  main;                                                                                                                         

 sendfile        on;                                                                                                                                                  
 #tcp_nopush     on;                                                                                                                                                  

 keepalive_timeout  65;                                                                                                                                               

 #gzip  on;                                                                                                                                                           

 include /etc/nginx/conf.d/*.conf;                                                                                                                                    

}
1

You should use the following configuration to return 404 header.

server {
  server_name _;
  listen 80 default_server;
  return 404;
}

If you need to add a custom 404 page then you can use the following

server {
  server_name _;
  listen 80 default_server;
  error_page 404 http://example.com/404.html;
  return 404;
}
10
  • Thanks for the reply. Would it also work for the non-existing domain in https?
    – Matrix
    Aug 7 '17 at 12:27
  • It will not work for https because you will not have a valid certificate for it.
    – dkokmadis
    Aug 7 '17 at 13:03
  • I tested for a non-existing domain in my nginx file like: client-test.domain.no but it still routes to the first server block: test-domain.no. Any idea?
    – Matrix
    Aug 7 '17 at 13:21
  • Do you have another block that has default_server or server_name _; Can you check the default nginx.conf file?
    – dkokmadis
    Aug 7 '17 at 13:37
  • 1
    You need to create a server block with listen 443 ssl default_server. Aug 8 '17 at 9:39
0

I've set up the following lines in the end of config file for non-existing HTTPS domains and it works fine. As it might be helpful for someone, I post it here:

server {
 server_name _;
 listen 443 ssl;
 ssl_certificate    /etc/nginx/ssl/cert.pem;
 ssl_certificate_key    /etc/nginx/ssl/serve.key;
 return 404;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.