Nginx目錄瀏覽及其驗證功能、版本隱藏等配置說明

 

工做中經常有寫不能有網頁下載東西的需求,在Apache下搭建完成後直接導入文件便可達到下載/顯示文件的效果;
而Nginx的目錄列表功能默認是關閉的,若是須要打開Nginx的目錄列表功能,須要手動配置,還能夠進行訪問驗證;
nginx目錄列表功能須要用到下面這個模塊:
ngx_http_autoindex_module  
此模塊用於自動生成目錄列表,只在 ngx_http_index_module模塊未找到索引文件時發出請求.php

下面就對nginx的目錄瀏覽及驗證訪問功能的操做進行梳理:html

1)設置目錄瀏覽
打開nginx的配置文件,如:java

[root@wangshibo ~]# vim /usr/local/nginx/conf/vhost/test.conf
 server {
   listen 80;
   server_name localhost;            //訪問http://ip,發現訪問的站點目錄仍是默認的;能夠將此處的localhost直接改爲服務器ip地址
   root /var/www/html;
   index index.html;

   location / {
   autoindex on;
   autoindex_exact_size off;
   autoindex_localtime on;
   }

   location /images {
      root   /var/www/html/shibo;
      autoindex on;
        }

   location /bo   {
      autoindex  on;                        #自動顯示目錄
      autoindex_exact_size  off;            #改成off後,顯示出文件的大概大小,單位是kB或者MB或者GB;即人性化方式顯示文件大小不然以byte顯示
      autoindex_localtime on;               #顯示的文件時間爲文件的服務器時間;即按服務器時間顯示
      limit_rate_after 10m;                 #10m以後下載速度爲10k
      limit_rate 10k;
      alias /opt/html/redhat;   #虛擬目錄
      }

}

查看下站點根目錄下的文件:
[root@wangshibo ~]# ls /var/www/html/
aa hehe shibo test.html
重啓nginx服務
[root@wangshibo ~]# /usr/local/nginx/sbin/nginx -s reloadlinux

而後就能夠訪問了:nginx

如上的設置,要想設置nginx的目錄瀏覽功能,必需要打開下面這個參數
autoindex on;apache

此外,另外兩個參數最好也加上去:
autoindex_exact_size off;
默認爲on,顯示出文件的確切大小,單位是bytes。
改成off後,顯示出文件的大概大小,單位是kB或者MB或者GBbootstrap

autoindex_localtime on;
默認爲off,顯示的文件時間爲GMT時間。
改成on後,顯示的文件時間爲文件的服務器時間vim

2)設置訪問驗證
建立類htpasswd文件(若是沒有htpasswd命令,可經過"yum install -y *htpasswd*"或"yum install -y httpd")
[root@wangshibo ~]# htpasswd -c /usr/local/nginx/conf/auth_password wangshibo //會被要求輸入兩次密碼
[root@wangshibo ~]# cat /usr/local/nginx/conf/auth_password
wangshibo:$awe1$I2FIVtPG$I51oSU4eatH.tJdnmxtg67瀏覽器

Nginx配置中添加auth認證配置
[root@wangshibo ~]# vim /usr/local/nginx/conf/vhost/test.conf
......
location ^~ /soft/ {
     root /var/www/html;              //此處爲soft的上一級目錄。注意root和alias虛擬目錄設置區別
     autoindex on;
     autoindex_exact_size off;
     autoindex_localtime on;
     auth_basic "MyPath Authorized";                    //爲提示信息,能夠自行修改;會出如今第一次訪問Nginx站點的彈出框內
     auth_basic_user_file /usr/local/nginx/conf/auth_password;                  //最好跟htpasswd文件的全路徑
}tomcat

重啓nginx服務
[root@wangshibo ~]# /usr/local/nginx/sbin/nginx -s reload

這時候訪問站點的soft目錄時就會被要求輸入用戶名和密碼:

若是用戶名和密碼輸入錯誤會提示401錯誤(大名鼎鼎的http基本認證)

須要特別注意的是:
加上認證以後該目錄下的php文件將不會被解析,會運行下載。
若是要使其可以解析php能夠,可將上面的配置改成:

location ^~ /soft/ {
   location ~ \.php$ {                           //或者是location ~ .*\.(php|php5)?$ {
      root   /var/www/html;
      fastcgi_pass   127.0.0.1:9000;
      fastcgi_read_timeout 300;
      fastcgi_index  index.php;
      fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;;
      include        fastcgi.conf;
  }
      auth_basic "Authorized users only";
      auth_basic_user_file /usr/local/nginx/conf/auth_password;
}

nginx運行目錄瀏覽後,就能夠利用wget進行文件遠程傳輸了(只針對文件,由於在http下只能文件訪問,直接跟url訪問目錄是404)
好比:
[root@wangshibo ~]# cat /var/www/html/aa/haha
this is test file

在瀏覽器裏直接點擊站點下的文件(好比上面的haha文件)就會下載下來了(點擊文件,除了html格式的文件能直接讀出來,其餘文件都是直接下載)。
也能夠在linux終端命令行裏使用wget進行文件傳輸,好比在遠程機器上下載上面站點的haha文件:
[root@bastion-IDC ~]# wget http://113.110.186.117/aa/haha -P /tmp/testmd
--2017-01-03 15:14:18-- http://113.110.186.117/aa/haha
Connecting to 113.110.186.117... connected.
HTTP request sent, awaiting response... 200 OK
Length: 18 [application/octet-stream]
Saving to: 「/tmp/testmd/haha」

