nginx和Tomcat配合使用時nginx.conf 配置中有個漏洞,那就是沒有配置哪些目錄是不容許直接訪問的,在傳統tomcat做爲服務器的時候,tomcat自己的機制就禁止直接訪問WEB-INF下的內容,可是在nginx中,因爲配置了部份內容直接從nginx轉發出去,這就致使了WEB-INF目錄實際上可能會被暴露出去,一旦暴漏了,那麼系統架構,源代碼,數據庫配置文件,系統配置文件等內容將一併泄露,這對於商業項目來說會是致命的安全隱患,再次提醒本身以及相關人士,必定要配置不容許訪問的目錄。
爲此,必須在nginx.conf中配置上不容許訪問的網站目錄。
不容許訪問配置方式以下:
css
location ~ ^/(WEB-INF|META-INF)/* { deny all; }
一個完整的nginx.conf例子:
html
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 #### ############################ }