Nginx应用简介

 
Nginx的安装
 
在windows下的安装:
在windows下的很简单,下载下来直接运行就行了 。
 
在linux下的安装
首先安装pcre扩展
pcre-8.01.tar.gz
tar –zxvf  pcre-8.01.tar.gz
cd pcre-8.01
./configure
make
make install
zlib扩展
zlib-1.2.5.tar.gz
./configure
make
make install
openssl安装(貌似缓存需要这玩意儿)
openssl下载: http://www.openssl.org/source/  
OPENSSL不需要安装,只需要解压出来就行。
安装Nginx的时候,把openssl路径指定到解压出来的路径
也可以rpm安装,那样就不用在 ./configure后面加openssl的路径了。
 
最后就可以安装nginx了 参数也很简单(当然也可以复杂)
 ./configure --prefix=/usr/www/nginx  --with-openssl=/usr/www/openssl-1.0.0
make
make install
 
 
Nginx和php结合
 
 
只安装Nginx就只能支持静态的网页图片,要想运行php当然还得安装php,在windows上是很简单的
Nginx通过fast-cgi调用php,在windows上只要在配置文件中开启fast-cgi 然后运行php-cgi.exe就行了,Ngnix可以通过端口(默认是9000)找到对应的php服务.
开启fast-cgi 在php.ini中:
cgi.force_redirect = 1
cgi.fix_pathinfo = 1
cgi.rfc2616_headers = 1
启动php-cgi.exe的命令
D:/php/php-cgi.exe -b 127.0.0.1:9000 -c D:/php/php.ini
 
在nginx.conf中server中添加如下配置
location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
 
 
在linux上和在windows下原理是一样的,只不过fast-cgi的开启要复杂些。
首先php在linux下是通过php-fpm管理fast-cgi的,而php-fpm这个模块是通过补丁的方式加入到php中的,所以必须重新编译安装php。
步骤如下:
首先是必要的扩展
freetype-2.3.5
chmod -R 777 freetype-2.3.5
./configure
make
make install
 
libpng-1.2.25.tar.gz
tar -zxvf libpng-1.2.25.tar.gz
cd libpng-1.2.25
cd scripts/
mv makefile.linux ../makefile
cd ..
make
make install
 
jpegsrc.v6b.tar.gz
tar -zxvf jpegsrc.v6b.tar.gz
 mkdir  /usr/local/man
 mkdir  /usr/local/man/man1
cd jpeg-6b/
./configure --enable-shared
make
make test
make install
 
tar zxvf gd-2.0.33.tar.gz
cd gd-2.0.33
./configure --with-png --with-freetype --with-jpeg
make
make install
tar -zxvf libxml2-2.6.26.tar.gz
cd libxml2-2.6.26
./configure
make
make install
 
libmcrypt-2.5.8.tar.gz
tar -zxvf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8
./configure
make
make install
最后关键的是要将php-fpm补丁加入php的源码
$tar xzvf php-5.2.17.tar.gz
$gzip -cd php-5.2.17-fpm-0.5.14.diff.gz | patch -d php-5.2.17 -p1
然后编译
./configure --prefix=/usr/www/php --enable-fastcgi \
--enable-fpm --with-config-file-path=/usr/www/php/  \
--with-mysql=/usr/www/mysql \
--with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib \
--enable-force-cgi-redirect --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf \
 --enable-sockets --enable-zip --enable-soap
make
make test
make install
 
然后在php-fpm的配置文件中修改下面几行usr/www/php/etc/php-fpm.conf:
Unix user of processes
<value name="user">nobody</value>
Unix group of processes
<value name="group">nobody</value>
启动fast-cgi
/usr/www/php/sbin/php-fpm start
Nginx的配置和前面一样就行了
启动nginx
/usr/www/nginx/sbin/nginx
 
 
Nginx的配置
 
虚拟主机的配置
 
1.基于ip的虚拟主机
http
{
 #第一个虚拟主机
 server {
                #监听的IP和端口
                listen   127.0.0.1:80;
                #主机名
                server_name   127.0.0.1;
                #访问日志文件存放路径
                 access_log    logs/server1.access.log combined:
                 location /
                {
                       #默刻‘首页文件,顺序从左到右                   
                        index index.html index.htm;
                       #根目录
                       root  /data0/htdoc/server1
               }
          }
#第二个虚拟主机
server {
                 listen   127.0.0.2:80;
                 server_name   127.0.0.2;
                 access_log    logs/server2.access.log combined:
                 location /
                {
                     index index.html index.htm;
                     root  /data0/htdoc/server2
               }
          }
}
 
 
2.基于域名的虚拟主机
http
{
 #第一个虚拟主机
 server {
                #监听的IP和端口
                listen   80;
                #主机名
                server_name   www.aaa.com;
                #访问日志文件存放路径
                 access_log    logs/server1.access.log combined:
                 location /
                {
                       #默刻‘首页文件,顺序从左到右                   
                        index index.html index.htm;
                       #根目录
                       root  /data0/htdoc/server1
               }
          }
#第二个虚拟主机
server {
                listen   80;
                server_name   www.bbb.com;
                access_log    logs/server2.access.log combined:
                 location /
                 {
                        index index.html index.htm;
                        root  /data0/htdoc/server2
                 }
          }
}
 
 
Nginx的浏览器本地缓存设置
缓存30天
location ~.*\. (gif|png|jpg|swf)$
{
    expires   30d;
}
 
