nginx.conf中配置上不允许访问的网站目录

nginx.conf 配置中有个漏洞,那就是没有配置哪些目录是不允许直接访问的,在传统tomcat作为服务器的时候,tomcat本身的机制就禁止直接访问WEB-INF下的内容,但是在nginx中,由于配置了部分内容直接从nginx转发出去,这就导致了WEB-INF目录实际上可能会被暴露出去,一旦暴漏了,那么系统架构,源代码,数据库配置文件,系统配置文件等内容将一并泄露,这对于商业项目来讲会是致命的安全隐患,再次提醒自己以及相关人士,一定要配置不允许访问的目录。

为此,必须在nginx.conf中配置上不允许访问的网站目录。

不允许访问配置方式如下:

       location ~ ^/(WEB-INF|META-INF)/{
                deny all;
        }

一个完整的nginx.conf例子:

user root;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  10240;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    gzip  on;
    gzip_min_length 1k;
    gzip_buffers 16 64k;
    gzip_http_version 1.1;
    gzip_comp_level 2;
    gzip_types application/json text/plain text/css;
    gzip_vary on;

    fastcgi_intercept_errors on;

	upstream tomcat_server {
	   server   127.0.0.1:8080;
#      server   127.0.0.1:8081;
#      server   127.0.0.1:8082;
	}


############################
########   server start ####
############################
    server {
        listen       80;
        server_name  www.xxx.com;
        root    /data/www/www.xxx.com;

        location /{
        	index index.htm index.html index.jsp;
        }

        location ~ ^/(WEB-INF|META-INF)/{
                deny all;
        }

        location ~ \.(jsp|do)?$ {
	        proxy_set_header Host  $host:80;
	        proxy_set_header X-Forwarded-For  $remote_addr;
	        proxy_pass http://tomcat_server;
    	}

    	location ~ .*\.(js|css|gif|jpg|jpeg|png|bmp|swf)$ {
	        expires 24h;
	    }

        if (!-e $request_filename){
                rewrite "^/a/eg/([0-9]+)/([0-9]+)/([0-9]+)$" /api/eventlog.do?game_id=$1&platform_id=$2&channel_id=$3 last;
                rewrite "^/games/game_([0-9]+)\.html$" /games/game_show.jsp?id=$1 last;
        	rewrite "^/games/page_([0-9]+)\.html$" /games/index.jsp?p=$1 last;
        	rewrite "^/games/category_([0-9]+)\.html$" /games/index.jsp?c=$1 last;
        	rewrite "^/games/category_([0-9]+)/page_([0-9]+)\.html$" /games/index.jsp?c=$1&p=$2 last;
        	rewrite "^/blogs/blog_([0-9]+)\.html$" /blogs/blog_show.jsp?id=$1 last;
        	rewrite "^/blogs/page_([0-9]+)\.html$" /blogs/index.jsp?p=$1 last;
        	rewrite "^/blogs/category_([0-9]+)\.html$" /blogs/index.jsp?c=$1 last;
        	rewrite "^/blogs/category_([0-9]+)/page_([0-9]+)\.html$" /blogs/index.jsp?c=$1&p=$2 last;
        	rewrite "^/links/category_([0-9]+)\.html$" /links/index.jsp?c=$1 last;
        }
    }
############################
########   server end   ####
############################
}

猜你喜欢

转载自stephen830.iteye.com/blog/2208507