zoukankan      html  css  js  c++  java
  • Nginx——基于站点目录和文件的URL访问控制、禁止IP/非法域名访问

    Nginx——基于站点目录和文件的URL访问控制

    对于为用户服务的大多数公司而言,把控用户权限是一件十分重要的事情。通过配置Nginx来禁止访问上传资源目录下的PHP、shell、Python等程序文件,这样用户即使上传了这些文件也没法去执行,以此来加强网站安全。

    1. 限制禁止解析指定目录下的制定程序

    location ~ ^/images/.*.(php|php5|.sh|.pl|.py)$ {
      deny all;
    }
    location ~ ^/static/.*.(php|php5|.sh|.pl|.py)$ {
      deny all;
    }
    location ~* ^/data/(attachment|avatar)/.*.(php|php5)$ {
      deny all;
    }

     2. 禁止访问Nginx的root根目录下的某些文件

    location ~*.(txt|doc)${
      if (-f $request_filename) {
        root /data/www/www;
        #还可以使用"rewrite ...."重定向某个URL
        break;
      }
    }
    location ~*.(txt|doc)${
        root /data/www/www;
        deny all;
    }
    需要注意:如果有php匹配配置,上面的限制配置应该放在php匹配的前面
    location ~.*.(php|php5)?${
      fastcgi_pass 127.0.0.1:9000
      fastcgi_index index.php
      include fcgi.conf;
    }

    3. 禁止指定目录或path匹配的访问

    location ~ ^/(static)/ {
            deny all;
    }
    location ~ ^/static {
            deny all;
    }
    #禁止访问目录并返回指定的http状态码
    location /admin/ {
      return 404;
    }
    location /templates/ {
      return 403;
    }

    4. 限制网站来源IP访问。比如禁止某目录让外界访问,但允许某IP访问该目

    location ~ ^/order/ {
      allow 172.16.60.23;
      deny all;
    }
     还可以使用if来限制客户端ip访问(即添加白名单限制)
     
    if ( $remote_addr = 172.16.60.28 ) {
        return 403;
    }
    if ( $remote_addr = 172.16.60.32 ) {
        set $allow_access_root 'true';
    }

    Nginx——禁止IP/非法域名访问

     
    在生产环境中,为了网站的安全访问,需要Nginx禁止一些非法访问,如恶意域名解析,直接使用IP访问网站。下面记录一些常用的配置示例:

    1)禁止IP访问,如果没有匹配上server name就会找default默认,返回501错误。

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

    2)通过301跳转到主页

    server {
      listen 80 default_server;
      server_name _;
      rewrite ^(.*) http://www.kevin.com/$1 permanent;
    }

    3)凡是请求kevin.bao.com 都跳转到后面域名grace.ru.com上。(需要放到server配置里)

    if ($host ~ '^kevin.bao.com'){
         return 301 https://grace.ru.com$request_uri;
       }
  • 相关阅读:
    【TensorFlow篇】--Tensorflow框架可视化之Tensorboard
    UTF-8与UTF-8(BOM)区别
    JSP response.setCharacterEncoding与response.setContentType的区别
    Tomcat启动报错org.apache.catalina.core.StandardContext listenerStart
    JS 变量作用域
    JS 函数
    JS中typeof的用法
    JS Map与Set
    JS 选择结构语句与循环结构语句
    JS 对象
  • 原文地址:https://www.cnblogs.com/user-sunli/p/14639702.html
Copyright © 2011-2022 走看看