关于一些nginx的高级扩展应用

nginx.conf 配置解释

 
 
  1. 详解user   www www; 

  2. 定义 Nginx 运行的用户及组 

  3. worker_processes 8; #[ debug | info | notice | warn | error | crit ] 

  4. error_log /data1/logs/nginx_error.log crit; pid 

  5. /usr/local/webserver/nginx/nginx.pid; #Specifies the value for maximum file descriptors that can be opened by this process. 

  6. worker_rlimit_nofile 65535; 

  7. 一个 nginx 进程打开的最多文件描述符数目,理论值应该是最多打开文件数(ulimit 

  8. -n) nginx 进程数相除,与但是 nginx 分配请求并不是那么均匀,所以最好与 ulimit -n的值保持一致。 

  9. # use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; 

  10. events use epoll; 参考事件模型 

  11. worker_connections 65535; 每个进程最大连接数(最大连接=连接数 x 进程数) #设定 http 服务器 

  12. http include 

  13. mime.types; 文件扩展名与文件类型映射表 

  14. default_type application/octet-stream; #默认文件类型 

  15. #charset gb2312; 默认编码 

  16. server_names_hash_bucket_size 128; #服务器名字的 hash 表大小 

  17. client_header_buffer_size 32k; 上传文件大小限制 

  18. large_client_header_buffers 4 32k; 设定请求缓 

  19. client_max_body_size 8m; 设定请求缓 

  20. sendfile on; #开启高效文件传输模式 

  21. tcp_nopush 

  22. on; 防止网络阻塞 

  23. tcp_nodelay on; 防止网络阻塞 

  24. keepalive_timeout 60; 超时时间 

  25. #FastCGI 是为了改善网站的性能--减少资源占用,提高访问速度.有关 fastCGI 的 

  26. 详细资料请参阅:http://www.fastcgi.com 

  27. fastcgi_connect_timeout 300; 

  28. fastcgi_send_timeout 300; 

  29. fastcgi_read_timeout 300; 

  30. fastcgi_buffer_size 64k; 

  31. fastcgi_buffers 4 64k; 

  32. fastcgi_busy_buffers_size 128k; 

  33. fastcgi_temp_file_write_size 128k; 

  34. gzip on; 

  35. gzip_min_length 1k; #最小压缩文件大小 

  36. gzip_buffers 

  37. 4 16k; #压缩缓冲区 

  38. gzip_http_version 1.0; 

  39. #压缩版本(默认 1.1,前端为 squid2.5 使用 1.0 

  40. gzip_comp_level 2; 压缩等级 

  41. gzip_types 

  42. text/plain application/x-javascript text/css application/xml; 

  43. 压缩类型,默认就已经包含 text/html 所以下面就不用再写了,当然写上去的话,也 

  44. 不会有问题,但是会有一个 warn 

  45. gzip_vary on; 

  46. #limit_zone crawler $binary_remote_addr 10m; server listen   80; 

  47. server_name www.opendoc.com.cn 

  48. index index.html index.htm index.php; 

  49. root /data0/htdocs/opendoc; 

  50. location ~ .*\.(php|php5)?$ #fastcgi_pass unix:/tmp/php-cgi.sock; 

  51. fastcgi_pass 127.0.0.1:9000; 

  52. fastcgi_index index.php; 

  53. include fcgi.conf; #对图片缓存 

  54. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ expires 30d; #对 JS CSS 缓存 

  55. location ~ .*\.(js|css)?$ expires  1h; #日志设定 

  56. log_format access '$remote_addr - $remote_user [$time_local] "$request" ' 

  57. '$status $body_bytes_sent "$http_referer" '  '"$http_user_agent" $http_x_forwarded_for'; 

  58. #日志的格式

  59. access_log /data1/logs/access.log access; 

nginx判断手机移动设备用户的方法

有两种方法

