1. 程式人生 > >二進位制nginx靜態資源相關模組

二進位制nginx靜態資源相關模組

安裝二進位制nginx包指令碼

[[email protected] ~]# cat nginx_yum.sh 
#!/bin/bash
cat> /etc/yum.repos.d/nginx.repo <<EOF
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/\$basearch/
gpgcheck=0
enabled=1
EOF
#echo "清除yum快取"
#yum clean all &>/dev/null
#yum makecache &>/dev/null
echo "準備下載nginx"
yum -y install nginx &>/dev/null 
rpm -q nginx

配置檔案中設定worker_connections 10240 [[email protected] ~]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: [warn] 10240 worker_connections exceed open file resource limit: 1024 nginx: configuration file /etc/nginx/nginx.conf test is successful 10240併發連線已經超過了開啟檔案的資源限制:1024 修改: [[email protected]

~]# ulimit -n 10240 [[email protected] ~]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful

檔案壓縮模組: 檔案壓縮和預讀功能 gzip_static on; #預讀gzip tcp_nopush on; gzip on; #開啟Gzip壓縮 gzip_http_version 1.1; #針對http協議1.1,預設就是可以不寫 gzip_comp_level 2; #壓縮過高效能反而會差 gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png; #進行壓縮的檔案型別 gzip_min_length 1k; #不壓縮臨界值,大於1k的才壓縮,一般不用改

ngx_http_headers_module模組 提供了兩個重要的指令add_header和expires,來新增 “Expires” 和 “Cache-Control” 頭欄位,對響應頭新增任何域欄位。 add_header可以用來標示請求訪問到哪臺伺服器上。 語法: add_header name value; 配置段: http, server, location, if in location 對響應程式碼為200,201,204,206,301,302,303,304,或307的響應報文頭欄位新增任意域。

Expires設定伺服器端快取過期時間 Syntax: expires [modified] time; expires epoch | max | off; Default: expires off; Context: http, server, location, if in location 例子 location / { root /app/webroot; index index.html; expires 24h; } (快取的是內容) ngx_http_referer_module模組 防止網站資源被盜用: Syntax: valid_referers none | blocked | server_names | string …; Default:— Context: server, location 例子:

location ~ \.(jpg|gif|png)$ {
root  /webroot/image;
valid_referers none blocked  *.regular.top server_names  ~\.google  \. ~baidu ;
##除了自己(regular.top)和谷歌(匹配帶有.google的)和百度之外的所有都不能用/webroot/image下的圖片
if ($invalid_referer) {
return 403;  #錯誤源域名不在這個列表中,那麼$invalid_referer等於1,返回403錯誤
}
}

實驗錯誤:圖片要給nginx讀的許可權