Nginx只允许域名访问,禁止IP访问

QuibblerQuibbler 2020-08-13 约 9 分钟 1831 次阅读

Nginx只允许域名访问,禁止IP访问

IP地址很容易暴露,通过域名访问则相对安全,而且不管换到哪台服务器上,只需要通过域名访问就行了。不希望自己的站点通过IP(114.116.128.229)被访问,只允许通过唯一域名Quibbler.cn访问。那么该如何设置呢?


1、Nginx配置

nginx配置文件是/usr/local/nginx/conf目录下的nginx.conf文件。修改server节点中部分字段:

...
        server_name quibbler.cn;
        if ($host != 'quibbler.cn'){
  			return 403;
		}
    ...

修改后的完整conf配置文件

user  www www;
worker_processes auto;
worker_cpu_affinity auto;
error_log  /home/wwwlogs/nginx_error.log  crit;
pid        /usr/local/nginx/logs/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200;
events
    {
        use epoll;
        worker_connections 51200;
        multi_accept off;
        accept_mutex off;
    }
http
    {
        include       mime.types;
        default_type  application/octet-stream;
        server_names_hash_bucket_size 128;
        client_header_buffer_size 32k;
        large_client_header_buffers 4 32k;
        client_max_body_size 50m;
        sendfile on;
        sendfile_max_chunk 512k;
        tcp_nopush on;
        keepalive_timeout 60;
        tcp_nodelay on;
        fastcgi_connect_timeout 300;
        fastcgi_send_timeout 300;
        fastcgi_read_timeout 300;
        fastcgi_buffer_size 64k;
        fastcgi_buffers 4 64k;
        fastcgi_busy_buffers_size 128k;
        fastcgi_temp_file_write_size 256k;
        gzip on;
        gzip_min_length  1k;
        gzip_buffers     4 16k;
        gzip_http_version 1.1;
        gzip_comp_level 2;
        gzip_types     text/plain application/javascript application/x-javascript;
        gzip_vary on;
        gzip_proxied   expired no-cache no-store private auth;
        gzip_disable   "MSIE [1-6]\.";
        #limit_conn_zone $binary_remote_addr zone=perip:10m;
        ##If enable limit_conn_zone,add "limit_conn perip 10;" to server section.
        server_tokens off;
        access_log off;
server
    {
        listen 80 default_server reuseport;
        #listen [::]:80 default_server ipv6only=on;
        server_name quibbler.cn;
        if ($host != 'quibbler.cn'){
  			return 403;
		}
        index index.html index.htm index.php;
        root  /home/wwwroot/default;
        #error_page   404   /404.html;
        # Deny access to PHP files in specific directory
        #location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }
        include enable-php.conf;
        location /nginx_status
        {
            stub_status on;
            access_log   off;
        }
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
            expires      30d;
        }
        location ~ .*\.(js|css)?$
        {
            expires      12h;
        }
        location ~ /.well-known {
            allow all;
        }
        location ~ /\.
        {
            deny all;
        }
        access_log  /home/wwwlogs/access.log;
    }
include vhost/*.conf;
}

2、重启Nginx

进入到nginx可执行文件目录。

cd /usr/local/nginx/sbin

重启nginx服务,让修改的配置生效。

./nginx -s reload

这样就只允许通过域名访问。

参考资料:

        nginx只允许域名访问,禁止ip访问

        nginx禁止IP只允许域名访问

        让apache只允许域名访问而禁止IP实现方法

        nginx只允许域名访问,禁止ip访问

        linux下apache配置文件在哪里

相关推荐

Honeycam 最好的GIF动图软件
精选分享

Honeycam 最好的GIF动图软件

Honeycam 最好的GIF动图软件推荐一款Gif制作软件:Honeycam,是一款高质量的GIF动图制作和编辑器。以前将视频转GIF,录制屏幕动图要费很大功夫,这款软件真的非常简洁好用,值得收藏,博客上的GIF都是用Honeycam录制的。官网:Honeycam1、简洁的界面下载安装完成后打开,主界面很简洁。2、录制屏幕选择录制,调整好要录制的区域录制完成后,可以编辑每个帧,调整帧时长,删除多

2.6k
国内开源镜像站点
精选分享

国内开源镜像站点

国内开源镜像站点开源,(Open Source)全称为开放源代码。开源就是要用户利用源代码在其基础上修改和学习的,但开源系统同样也有版权,同样也受到法律保护。国内的高校和一些企业也建立了开源镜像站点,支持开源。从开源站点上面可以下载很多开源资源(Android源码),上大学的时候就在这些镜像网站上下载Linux发行版、Qt等软件。 清华大学:http://mirrors.tuna.tsinghua

2.6k
牛人快跑:Boss 直聘自动化求职工具
置顶 精选分享

牛人快跑:Boss 直聘自动化求职工具

找工作最难熬的不是面试,而是投递期的无意义消耗:同一句打招呼发两百遍,眼睛还要替平台把关——筛掉皮包公司、跳过半年不登录的招聘方、避开挂羊头卖狗肉的僵尸职位。牛人快跑 GeekGeekRunhttps://github.…

160