一种是用语言进行判断,比如用php的 $_SERVER['User-Agent']  

 
 
  1. <?php

  2. function is_mobile(){  

  3.     // returns true if one of the specified mobile browsers is detected  

  4.     $regex_match="/(nokia|iphone|android|motorola|^mot\-|softbank|foma|docomo|kddi|up\.browser|up\.link|";  

  5.     $regex_match.="htc|dopod|blazer|netfront|helio|hosin|huawei|novarra|CoolPad|webos|techfaith|palmsource|";  

  6.     $regex_match.="blackberry|alcatel|amoi|ktouch|nexian|samsung|^sam\-|s[cg]h|^lge|ericsson|philips|sagem|wellcom|bunjalloo|maui|";      

  7.     $regex_match.="symbian|smartphone|midp|wap|phone|windows ce|iemobile|^spice|^bird|^zte\-|longcos|pantech|gionee|^sie\-|portalmmm|";  

  8.     $regex_match.="jig\s browser|hiptop|^ucweb|^benq|haier|^lct|opera\s*mobi|opera\*mini|320x320|240x320|176x220";  

  9.     $regex_match.=")/i";          

  10.     return isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE']) or preg_match($regex_match, strtolower($_SERVER['HTTP_USER_AGENT']));  

  11. }  

  12. /*  

  13. allow the user a way to force either the full or mobile versions of the site - use a GET parameter on requests:  

  14. include likes to both versions of the site w/ the special force mode parameters, 'mobile' and 'full':  

  15. <ahref="View'>http://www.php100.com/?mobile">View Mobile Site</a>

  16. <ahref="View'>http://www.php100.com/?full">View Full Site</a>

  17. Always check for 'mobile' or 'full' parameters before accounting for any User-Agent conditions:  

  18. */  

  19. if ($_GET['mobile']) {  

  20.  $is_mobile = true;  

  21. }  

  22. if ($_GET['full']) {  

  23.  $is_mobile = false;  

  24. }  

  25. if($is_mobile) {  

  26.     //it's a mobile browser, do something  

  27.     header("Location: http://wap.baidu.com");  

  28. } else {  

  29.     //it's not a mobile browser, do something else  

  30.     header("Location: http://www.baidu.com");  

  31.     // or instead of a redirect, simply build html below  

  32. }  

  33. ?>

还有一种是用nginx的来判断

 
 
  1. if ($http_user_agent ~* (mobile|nokia|iphone|ipad|android|samsung|htc|blackberry)) { 

  2. // 添加你需要处理的语句,比如rewrite等 

 

可能一些设备可能没有识别的,大家可以看分析日志,然后把 User-Agent的关键字写到if里面~

 

nginx的配置文件if语句是不支持“并且”和“或者”这样的多重条件判断的。在一些情况下,我们又需要if语句进行多个条件的判断,那么如何来实现呢?我们可以利用nginx的set语句设置变量的方法来解决。

假设我们需要对 /123/ 路径进行rewrite,但同时要排除 /123/images/ 路径不对该路径进行rewrite,可以采用下面的解决办法:

 
 
  1. set $doRewrite "0"; 

  2. if ($request_uri ~ ^/123/) { 

  3. set $doRewrite "1"; 

  4. if ($request_uri ~ ^/123/images/) { 

  5. set $doRewrite "0"; 

  6. if ($doRewrite = "1") { 

  7. // do rewrite 


还有一个实例

这个意思是   判断真是的ip,然后根据ip做一些操作~  这里用的map映射

 
 
  1. map $http_x_forwarded_for $deny_access { 

  2.     default     0; 

  3.     1.2.3.4     1; 

  4.     1.2.3.5     1; 

  5.     1.2.3.6     1; 

  6. if ($deny_access = 1) { 

  7.     return 403; 

 

防盗链的一些个配置

 
 
  1. location ~* \.(gif|png|jpg|bmp|swf|flv)$ { 

  2.     valid_referers none blocked www.ruifengyun.com ruifengyun.com; 

  3.     if ($invalid_referer) { 

  4.             return 403; 

  5.     } 

以上的例子可以实现扩展名为 gif,png,jpg,bmp,swf,flv的url防止被盗链。如果你需要其它的url防止被盗链,添加相应的后缀即可。

也可以 把return 403 替换成 #rewrite ^/ http://ruifnegyun.com/404.jpg;     这样可以用另一种方法推广自己的网站

nginx的限速的规则

 

配置简单,只需3行

 
 
  1. http{ 

  2.     …… 

  3.     limit_zone one $binary_remote_addr 10m; 

  4.     …… 

  5.     server { 

  6.         location / { 

  7.             …… 

  8.             limit_conn one 2; 

  9.             limit_rate 40k; 

  10.         } 

  11.     } 

意思是:limit_zone针对每个IP定义一个存储session状态的容器。这个示例中定义了一个名叫one的10m大小的容器,这个名字会在后面的limit_conn中使用。limit_conn指定每个访客只能建立两条链接,limit_rate限制每条链接的速度不超过40K。所以,以上配置限制用户访问此站点总速度上限为80K。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值