100%[=====================================================================================================>] 18 --.-K/s in 0s

2017-01-03 15:14:18 (2.60 MB/s) - 「/tmp/testmd/haha」 saved [18/18]

查看,發現已經傳過來了
[root@bastion-IDC ~]# ls /tmp/testmd/
haha
[root@bastion-IDC ~]# cat /tmp/testmd/haha
this is test file

====================================版本隱藏設置===========================================

1)在nginx下禁止使用ip訪問,將使用ip訪問的流量重定向到公司的官網上。
在vhost下從新編輯一個default.conf 文件,以下:
server { 
    listen 80 default_server;
#    server_name _;
#    return 500;
    rewrite ^(.*) https://www.wangshibo.com permanent; 
}

而後重啓nginx便可!
=====================================================
下面配置就是直接禁止ip訪問了
server { 
    listen 80 default_server;
    server_name _;
    return 500;
}
====================================================

2)隱藏nginx的版本號
直接在nginx.conf文件中的http{}裏面添加:
server_tokens off;

重啓nginx服務便可!
curl -i http://www.wangshibo.com    測試訪問就會發現nginx的header信息中已沒有版本信息了(-i參數)

3)隱藏tomcat的版本號
# /data/tomcat8/bin/version.sh        #查看版本號信息
Using CATALINA_BASE:   /data/tomcat8
Using CATALINA_HOME:   /data/tomcat8
Using CATALINA_TMPDIR: /data/tomcat8/temp
Using JRE_HOME:        /usr/lib/jvm/java-1.7.0-openjdk.x86_64
Using CLASSPATH:       /data/tomcat8/bin/bootstrap.jar:/data/tomcat8/bin/tomcat-juli.jar
Server version: Apache Tomcat/8.5.15
Server built:   May 5 2017 11:03:04 UTC
Server number:  8.5.15.0
OS Name:        Linux
OS Version:     2.6.32-696.3.2.el6.x86_64
Architecture:   amd64
JVM Version:    1.7.0_141-mockbuild_2017_05_09_14_20-b00
JVM Vendor:     Oracle Corporation

# cp -r /data/tomcat8 /data/tomcat8.bak
# cd /data/tomcat8/lib
# jar xf catalina.jar
# vim org/apache/catalina/util/ServerInfo.properties 
server.info=Apache Tomcat        //修改爲這樣
server.number=                 //清空
server.built=                  //清空

# jar cf catalina.jar org     //再次打包覆蓋
# ll catalina.jar 
# /data/tomcat8/bin/version.sh        //發現tomcat的版本信息已被隱藏
Using CATALINA_BASE:   /data/tomcat8
Using CATALINA_HOME:   /data/tomcat8
Using CATALINA_TMPDIR: /data/tomcat8/temp
Using JRE_HOME:        /usr/lib/jvm/java-1.7.0-openjdk.x86_64
Using CLASSPATH:       /data/tomcat8/bin/bootstrap.jar:/data/tomcat8/bin/tomcat-juli.jar
Server version: Apache Tomcat           
Server built:   
Server number:  
OS Name:        Linux
OS Version:     2.6.32-696.3.2.el6.x86_64
Architecture:   amd64
JVM Version:    1.7.0_141-mockbuild_2017_05_09_14_20-b00
JVM Vendor:     Oracle Corporation

4)nginx-status開啓狀態查看功能及參數說明
本模塊在編譯的時候默認是不編譯的,若是是從源碼編譯安裝的nginx,那麼須要在./configure編譯的時候加上對應的模塊--with-http_stub_status_module
使用 ./configure --help 能看到更多的模塊支持,而後編譯安裝便可。
[root@www vhosts]# /usr/local/nginx/sbin/nginx -V      //使用這個命令能夠查看在編譯安裝的時候使用了哪些模塊
nginx version: nginx/1.8.1
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC) 
built with OpenSSL 1.0.1g 7 Apr 2014 (running with OpenSSL 1.0.1e-fips 11 Feb 2013)
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre

接下來須要在nginx的配置文件中增長這個配置項。打開nginx的配置文件 nginx.conf,在server段裏面增長以下的內容:
 
       location /nginx-status {
       stub_status on;
       access_log off;
       #allow 127.0.0.1;                        
       #deny all;
}

重啓nginx服務後,訪問:
# curl http://127.0.0.1/nginx-status
Active connections: 11921 
server accepts handled requests
113    113     116 
Reading: 0 Writing: 7 Waiting: 42

active connections – 活躍的鏈接數量
server accepts handled requests — 總共處理了113個鏈接 , 成功建立113次握手, 總共處理了116個請求
reading — 讀取客戶端的鏈接數.
writing — 響應數據到客戶端的數量
waiting — 開啓 keep-alive 的狀況下,這個值等於 active – (reading+writing), 意思就是 Nginx 已經處理完正在等候下一次請求指令的駐留鏈接.
相關文章
相關標籤/搜索