nginx 禁止未绑定的域名访问

nginx 禁止未绑定的域名访问

方法1:

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:

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 的内容:

 

posted on 2018-04-15 10:22 大豆男生 阅读(...) 评论(...) 编辑 收藏

导航

公告

统计