joozh 发表于 2016-12-28 09:31:04

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_processes1;
#error_loglogs/error.log;
#error_loglogs/error.lognotice;
#error_loglogs/error.loginfo;
#pid      logs/nginx.pid;

events {
worker_connections10240;
}

http {
include       mime.types;
default_typeapplication/octet-stream;
#log_formatmain'$remote_addr - $remote_user [$time_local] "$request" '
#                  '$status $body_bytes_sent "$http_referer" '
#                  '"$http_user_agent" "$http_x_forwarded_for"';
#access_loglogs/access.logmain;
sendfile      on;
#tcp_nopush   on;
#keepalive_timeout0;
keepalive_timeout65;
gzipon;
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_namewww.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/(+)/(+)/(+)$" /api/eventlog.do?game_id=$1&platform_id=$2&channel_id=$3 last;
rewrite "^/games/game_(+)\.html$" /games/game_show.jsp?id=$1 last;
rewrite "^/games/page_(+)\.html$" /games/index.jsp?p=$1 last;
rewrite "^/games/category_(+)\.html$" /games/index.jsp?c=$1 last;
rewrite "^/games/category_(+)/page_(+)\.html$" /games/index.jsp?c=$1&p=$2 last;
rewrite "^/blogs/blog_(+)\.html$" /blogs/blog_show.jsp?id=$1 last;
rewrite "^/blogs/page_(+)\.html$" /blogs/index.jsp?p=$1 last;
rewrite "^/blogs/category_(+)\.html$" /blogs/index.jsp?c=$1 last;
rewrite "^/blogs/category_(+)/page_(+)\.html$" /blogs/index.jsp?c=$1&p=$2 last;
rewrite "^/links/category_(+)\.html$" /links/index.jsp?c=$1 last;
}
}
############################
########   server end   ####
############################
}
页: [1]
查看完整版本: nginx.conf中配置上不允许访问的网站目录