nginx禁止直接通过ip进行访问并跳转到自定义403页面

禁止直接通过ip方法80端口

server {
    listen 80 default;
    server_name _;
    error_page 403 /403.html;
    location = /403.html {
        root html;
    }
    location / {
        return 403;
    }
}
server {
    listen 80;
    server_name mayanan.cn;
    location / {
       root   html;
       index  index.html index.htm;
    }
    error_page 404 /404.html;
    location = /404.html {
        root html;
    }
}

参考文档

禁止直接通过ip访问80和443端口

 # 禁用ip访问80、443端口
server {
    listen 80 default;
    listen 443 default_server;
    server_name _;
    # ssl相关配置
    ssl_certificate /usr/local/nginx/keys/6569749_www.mayanan.cn.pem;
    ssl_certificate_key /usr/local/nginx/keys/6569749_www.mayanan.cn.key;
    ssl_session_cache    shared:SSL:1m;  # 多个工作进程共享缓存
    ssl_session_timeout 5m;  # 缓存超时时间
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; # 表示使用的加密套件的类型
    ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;  # 表示使用的TLS协议的类型
    ssl_prefer_server_ciphers on;  # 优先使用服务器密码

    # 自定义403错误页面
    error_page 403 /403.html;
    location = /403.html {
        root html;
    }
    location / {
        return 403;
    }
    # 注意需要通过location返回403,不要直接return 403,否则返回的是nginx默认的错误页面。
}
posted @ 2022-09-25 14:04  专职  阅读(1150)  评论(0编辑  收藏  举报