Nginx核心配置-作為上傳服務器配置
作者:尹正傑
版權聲明:原創作品,謝絕轉載!否則將追究法律責任。
一.關鍵參數說明
client_max_body_size 1m;
設置允許客戶端上傳單個文件的最大值,默認值為1m,推薦值500M
client_body_buffer_size size;
用於接收每個客戶端請求報文的body部分的緩沖區大小;默認16k;超出此大小時,其將被暫存到磁盤上的由下面client_body_temp_path指令所定義的位置。推薦之2048k
client_body_temp_path path [level1 [level2 [level3]]];
設定存儲客戶端請求報文的body部分的臨時存儲路徑及子目錄結構和數量,目錄名為16進制的數字,使用hash之后的值從后往前截取1位、2位、2位作為文件名:
二.上傳服務器參數配置案例
1>.修改主配置文件(在主配置的httpd標簽中定義后,會對所有server標簽生效,包括加載進來的server標簽喲,這樣如果有多個虛擬主機需要上傳功能,我們只需要在主配置文件配置一份即可)
[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf/nginx.conf
worker_processes 4;
worker_cpu_affinity 00000001 00000010 00000100 00001000;
events {
worker_connections 100000;
use epoll;
accept_mutex on;
multi_accept on;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
gzip on;
charset utf-8;
#當文件大於等於給定大小時,同步(直接)寫磁盤,而非寫緩存。
directio 4m;
#上傳文件相關參數
client_max_body_size 10m;
client_body_buffer_size 16k;
client_body_temp_path /yinzhengjie/data/web/nginx/temp 1 2 2;
#禁用IE系列的長連接,默認就是禁用了IE的長連接功能.
keepalive_disable msie6;
#開啟長連接后,返回客戶端的會話保持時間為60s,單次長連接累計請求達到指定次數請求或65秒就會被斷開,后面的60為發送給客戶端應答報文頭部中顯示的超時時間設置為60s:如不設置
客戶端將不顯示超時時間。 keepalive_timeout 65 60;
#在一次長連接上所允許請求的資源的最大數量
keepalive_requests 3;
#導入其他路徑的配置文件
include /yinzhengjie/softwares/nginx/conf.d/*.conf;
}
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]#
2>.修改子配置文件
[root@node101.yinzhengjie.org.cn ~]# vim /yinzhengjie/softwares/nginx/conf.d/share.conf
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf.d/share.conf
server {
listen 80;
server_name node101.yinzhengjie.org.cn;
location / {
root /yinzhengjie/data/web/nginx/static;
index index.html;
}
location /download {
root /yinzhengjie/data/web/nginx;
autoindex on;
autoindex_exact_size on;
autoindex_localtime on;
limit_rate 10k;
}
location /upload {
root /yinzhengjie/data/web/nginx;
#排除GET方法外,其它方法均可以在當前localtion使用,而且我們只允許172.30.1.108節點來訪問。
limit_except GET {
allow 172.30.1.108;
deny all;
}
}
}
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]#
3>.創建數據目錄
[root@node101.yinzhengjie.org.cn ~]# mkdir /yinzhengjie/data/web/nginx/upload #該目錄需要自動手動創建出來
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ll /yinzhengjie/data/web/nginx/ #使用nginx -t命令時會自動創建client_body_temp_path所對應的目錄
total 4
drwxr-xr-x 2 root root 166 Dec 17 14:54 download
-rw-r--r-- 1 root root 43 Dec 17 13:04 index.html
drwxr-xr-x 3 root root 51 Dec 17 13:08 login
drwxr-xr-x 2 root root 44 Dec 17 12:54 static
drwx------ 2 nginx root 6 Dec 17 15:24 temp
drwxr-xr-x 2 root root 6 Dec 17 15:26 upload
[root@node101.yinzhengjie.org.cn ~]#
4>.重新加載nginx的配置文件
[root@node101.yinzhengjie.org.cn ~]# ps -ef | grep nginx | grep -v grep
root 2840 1 0 09:37 ? 00:00:00 nginx: master process nginx
nginx 5023 2840 0 14:48 ? 00:00:00 nginx: worker process
nginx 5024 2840 0 14:48 ? 00:00:00 nginx: worker process
nginx 5025 2840 0 14:48 ? 00:00:00 nginx: worker process
nginx 5026 2840 0 14:48 ? 00:00:00 nginx: worker process
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -s reload
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ps -ef | grep nginx | grep -v grep
root 2840 1 0 09:37 ? 00:00:00 nginx: master process nginx
nginx 5477 2840 7 16:10 ? 00:00:00 nginx: worker process
nginx 5478 2840 9 16:10 ? 00:00:00 nginx: worker process
nginx 5479 2840 10 16:10 ? 00:00:00 nginx: worker process
nginx 5480 2840 11 16:10 ? 00:00:00 nginx: worker process
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#
5>.IP地址為172.30.1.101的節點上傳文件到web服務器
[root@node101.yinzhengjie.org.cn ~]# curl -XPUT /etc/passwd http://node101.yinzhengjie.org.cn/upload #由於nginx只允許172.30.1.108節點可以使用所有方法(GET方法除外),當前IP地址被nginx拒絕了 curl: (3) <url> malformed <html> <head><title>403 Forbidden</title></head> <body bgcolor="white"> <center><h1>403 Forbidden</h1></center> <hr><center>nginx/1.14.2</center> </body> </html> [root@node101.yinzhengjie.org.cn ~]#
5>.IP地址為172.30.1.108的節點上傳文件到web服務器
[root@node108.yinzhengjie.org.cn ~]# curl -XPUT /etc/passwd http://node101.yinzhengjie.org.cn/upload #Nginx已經允許當前節點上傳數據,但是程序未支持上傳功能。 curl: (3) <url> malformed <html> <head><title>405 Not Allowed</title></head> <body bgcolor="white"> <center><h1>405 Not Allowed</h1></center> <hr><center>nginx/1.14.2</center> </body> </html> [root@node108.yinzhengjie.org.cn ~]#