缓存10小时
location ~.*\. (css|js)$
{
    expires   10h;
}
  用途:使用本指令可以控制HTTP应答中的“Expires”和“Cache-Control”的Heade:头信
息(起到控制页面缓存的作用)。
Expires: 过期时刻
Cache-Control:max-age=300 缓存的最长时间
 
 
Nginx的负载均衡和反向代理
 
http {
...........
     upstream  192.168.1.10{
              server   192.168.1.11:80;
              server   192.168.1.12:80;
      }
     server {
        listen       80;
        server_name  192.168.1.10;
        location / {
             proxy_pass        http://192.168.1.10/;
             proxy_set_header   Host             $host;
             proxy_set_header   X-Real-IP        $remote_addr;
             proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
          }
   }
}
默认是轮询
 
还可以支持按权重分配
 upstream  192.168.1.10{
              server   192.168.1.11:80 weight=1;
              server   192.168.1.12:80 weight=2;
      }
如果按客户端的ip hash 就可以这样写
upstream  192.168.1.10{
              ip_hash;
              server   192.168.1.11:80;
              server   192.168.1.12:80;
      }
另外还有第三方的策略
 
3)、fair(第三方):按后端服务器的响应时间来分配请求,响应时间短的优先分配。
4)、url_hash(第三方)
 
 
Nginx的rewrite规则
 
 nginx rewrite规则相关指令有if, rewrite, set, return, break等,其中rewrite是最关键的指令。一个简单的nginx rewrit。规则语法如下:
rewrite  ^/new/(.*)\.html /new.php?id=$1 break;
break 终止rewrite规则
if指令用于条件判断
例如:if($http_user_agent ~ MSIE)
set 可以用来设置变量
set $varname ‘hello’;
return可返回一个状态码
例如 防盗链
location ~* \.(gif|jpg|png)$ {
valid_referers none blocked  localhost;
if ($invalid_referer) {
  rewrite   ^/(.*)   http://localhost:8080/404.nojpg;
 #return 404;
}
}
valid_referers 表示允许的来路
none意为不存在的Referer头
blocked意为根据防火墙伪装Referer头,如:“Referer: XXXXXXX”。
 
 
Nginx的缓存
 
首先,我们要按照以下步骤,把第三方的ngx_cache_purge模块编译安装到nginx中,用来清除指定URL的缓存,示例如代码9-1所示。
ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.00.tar.gz
http://labs.frickle.com/files/ngx_cache_purge-1.3.tar.gz
./configure一user=www-group=www --add-module=../ngx_cache_purge-1 .3
 
(3 ) Nginx配置文件(nginx.conf ):对扩展名为gif, jpg,jpeg,png,bmp的图片、, swf,  js,  css Flash, JavaScript, CSS文件开启Web缓存,其他文件不缓存,示例如代码9-2所示。
http {
...........
     proxy_temp_path /usr/www/nginx/proxy_temp_cache;
     proxy_cache_path  /usr/www/nginx/proxy_cache_dir levels=1:2        keys_zone=cache_one:200m inactive=1d max_size=30g;
     upstream  192.168.1.10{
              server   192.168.1.11:80;
              server   192.168.1.12:80;
      }
  
server {
        listen       80;
        server_name  192.168.1.10;
      #此地址用来清除缓存 例如url http://localhost/image/login.jpg 的缓存可以通过向url http://localhost/purge/image/login.jpg 发送请求来清除
       location ~ /purge(/.*)
       {
          allow            192.168.142.130;
          allow            192.168.142.1;
          deny             all;
          proxy_cache_purge    cache_one   $host$1$is_args$args;
       }
        location~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css)$ {
             proxy_pass        http://192.168.1.10/;
             proxy_cache_valid  200  12h;
             proxy_set_header   Host             $host;
             proxy_set_header   X-Real-IP        $remote_addr;
             proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
          }
   }
}
 
 
暂时就是这些了
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值