工作中常常有寫不能有網(wǎng)頁下載東西的需求,在Apache下搭建完成后直接導(dǎo)入文件即可達到下載/顯示文件的效果;
而Nginx的目錄列表功能默認是關(guān)閉的,如果需要打開Nginx的目錄列表功能,需要手動配置,還可以進行訪問驗證;
nginx目錄列表功能需要用到下面這個模塊:
ngx_http_autoindex_module
此模塊用于自動生成目錄列表,只在 ngx_http_index_module模塊未找到索引文件時發(fā)出請求.
下面就對nginx的目錄瀏覽及驗證訪問功能的操作進行梳理:
1)設(shè)置目錄瀏覽
打開nginx的配置文件,如:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | [root@wangshibo ~] # vim /usr/local/nginx/conf/vhost/test.conf server { listen 80; server_name localhost; // 訪問http: //ip ,發(fā)現(xiàn)訪問的站點目錄還是默認的;可以將此處的localhost直接改成服務(wù)器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; #顯示的文件時間為文件的服務(wù)器時間;即按服務(wù)器時間顯示 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服務(wù)
[root@wangshibo ~]# /usr/local/nginx/sbin/nginx -s reload
然后就可以訪問了:
如上的設(shè)置,要想設(shè)置nginx的目錄瀏覽功能,必須要打開下面這個參數(shù)
autoindex on;
此外,另外兩個參數(shù)最好也加上去:
autoindex_exact_size off;
默認為on,顯示出文件的確切大小,單位是bytes。
改為off后,顯示出文件的大概大小,單位是kB或者MB或者GB
autoindex_localtime on;
默認為off,顯示的文件時間為GMT時間。
改為on后,顯示的文件時間為文件的服務(wù)器時間
2)設(shè)置訪問驗證
創(chuàng)建類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虛擬目錄設(shè)置區(qū)別
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
auth_basic "MyPath Authorized"; //為提示信息,可以自行修改;會出現(xiàn)在第一次訪問Nginx站點的彈出框內(nèi)
auth_basic_user_file /usr/local/nginx/conf/auth_password; //最好跟htpasswd文件的全路徑
}
重啟nginx服務(wù)
[root@wangshibo ~]# /usr/local/nginx/sbin/nginx -s reload
這時候訪問站點的soft目錄時就會被要求輸入用戶名和密碼:
如果用戶名和密碼輸入錯誤會提示401錯誤(大名鼎鼎的http基本認證)
需要特別注意的是:
加上認證之后該目錄下的php文件將不會被解析,會運行下載。
如果要使其能夠解析php可以,可將上面的配置改為:
1 2 3 4 5 6 7 8 9 10 11 12 | 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]
查看,發(fā)現(xiàn)已經(jīng)傳過來了
[root@bastion-IDC ~]# ls /tmp/testmd/
haha
[root@bastion-IDC ~]# cat /tmp/testmd/haha
this is test file
-----------------------------------------------------------------------版本隱藏設(shè)置-------------------------------------------------------------------------------------------
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | ---------------------------------------------------------------------------- 1)在nginx下禁止使用ip訪問,將使用ip訪問的流量重定向到公司的官網(wǎng)上。 在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服務(wù)即可! curl -i http: //www .wangshibo.com 測試訪問就會發(fā)現(xiàn)nginx的header信息中已沒有版本信息了(-i參數(shù)) 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 //發(fā)現(xiàn)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開啟狀態(tài)查看功能及參數(shù)說明 本模塊在編譯的時候默認是不編譯的,如果是從源碼編譯安裝的nginx,那么需要在. /configure 編譯的時候加上對應(yīng)的模塊--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段里面增加如下的內(nèi)容: location /nginx-status { stub_status on; access_log off; #allow 127.0.0.1; #deny all; } 重啟nginx服務(wù)后,訪問: # 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 – 活躍的連接數(shù)量 server accepts handled requests — 總共處理了113個連接 , 成功創(chuàng)建113次握手, 總共處理了116個請求 reading — 讀取客戶端的連接數(shù). writing — 響應(yīng)數(shù)據(jù)到客戶端的數(shù)量 waiting — 開啟 keep-alive 的情況下,這個值等于 active – (reading+writing), 意思就是 Nginx 已經(jīng)處理完正在等候下一次請求指令的駐留連接. |