nginx.conf配置解釋
詳解user www www;
定義 Nginx 運行的用戶及組
worker_processes 8; #[ debug | info | notice | warn | error | crit ]
error_log /data1/logs/nginx_error.log crit; pid
/usr/local/webserver/nginx/nginx.pid; #Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;
一個 nginx 進程打開的最多文件描述符數目,理論值應該是最多打開文件數(ulimit
-n) nginx 進程數相除,與但是 nginx 分配請求並不是那麼均勻,所以最好與 ulimit -n的值保持一致。
# use [ kqueue | rtsig | epoll | /dev/poll | select | poll ];
events use epoll; 參考事件模型
worker_connections 65535; 每個進程最大連接數(最大連接=連接數 x 進程數) #設定 http 服務器
http include
mime.types; 文件擴展名與文件類型映射表
default_type application/octet-stream; #默認文件類型
#charset gb2312; 默認編碼
server_names_hash_bucket_size 128; #服務器名字的 hash 表大小
client_header_buffer_size 32k; 上傳文件大小限制
large_client_header_buffers 4 32k; 設定請求緩
client_max_body_size 8m; 設定請求緩
sendfile on; #開啟高效文件傳輸模式
tcp_nopush
on; 防止網絡阻塞
tcp_nodelay on; 防止網絡阻塞
keepalive_timeout 60; 超時時間
#FastCGI 是為了改善網站的性能--減少資源占用,提高訪問速度.有關 fastCGI 的
詳細資料請參閱:http://www.fastcgi.com
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
gzip on;
gzip_min_length 1k; #最小壓縮文件大小
gzip_buffers
4 16k; #壓縮緩沖區
gzip_http_version 1.0;
#壓縮版本(默認 1.1,前端為 squid2.5 使用 1.0
gzip_comp_level 2; 壓縮等級
gzip_types
text/plain application/x-javascript text/css application/xml;
壓縮類型,默認就已經包含 text/html 所以下面就不用再寫了,當然寫上去的話,也
不會有問題,但是會有一個 warn
gzip_vary on;
#limit_zone crawler $binary_remote_addr 10m; server listen 80;
server_name www.opendoc.com.cn
index index.html index.htm index.php;
root /data0/htdocs/opendoc;
location ~ .*\.(php|php5)?$ #fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fcgi.conf; #對圖片緩存
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ expires 30d; #對 JS CSS 緩存
location ~ .*\.(js|css)?$ expires 1h; #日志設定
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" $http_x_forwarded_for';
#日志的格式
access_log /data1/logs/access.log access;
}
nginx判斷手機移動設備用戶的方法
有兩種方法
一種是用語言進行判斷,比如用php的 $_SERVER['User-Agent']
<?php
function is_mobile(){
// returns true if one of the specified mobile browsers is detected
$regex_match="/(nokia|iphone|android|motorola|^mot\-|softbank|foma|docomo|kddi|up\.browser|up\.link|";
$regex_match.="htc|dopod|blazer|netfront|helio|hosin|huawei|novarra|CoolPad|webos|techfaith|palmsource|";
$regex_match.="blackberry|alcatel|amoi|ktouch|nexian|samsung|^sam\-|s[cg]h|^lge|ericsson|philips|sagem|wellcom|bunjalloo|maui|";
$regex_match.="symbian|smartphone|midp|wap|phone|windows ce|iemobile|^spice|^bird|^zte\-|longcos|pantech|gionee|^sie\-|portalmmm|";
$regex_match.="jig\s browser|hiptop|^ucweb|^benq|haier|^lct|opera\s*mobi|opera\*mini|320x320|240x320|176x220";
$regex_match.=")/i";
return isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE']) or preg_match($regex_match, strtolower($_SERVER['HTTP_USER_AGENT']));
}
/*
allow the user a way to force either the full or mobile versions of the site - use a GET parameter on requests:
include likes to both versions of the site w/ the special force mode parameters, 'mobile' and 'full':
<a href="View'>http://www.php100.com/?mobile">View Mobile Site</a>
<a href="View'>http://www.php100.com/?full">View Full Site</a>
Always check for 'mobile' or 'full' parameters before accounting for any User-Agent conditions:
*/
if ($_GET['mobile']) {
$is_mobile = true;
}
if ($_GET['full']) {
$is_mobile = false;
}
if($is_mobile) {
//it's a mobile browser, do something
header("Location: http://wap.baidu.com");
} else {
//it's not a mobile browser, do something else
header("Location: http://www.baidu.com");
// or instead of a redirect, simply build html below
}
?>
還有一種是用nginx的來判斷
if ($http_user_agent ~* (mobile|nokia|iphone|ipad|android|samsung|htc|blackberry)) {
// 添加你需要處理的語句,比如rewrite等
}
可能一些設備可能沒有識別的,大家可以看分析日志,然後把 User-Agent的關鍵字寫到if裡面~
nginx的配置文件if語句是不支持“並且”和“或者”這樣的多重條件判斷的。在一些情況下,我們又需要if語句進行多個條件的判斷,那麼如何來實現呢?我們可以利用nginx的set語句設置變量的方法來解決。
假設我們需要對 /123/ 路徑進行rewrite,但同時要排除 /123/images/ 路徑不對該路徑進行rewrite,可以采用下面的解決辦法:
set $doRewrite "0";
if ($request_uri ~ ^/123/) {
set $doRewrite "1";
}
if ($request_uri ~ ^/123/images/) {
set $doRewrite "0";
}
if ($doRewrite = "1") {
// do rewrite
}
還有一個實例
這個意思是 判斷真是的ip,然後根據ip做一些操作~ 這裡用的map映射
map $http_x_forwarded_for $deny_access {
default 0;
1.2.3.4 1;
1.2.3.5 1;
1.2.3.6 1;
}
if ($deny_access = 1) {
return 403;
}
防盜鏈的一些個配置
location ~* \.(gif|png|jpg|bmp|swf|flv)$ {
valid_referers none blocked www.ruifengyun.com ruifengyun.com;
if ($invalid_referer) {
return 403;
}
}
以上的例子可以實現擴展名為 gif,png,jpg,bmp,swf,flv的url防止被盜鏈。如果你需要其它的url防止被盜鏈,添加相應的後綴即可。
也可以 把return 403 替換成 #rewrite ^/ http://ruifnegyun.com/404.jpg; 這樣可以用另一種方法推廣自己的網站
nginx的限速的規則
配置簡單,只需3行
http{
……
limit_zone one $binary_remote_addr 10m;
……
server {
location / {
……
limit_conn one 2;
limit_rate 40k;
}
}
}
意思是:limit_zone針對每個IP定義一個存儲session狀態的容器。這個示例中定義了一個名叫one的10m大小的容器,這個名字會在後面的limit_conn中使用。limit_conn指定每個訪客只能建立兩條鏈接,limit_rate限制每條鏈接的速度不超過40K。所以,以上配置限制用戶訪問此站點總速度上限為80K。