Nginx 不存在的域名无法路由到404页面

Nginx 不存在的域名无法路由到404页面

我的 Nginx 配置文件包含几个服务器块,如下所示。使用以下配置,它会将不存在的域重定向到配置文件中的第一个服务器块。但是我想将不存在的域重定向到 404 页面。

 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...

如果您能告诉我如何将配置文件中不存在的域路由到 404 页面,我将不胜感激。

編輯:

默认 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

您应该使用以下配置来返回 404 标头。

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

如果你需要添加自定义 404 页面,则可以使用以下命令

server {
  server_name _;
  listen 80 default_server;
  error_page 404 http://example.com/404.html;
  return 404;
}

答案2

我在配置文件末尾为不存在的 HTTPS 域设置了以下几行,它工作正常。因为它可能对某些人有帮助,所以我把它发布在这里:

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

相关内容