nginx站點目錄及文件URL訪問控制


一、根據擴展名限制程序和文件訪問

利用nginx配置禁止訪問上傳資源目錄下的PHP、Shell、Perl、Python程序文件。

配置nginx,禁止解析指定目錄下的指定程序

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;
		}

 對上述目錄的限制必須寫在nginx處理PHP服務配置的前面,如下:

放置在server標簽內:

    server {
        listen       80;
        server_name  www.dmtest.com;
        location / {
            root   html;
            index  index.php index.html index.htm;
        }

        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;
            }
		
		......
		......
	}

 nginx下配置禁止訪問*.txt和*.doc文件

配置如下:

放置在server標簽內:

        location ~* \.(txt|doc)$ {
            if (-f $request_filename) {
            root /data/www/www;
            #rewrite ...    #可以重定向到某個URL;
            break;
            }
        }
        location ~* \.(txt|doc)$ {
            root /data/www/www;
            deny all;
        }

二、禁止訪問指定目錄下的所有文件和目錄

配置禁止黨文指定的單個或多個目錄。

禁止訪問單個目錄的命令如下:

放置在server標簽內:

       location ~ ^/(static)/ {
           deny all;
       }

       location ~ ^/static {
           deny all;
       }

 禁止訪問多個目錄的配置如下:

        location ~ ^/(static|js) {
            deny all;
        }

 禁止訪問目錄並返回指定的http狀態碼,配置如下:

放置在server標簽內:

    server {
        listen       80;
        server_name  www.dmtest.com;
        location / {
            root   html;
            index  index.php index.html index.htm;
        }
        location /admin/ { return 404; }    #訪問admin目錄返回404;
        location /templates/ { return 403; }  #訪問templates目錄返回403

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

 作用:禁止訪問目錄下的指定文件或者禁止訪問指定目錄下的所有內容。

三、限制網站來源IP訪問

禁止目錄讓外界訪問,但允許某IP訪問該目錄且支持PHP解析,配置如下:

在server標簽內配置如下:

        location ~ ^/mysql_loging/ {
            allow 192.168.0.4;
            deny all;
        }

        location ~ .*\.(php|php5)?$ {
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        }

說明:該配置只允許192.168.0.4IP訪問mysql_loging目錄

 限制IP或IP段訪問,配置如下:

添加在server標簽內:

        location / {
            deny 192.168.0.4;
            allow 192.168.1.0/16;
            allow 10.0.0.0/24;
            deny all;
        }

 說明:此限制是對某些IP做整個網站的限制訪問。

nginx做反向代理的時候也可以限制客戶端IP,具體如下

方法1:使用if來控制,配置如下:

if ( $remoteaddr = 10.0.0.7 ) {
	return 403;
}

if ( $remoteaddr = 218.247.17.130 ) {
	set $allow_access_root 'ture';
}

 方法2:利用deny和allow只允許IP訪問,配置如下:

location / {
	root html/blog;
	index index.php index.html index.htm;
	allow 10.0.0.7;
	deny all;
}

 方法3:只拒絕某些IP訪問,配置如下

location / {
	root html/blog;
	index indx.php index.html index.htm;
	deny 10.0.0.7;
	allow all;
}

 注意事項:

deny一定要加一個IP,否者會直接跳轉到403,不在往下執行了,如果403默認頁在同一域名下,會造成死循環訪問。

對於allow的IP段,從允許訪問的段位從小到大排列,如127.0.0.0/24的下面才能是10.10.0.0/16,其中:

  24表示子網掩碼:255.255.255.0

  16表示子網掩碼:255.255.0.0

  8表示子網掩碼:255.0.0.0

以deny all; 結尾,表示除了上面允許的,其他的都禁止。如:

deny 192.168.1.1;

allow 127.0.0.0/24;

allow 192.168.0.0/16;

allow 10.10.0.0/8;

deny all;

四、配置nginx,禁止非法域名解析訪問企業網站

方法1:讓使用IP訪問網站的用戶,或惡意接卸域名的用戶,收到501錯誤,配置如下:

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

 方法2:通過301跳轉主頁,配置如下:

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

 方法3:發現某域名惡意解析到公司的服務器IP,在server標簽里添加以下代碼即可,若有多個server要多處添加。

if ($host !~ ^www/.dmtest/.com$) {
        rewrite ^(.*) http://www.dmtest.com.com$1 permanent;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM