Nginx连七八糟配置

来源:互联网 发布:传送带plc编程 编辑:程序博客网 时间:2024/04/29 15:08
//nginx使用gzip压缩提升网站速度
浏览器---请求----> 声明可以接受 gzip压缩 或 deflate压缩 或compress 或 sdch压缩
从http协议的角度看--请求头 声明 acceopt-encoding: gzip deflate sdch  (是指压缩算法,其中sdch是google倡导的一种压缩方式,目前支持的服务器尚不多)
服务器-->回应---把内容用gzip方式压缩---->发给浏览器----接收gzip压缩内容-----解码gzip----->浏览



注意:比较小的文件不必压缩,因为压缩率比较小, 比如100->80字节,而且压缩也是耗费CPU资源的


gzip配置的常用参数

gzip on|off;  #是否开启gzip
gzip_buffers 32 4K| 16 8K #缓冲(压缩在内存中缓冲几块? 每块多大?)
gzip_comp_level [1-9] #推荐6 压缩级别(级别越高,压的越小,越浪费CPU计算资源)
gzip_disable #正则匹配UA 什么样的Uri不进行gzip
gzip_min_length 200 # 开始压缩的最小长度(再小就不要压缩了,意义不在)
gzip_http_version 1.0|1.1 # 开始压缩的http协议版本(可以不设置,目前几乎全是1.1协议)
gzip_proxied          # 设置请求者代理服务器,该如何缓存内容
gzip_types text/plain  application/xml # 对哪些类型的文件用压缩 如txt,xml,html ,css
gzip_vary on|off  # 是否传输gzip压缩标志



--------------------------------------------------------------------------------------------------------------------------

//nginx使用expires设置缓存图片,提高网站性能

在location或if段里,来写.
格式  expires 30s;
         expires 30m;
         expires 2h;
         expires 30d;
(注意:服务器的日期要准确,如果服务器的日期落后于实际日期,可能导致缓存失效)

location ~* \.(jpg|jpeg|gif|png){
expires 1d;
}



----------------------------------------------------------------------------------------------------------------------------

nginx反向代理(proxy)

location ~ \.php$ {
proxy_pass http://ip:端口号(Apache)
}

-----------------------------------------------------------------------------------------------------------------------------

//nginx配置处理PHP

location ~ \.php$ {
fastcgi_pass  127.0.0.1:9000;
fastcgi_index  index.php;
fastcgi_param  SCRIPT_FILENAME /usr/local/nginx/html/$fastcgi_script_name;
include  fastcgi_params;
}


------------------------------------------------------------------------------------------------------------------------------

//nginx实现负载均衡(upstream)

反向代理后端如果有多台服务器,自然可形成负载均衡,


在nginx配置文件中http下
upstream imgserver {
server 192.168.1.200:81  weight=1 max_fails=2 fail_timeout=3;
server 192.168.1.200:82  weight=1 max_fails=2 fail_timeout=3;
}


location ~* \.(jpg|jpeg|gif|png)$ {
proxy_pass http://imgserver;
}

//做完负载以后,后端服务器只知道前端的IP而不是用户客户端的IP,所以要设置

在nginx配置文件http中
log_format  main '$remote_addr - $remote_user [$time_local], "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

location ~ \.php$ {
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://ip:端口号(Apache);
}


------------------------------------------------------------------------------------------------------------------------------

//Nginx 第三方模块的安装
以ngx_http_php_memcache_standard_balancer-master为例
1:解压 到 path/ngx_module

配置:
./configure --prefix=/xxx/xxx --add_module=/path/ngx_module
编译 安装
Make && make instal

配置memcache集群

    upstream memserver {  把用到的memcached节点,声明在一个组里
        hash_key $request_uri;  // hash计算时的依据,以uri做依据来hash
        server localhost:11211;
        server localhost:11212;
    }
Location里

        location / {
           # root   html;
           set $memcached_key $uri;
           memcached_pass memserver;  // memserver为上面的memcache节点的名称
           error_page 404 /writemem.php;
           index  index.php index.html index.htm;
        }



在nginx中做集群与负载均衡,步骤都是一样的
Upstream {}模块 把多台服务器加入到一个组
然后 memcached_pass, fastcgi_pass, proxy_pass ==> upstream组

默认的负载均衡的算法:
是设置计数器,轮流请求N台服务器.
可以安装第3方模式,来利用uri做hash等等.

如http://wiki.nginx.org/NginxHttpUpstreamConsistentHash 
这个模块就是用一致性hash来请求后端结节,并且其算法,与PHP中的memcache模块的一致性hash算法,兼容.

安装该模块后:
Nginx.conf中

    upstream memserver {
        consistent_hash $request_uri;
        server localhost:11211;
        server localhost:11212;
    }


在PHP.ini中,如下配置

memcache.hash_strategy = consistent

这样: nginx与PHP即可完成对memcached的集群与负载均衡算法.
0 0