又一枚神器:nginx

   一直听说过nginx的大名,也自己装过,但是未直接使用其各种牛X的功能。

   今天试用了一下,只能用两字感叹:牛逼!比如它提供的配置检查功能,真是贴心极了,又比如我想要的静态内容浏览器端缓存功能,动态内容转发功能,都极其简单,不得不感叹啊。

   我们先来看看nginx的各种能力,然后列出一些收集的nginx各项能力的配置以及一些调优的文章,供日后备用。

   nginx我就不介绍了,大家都知道。其工作原理参考这里,里面也讲了nginx的模块开发http://blog.codinglabs.org/articles/intro-of-nginx-module-development.html

   安装也略过,apt-get可以轻松搞定。

   nginx的能力一览:

   1. 负载均衡

   2. 反向代理:整合后端的各种服务器和语言(PHP, PERL, TOMCAT),反向代理缓存,静态内容浏览器缓存,动静分离

   3. 安全:防盗链,防爬虫,HTTPS

   4. 其它:同memcache结合干各种层次的缓存,限速,自动裁剪图片,请求合并,集成lua,玩法太多了……

   这边列举一些常见的配置(均来源于网上),方便日后使用:

   nginx配置测试(假设nginx已经加入path,下同): nginx -t

   nginx平滑重启:首先找到master processor的pid,简单点用ps -ef | grep nginx,然后kill -HUP pid

   负载均衡

  1. http     
  2. {    
  3.   upstream  myserver {    
  4.     server   192.168.12.181:80 weight=3 max_fails=3 fail_timeout=20s;    
  5.     server   192.168.12.182:80 weight=1 max_fails=3 fail_timeout=20s;    
  6.     server   192.168.12.183:80 weight=4 max_fails=3 fail_timeout=20s;    
  7.   }    
  8.    
  9.   server    
  10.   {    
  11.     listen       80;    
  12.     server_name  www.domain.com 192.168.12.189;    
  13.     index index.htm index.html;    
  14.     root  /ixdba/web/wwwroot;      
  15.    
  16. location / {    
  17.  proxy_pass http://myserver;    
  18.  proxy_next_upstream http_500 http_502 http_503 error timeout invalid_header;    
  19.  include    /opt/nginx/conf/proxy.conf;    
  20.  }    
  21.   }    
  22. }    
  23.    

   虚拟主机

  1. http {    
  2.  server {    
  3.  listen          80;    
  4.  server_name     www.domain1.com;    
  5.  access_log      logs/domain1.access.log main;    
  6.  location / {    
  7.  index index.html;    
  8.  root  /web/www/domain1.com/htdocs;    
  9.  }    
  10.   }    
  11.  server {    
  12.  listen          80;    
  13.  server_name     www.domain2.com;    
  14.  access_log      logs/domain2.access.log main;    
  15.  location / {    
  16.  index index.html;    
  17.  root  /web/www/domain2.com/htdocs;    
  18.  }    
  19.   }    
  20.   include    /opt/nginx/conf/vhosts/www.domain2.com.conf;    
  21. }    
  1. server {    
  2.  listen          80;    
  3.  server_name     www.domain3.com;    
  4.  access_log      logs/domain3.access.log main;    
  5.  location / {    
  6.  index index.html;    
  7.  root  /web/www/domain3.com/htdocs;    
  8.  }    
  9.   }    


   反向代理,整合tomcat

  1. location / {  
  2.                         proxy_pass http://localhost:8080;  
  3.                         proxy_redirect off;  
  4.                         proxy_set_header Host $host;  
  5.                         proxy_set_header X-Real-IP $remote_addr;  
  6.                         proxy_set_header X-Forworded-For $proxy_add_x_forwarded_for;  
  7.                         proxy_connect_timeout 90;  
  8.                         proxy_send_timeout 90;  
  9.                         proxy_read_timeout 90;  
  10.                         proxy_buffer_size 4k;  
  11.                         proxy_buffers 4 32k;  
  12.                         proxy_busy_buffers_size 64k;  
  13.                         proxy_temp_file_write_size 64k;  
  14.                 }  

   反向代理,分离静态页面,并设置静态页面缓存时间(if-modify-since)

  1. location /img/ {  
  2.                         alias /www/root/img/;  
  3.                         expires 10d;  
  4.                 }  

  设置Cache-Control

  1. add_header    Cache-Control  max-age=3600;  

   安全,防盗链

  1. location ~* .(jpg|gif|png|swf|flv|wma|wmv|asf|mp3|mmf|zip|rar)$ {    
  2.         valid_referers none blocked *.ixdba1.net ixdba1.net;    
  3.         if ($invalid_referer) {    
  4.         rewrite ^/ http://www.ixdba.net/img/error.gif;    
  5.         #return 403;    
  6.        }    
  7.         }    
  8.         location /images {    
  9.         root /opt/nginx/html;    
  10.         valid_referers none blocked *.ixdba1.net ixdba1.net;    
  11.         if ($invalid_referer) {    
  12.                    return   403;    
  13.                                 }    
  14.                         }    


   安全,HTTPShttp://nginx.org/cn/docs/http/configuring_https_servers.html

原文地址:https://www.cnblogs.com/tonykan/p/3505879.html