nginx 禁止未綁定的域名訪問

nginx 禁止未綁定的域名訪問html

方法1:nginx

server {
    listen 80 default_server;
    #不指定 server_name, server_name 默認爲""
    return 444;
}

server {
    listen       80;
    server_name  www.mydomain.com;
    root         /usr/share/nginx/html;
    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;
    location / {
        proxy_set_header Host $host:$proxy_port;#proxy_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://myserver;
    }
    error_page 404 /404.html;
        location = /40x.html {
    }
    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }
}

瀏覽訪問未綁定的域名,訪問會這樣顯示(谷歌瀏覽器):瀏覽器

 

方法2:dom

server {
    listen      80 default_server;
    server_name _;
    root        /usr/share/nginx/html;
    location / {
    deny all;   
    }
    error_page 403 /403.html;
        location = /403.html {
    }
}

server {
    listen       80;
    server_name  www.mydomain.com;
    root         /usr/share/nginx/html;
    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;
    location / {
        proxy_set_header Host $host:$proxy_port;#proxy_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://myserver;
    }
    error_page 404 /404.html;
        location = /40x.html {
    }
    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }
}

瀏覽訪問未綁定的域名,會顯示 403.html 的內容:server

相關文章
相關標籤/搜索