nginx--学习总结

最近复习了一下nginx,由于要考试了,大概总结一下

下载地址: http://nginx.org/download/nginx-1.4.2.tar.gz
安装准备: nginx依赖于pcre库,要先安装pcre
yum install pcre pcre-devel
cd /usr/local/src/
wget http://nginx.org/download/nginx-1.4.2.tar.gz
tar zxvf nginx-1.4.2.tar.gz
cd nginx-1.4.2
./configure --prefix=/usr/local/nginx
make && make install
(unbantu下类似主要是记得安装pcre库)

启动:
cd /usr/local/nginx, 看到如下4个目录
./
....conf 配置文件
... html 网页文件
...logs 日志文件
...sbin 主要二进制程序

[root@localhost nginx]# ./sbin/nginx
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
....
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()

不能绑定80端口,80端口已经被占用
(有时是自己装了apache,nginx等,还有更多情况是操作系统自带了apache并作为服务启动)
解决: 把占用80端口的软件或服务关闭即可.

nginx信号控制

TERM, INT -------Quick shutdown
QUIT --------Graceful shutdown 优雅的关闭进程,即等请求结束后再关闭
HUP -------- Configuration reload ,Start the new worker processes with
a new configuration Gracefully shutdown the old worker processes
改变配置文件,平滑的重读配置文件
USR1- ---------Reopen the log files 重读日志,在日志按月/日分割时有用
USR2 --------Upgrade Executable on the fly 平滑的升级
WINCH ---------Gracefully shutdown the worker processes 优雅关闭旧的进程(配合USR2来进行升级)

具体语法:
Kill -信号选项 nginx的主进程号
Kill -HUP 4873

Kill -信号控制 cat /xxx/path/log/nginx.pid

Kil; -USR1 cat /xxx/path/log/nginx.pid

还有另外一种
-s signal : send signal to a master process: stop, quit, reopen, reload
nginx -s reload/stop/quit/reopen 重读配置/停止nginx/平滑的停止/重读日志

Nginx配置段

// 全局区
worker_processes 1; // 有1个工作的子进程,可以自行修改,但太大无益,因为要争夺CPU,一般设置为 CPU数*核数

Event {
// 一般是配置nginx连接的特性
// 如1个worker能同时允许多少连接
worker_connections 1024; // 这是指 一个子进程最大允许连1024个连接
}

http { //这是配置http服务器的主要段
Server1 { // 这是虚拟主机段,一个虚拟主机

        Location {  //定位,把特殊的路径或文件再次定位 ,如image目录单独处理
        }             /// 如.php单独处理//uri的处理

 }

 Server2 {
 }

}

例子1: 基于域名的虚拟主机

server {
    listen 80;  #监听端口,默认80端口
    server_name a.com; #监听域名 比如www.etiantian.org

    location / {
            root /var/www/a.com;   #根目录定位
            index index.html;
    }
}

server {
listen 80; #监听端口,默认80端口
server_name b.com; #监听域名 比如bbs.etiantian.org

    location / {
            root /var/www/b.com;   #根目录定位
            index index.html;
    }
}

详解:
mkdir /var/www/{a.com,b.com}/ -p
echo "a.com"> /var/www/a.com/index.html
echo "b.com"> /var/www/b.com/index.html
检查配置文件并重新加载nginx
./sbin/nginx -t
./sbin/nginx -s reload
然后修改/etc/hosts
vim /etc/hosts
192.168.52.128 a.com b.com
然后用浏览器或者curl测试
curl a.com // 结果a.com
curl b.com //结果a.com
在windows下测试需要修改:
win+r 键入drivers 进入etc下的hosts加入
192.168.52.128 a.com b.com
即可通过浏览器测试a.com,b.com成功!

例子2: 基于端口的虚拟主机配置

server {
    listen 8080;
    server_name 192.168.1.204;//ip,域名,unix等

    location / {
            root /var/www/html8080;
            index index.html;
    }
}

server {
listen 8081;
server_name 192.168.1.204;//ip,域名,unix等

    location / {
            root /var/www/html8080;
            index index.html;
    }
}

详解:基于端口的虚拟主机主要是通过修改端口来实现
可以域名不一样也可以一样,如果域名一样则通过端口区分
检查语法并重新启动nginx
然后测试
curl http://192.168.1.204:8080
curl http://192.168.1.204:8081//http一般可以省略

例3:基于ip的虚拟主机
server {
listen 192.168.52.129:80;
server_name bbs.com;
location / {
root html/bbs;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 192.168.52.128:80;
server_name www.com;
location / {
root html/www;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
详解:添加ip
ip addr { add | del } IFADDR dev STRING//语法

ip addr add 192.168.52.129/24 dev etho
ip addr //查看添加的ip
../sbin/nginx -t
../sbin/nginx -s reload
检测:
curl 192.168.52.128
curl 192.168.52.129

ok!

一个小技巧:
可以用:egrep -v "#|^$" nginx.conf.defalut>nginx.conf//可以去掉配置文件中的空格和#号

include 方法总结
简单的说就是:把一个复杂的nginx分模块引进

如:
cat -n nginx.conf

10 server {
11 listen 192.168.52.129:80;
12 # server_name bbs.com;
13 location / {
14 root html/bbs;
15 index index.html index.htm;
16 }
17 error_page 500 502 503 504 /50x.html;
18 location = /50x.html {
19 root html;
20 }
21 }
22 server {
23 listen 192.168.52.128:80;
24 # server_name www.com;
25 location / {
26 root html/www;
27 index index.html index.htm;
28 }
29 error_page 500 502 503 504 /50x.html;
30 location = /50x.html {
31 root html;
32 }
33 }
然后分割:

sed -d "10,21p" nginx.conf
sed -n "10,21p" nginx.conf
mkdir extra
sed -n "10,21p" nginx.conf>bbs.conf//取nginx.conf的第10,21 行到bbs.conf
cat -n nginx.conf
sed -n "22,33p" nginx.conf>com.conf

在nginx中配置如下:
http {
include       mime.types;
default_type  application/octet-stream;
sendfile        on;
keepalive_timeout  65;
include extra/*.conf; #引入配置文件
}     
变得很简单!

nginx别名配置

 server {
    listen       80;
   server_name  bbs.com bbs1.com;
    location / {
        root   html/bbs;
        index  index.html index.htm;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
}

vi    /etc/hosts
192.168.52.128 www.com bbs.com www1.com bbs1.com
重新加载配置即可!

监控nginx状态,
1.编译时需要--with-http_stub_status_module支持
2.开启
cat>>./extra/status.conf<<EOF
#starus
server {
    listen       80;
   server_name  status.com;
    location / {
      stub_status on;
                access_log off;
    }
        }
EOF
vi /etc/hosts 加入
192.168.52.128 www.com bbs.com www1.com bbs1.com status.com
重新加载配置文件并在浏览器测试

日志管理
我们观察nginx

的server段,可以看到如下类似信息
#access_log logs/host.access.log main;
这说明 该server, 它的访问日志的文件是 logs/host.access.log ,(路径为相对路径,即nginx的安装路径)
使用的格式”main”格式.
除了main格式,你可以自定义其他格式.

main格式是什么?
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

main格式是我们定义好一种日志的格式,并起个名字,便于引用.
以上面的例子, main类型的日志,记录的 remote_addr.... http_x_forwarded_for等选项.
其中有两个相对比较重要的参数,后面会经常遇到
http_user_agent-----客户端的代理信息,即用的那种浏览器可以通过它获得比如MSIE Firefox,andriod,iphone等
http_x_forwarded_for 相当于网路访问路径,比如代理服务器从哪儿来的,都可以通过设置它来获得

1: 日志格式 是指记录哪些选项
默认的日志格式: main
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

如默认的main日志格式,记录这么几项
远程IP- 远程用户/用户时间 请求方法(如GET/POST) 请求体body长度 referer来源信息
http-user-agent用户代理/蜘蛛 ,被转发的请求的原始IP

http_x_forwarded_for:在经过代理时,代理把你的本来IP加在此头信息中,传输你的原始IP

字段分别对应:
time_local 本地时间戳

host 请求host地址

remote_addr 远程请求地址

request 请求uri

request_time 整个请求的总时间

body_bytes_sent 请求文件内容大小

status http请求状态

upstream_addr 后台提供服务的地址(即转发处理的目标地址)

upstream_reponse_time 请求时,upstream的响应时间

upstream_status upstream状态

http_refer url跳转来源

http_user_agent 用户终端浏览器的UserAgent

2: 声明一个独特的log_format并命名

log_format  mylog '$remote_addr- "$request" '
                 '$status $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for"';

在下面的server/location,我们就可以引用 mylog

在server段中,这样来声明
Nginx允许针对不同的server做不同的Log ,(有的web服务器不支持,如lighttp)

access_log logs/access_8080.log mylog;
声明log log位置 log格式;

访问日志最好基于虚拟主机配置!

例子:
log_format mylog '$remote_addr- "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

192.168.52.128 - - [26/Apr/2018:05:16:05 -0400] "GET / HTTP/1.1" 200 8 "-" "curl/7.22.0 (i686-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3" "-"

为了说明http_referer
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
include extra/bbs.conf;
include extra/com.conf;

include extra/status.conf;

} 
    在html/bbs目录下新建一个test.html 内容如下
    <a href="192.168.52.128 ">test nginx</a>
    在浏览器测试:192.168.52.128 /test.html并且点击:
    tail -f ../logs/acess_bbs.log追踪文件状态
    得到:

192.168.52.1 - - [26/Apr/2018:05:25:37 -0400] "GET /bbs.com HTTP/1.1" 404 571 "http://192.168.52.128/test.html" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36" "-"
由上可知:http_referer 为 "http://192.168.52.128/test.html" url跳转来源,即从哪儿来的

实际应用: shell+定时任务+nginx信号管理,完成日志按日期存储
分析思路:
凌晨00:00:01,把昨天的日志重命名,放在相应的目录下
再USR1信息号控制nginx重新生成新的日志文件

具体脚本:
#!/bin/bash
base_path='/usr/local/nginx/logs'
log_path=$(date -d yesterday +"%Y%m")
day=$(date -d yesterday +"%d")
mkdir -p $base_path/$log_path
mv $base_path/access.log $base_path/$logpath/access$day.log
#echo $base_path/$logpath/access$day.log
kill -USR1 cat /usr/local/nginx/logs/nginx.pid#重读日志

以上脚本用来避免日志文件过大,即每天分割日志

定时任务
Crontab 编辑定时任务(Crontab用法请自行查阅)
01 00 * /xxx/path/b.sh 每天0时1分(建议在02-04点之间,系统负载小)

location 语法
location 有”定位”的意思, 根据Uri来进行不同的定位.
在虚拟主机的配置中,是必不可少的,location可以把网站的不同部分,定位到不同的处理方式上.
比如, 碰到.php, 如何调用PHP解释器? --这时就需要location
location 的语法
location [=|~|~*|^~] patt {
}
中括号可以不写任何参数,此时称为一般匹配
也可以写参数
因此,大类型可以分为3种
location = patt {} [精准匹配]
location patt{} [一般匹配]
location ~ patt{} [正则匹配]

如何发挥作用?:
首先看有没有精准匹配,如果有,则停止匹配过程.
location = patt {
config A
}
如果 $uri == patt,匹配成功,使用configA
location = / {
root /var/www/html/;
index index.htm index.html;
}

location / {
root /usr/local/nginx/html;
index index.html index.htm;
}

如果访问  http://xxx.com/
定位流程是 
1: 精准匹配中 ”/” ,得到index页为  index.htm
2: 再次访问 /index.htm , 此次内部转跳uri已经是”/index.htm” ,
根目录为/usr/local/nginx/html
3: 最终结果,访问了 /usr/local/nginx/html/index.htm

再来看,正则也来参与.
location / {
root /usr/local/nginx/html;
index index.html index.htm;
}

location ~ image {
root /var/www/image;
index index.html;
}

如果我们访问 http://xx.com/image/logo.png
此时, “/” 与”/image/logo.png” 匹配
同时,”image”正则 与”image/logo.png”也能匹配,谁发挥作用?
正则表达式的成果将会使用.

图片真正会访问 /var/www/image/logo.png

location / {
root /usr/local/nginx/html;
index index.html index.htm;
}

location /foo {
root /var/www/html;
index index.html;
}
我们访问 http://xxx.com/foo
对于uri “/foo”, 两个location的patt,都能匹配他们
即 ‘/’能从左前缀匹配 ‘/foo’, ‘/foo’也能左前缀匹配’/foo’,
此时, 真正访问 /var/www/html/index.html
原因:’/foo’匹配的更长,因此使用之.;

rewrite 重写

重写中用到的指令
if (条件) {} 设定条件,再进行重写
set #设置变量
return #返回状态码
break #跳出rewrite
rewrite #重写

If 语法格式
If 空格 (条件) {
重写模式
}

条件又怎么写?
答:3种写法
1: “=”来判断相等, 用于字符串比较,字符串不需要加引号
if($request_method=POST)
{
return 405;
}
2: “~” 用正则来匹配(此处的正则区分大小写,大小写敏感)
~ 不区分大小写的正则
这里依然支持非! 如!~ ,!~

可以使用小括号对变量值进行截取,在花括号中使用$1...$9引用截取的值
if ($http_user_agent ~ MSIE)
{

}
3: -f -d -e来判断是否为文件,为目录,是否存在.

例子:

        if  ($remote_addr = 192.168.1.100) {
            return 403;
        }

if ($http_user_agent ~ MSIE) {
rewrite ^.*$ /ie.htm;
break; #(不break会循环重定向)
}

         if (!-e $document_root$fastcgi_script_name) {
            rewrite ^.*$ /404.html break;
        } 
        注, 此处还要加break,

以 xx.com/dsafsd.html这个不存在页面为例,
我们观察访问日志, 日志中显示的访问路径,依然是GET /dsafsd.html HTTP/1.1
提示: 服务器内部的rewrite和302跳转不一样.
跳转的话URL都变了,变成重新http请求404.html, 而内部rewrite, 上下文没变,
就是说 fastcgi_script_name 仍然是 dsafsd.html,因此 会循环重定向.

set 是设置变量用的, 可以用来达到多条件判断时作标志用.
达到apache下的 rewrite_condition的效果

如下: 判断IE并重写,且不用break; 我们用set变量来达到目的
if ($http_user_agent ~* msie) {
set $isie 1;
}

        if ($fastcgi_script_name = ie.html) {
            set $isie 0;
        }

        if ($isie 1) {
            rewrite ^.*$      ie.html;
        }

Rewrite语法
Rewrite 正则表达式 定向后的位置 模式

该指令通过正则表达式的使用来改变uri
rewrite myweb.com http://newweb.com/ permanent

现在我们希望上面的rewrite指令重写http://myweb.com/source 是办不到的
因为rewrite接收到的uri是/source,不包含myweb.com
另外,请求url中的请求指令也是不包含在rewrite指令接收到的uri内容中
http://myweb.com/source ?arg1=value1
rewrite 指令接收到的uri为/source 不包含?arg1=value1
注意:用url重写时, 正则里如果有”{}”,正则要用双引号包起来

301跳转
server {
listen 80;
server_name www1.com;
rewrite ^/(.*) http://www.com/test.html permanent;
}

测试:  curl -I www1.com
结果:

HTTP/1.1 301 Moved Permanently
Server: nginx/1.13.7
Date: Thu, 26 Apr 2018 14:15:59 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://www.com/test.html

nginx+php的编译
apache一般是把php当做自己的一个模块来启动的.
而nginx则是把http请求变量(如get,user_agent等)转发给 php进程,即php独立进程,与nginx进行通信. 称为 fastcgi运行方式.
因此,为apache所编译的php,是不能用于nginx的.

注意: 我们编译的PHP 要有如下功能:
连接mysql, gd, ttf, 以fpm(fascgi)方式运行
./configure --prefix=/usr/local/fastphp \
--with-mysql=mysqlnd \
--enable-mysqlnd \
--with-gd \
--enable-gd-native-ttf \
--enable-gd-jis-conv
--enable-fpm

网页内容的压缩编码与传输速度优化
我们观察news.163.com的头信息
请求:
Accept-Encoding:gzip,deflate,sdch
响应:
Content-Encoding:gzip
Content-Length:36093
再把页面另存下来,观察,约10W字节,实际传输的36093字节
原因-------就在于gzip压缩上.

原理:
浏览器---请求----> 声明可以接受 gzip压缩 或 deflate压缩 或compress 或 sdch压缩
从http协议的角度看--请求头 声明 acceopt-encoding: gzip deflate sdch (是指压缩算法,其中sdch是google倡导的一种压缩方式,目前支持的服务器尚不多)
服务器-->回应---把内容用gzip方式压缩---->发给浏览器
浏览<-----解码gzip-----接收gzip压缩内容----

推算一下节省的带宽:
假设 news.163.com PV 2亿
210^8 910^4 字节 ==
2
10^8 9 10^4 10^-9 = 12K*G = 18T
节省的带宽是非常惊人的

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压缩标志

注意:
图片/mp3这样的二进制文件,不必压缩
因为压缩率比较小, 比如100->80字节,而且压缩也是耗费CPU资源的.
比较小的文件不必压缩,
nginx的缓存设置 提高网站性能
对于网站的图片,尤其是新闻站, 图片一旦发布, 改动的可能是非常小的.我们希望 能否在用户访问一次后, 图片缓存在用户的浏览器端,且时间比较长的缓存.
可以, 用到 nginx的expires设置 .
nginx中设置过期时间,非常简单,
在location或if段里,来写.
格式 expires 30s;
expires 30m;
expires 2h;
expires 30d;
(注意:服务器的日期要准确,如果服务器的日期落后于实际日期,可能导致缓存失效)
另: 304 也是一种很好的缓存手段
原理是: 服务器响应文件内容是,同时响应etag标签(内容的签名,内容一变,他也变), 和 last_modified_since 2个标签值
浏览器下次去请求时,头信息发送这两个标签, 服务器检测文件有没有发生变化,如无,直接头信息返回 etag,last_modified_since
浏览器知道内容无改变,于是直接调用本地缓存.
这个过程,也请求了服务器,但是传着的内容极少.
对于变化周期较短的,如静态html,js,css,比较适于用这个方式

nginx反向代理服务器+负载均衡
用nginx做反向代理和负载均衡非常简单,
支持两个用法 1个proxy, 1个upstream,分别用来做反向代理,和负载均衡
以反向代理为例, nginx不自己处理php的相关请求,而是把php的相关请求转发给apache来处理.

----这不就是传说的”动静分离”,动静分离不是一个严谨的说法,叫反向代理比较规范.

反向代理后端如果有多台服务器,自然可形成负载均衡,
但proxy_pass如何指向多台服务器?
把多台服务器用 upstream指定绑定在一起并起个组名,
然后proxy_pass指向该组

默认的均衡的算法很简单,就是针对后端服务器的顺序,逐个请求.
也有其他负载均衡算法,如一致性哈希,需要安装第3方模块.

Nginx具体的压缩配置
常用以下配置
gzip on|off
gzip_buffers 4K|8K 缓冲(和硬盘块相当)
gzip_comp_level [1-9] 推荐6
gzip_disable 正则匹配如User-Agent,针对古老浏览器不压缩
gzip_min_length 200
gzip_http_version 1.0|1.1
gzip_types text/plain , application/xml (各mime之间,一定要加空格,不是逗号)
gzip_vary on|off

请求时,不支持gzip, 缓存服务器将会生成一份未gzip的内容.
请求时,支持gzip, 缓存服务器将会生成一份gzip的内容.

下次再请求时, 缓存服务器会考虑客户端的Accept-Encoding因素,并合理的返回信息

Nginx对于图片,js等静态文件的缓存设置
注:这个缓存是指针对浏览器所做的缓存,不是指服务器端的数据缓存.

主要知识点: location expires指令

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

设置并载入新配置文件,用firebug观察,
会发现 图片内容,没有再次产生新的请求,原因--利用了本地缓存的效果.

注: 在大型的新闻站,或文章站中,图片变动的可能性很小,建议做1周左右的缓存
Js,css等小时级的缓存.

如果信息流动比较快,也可以不用expires指令,
用last_modified, etag功能(主流的web服务器都支持这2个头信息)
原理是:
响应: 计算响应内容的签名, etag 和 上次修改时间
请求: 发送 etatg, If-Modified-Since 头信息.
服务器收到后,判断etag是否一致, 最后修改时间是否大于if-Modifiled-Since
如果监测到服务器的内容有变化,则返回304,
浏览器就知道,内容没变,直接用缓存.

304 比起上面的expires 指令
多了1次请求,
但是比200状态,少了传输内容.

相关配置实际例子
负载均衡加热备
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

upstream web_pools {
    
    server 10.0.0.9:80 weight=5 max_fails=10 fail_timeout=10s;
    server 10.0.0.10:80 weight=5;
    server 10.0.0.10:80 weight=5  backup;
}
    server {
        listen       80;
        server_name  www.etiantian.org;
        location / {
            root   html;
            index  index.html index.htm;
            proxy_pass http://web_pools;
            proxy_set_header Host  $host;#设置传给web的host信息
            proxy_set_header X-Forwarded-For $remote_addr;#web真正的ip
        }

    }
}

proxy.conf优化配置:

            proxy_redirect off; 

        proxy_set_header Host $host; 
        proxy_set_header X-Real-IP $remote_addr; 
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
        proxy_connect_timeout 90; 
        proxy_send_timeout 90; 
        proxy_read_timeout 90; 
        proxy_buffer_size 4k; 
        proxy_buffers 4 32k;
        proxy_busy_buffers_size 64k; 
proxy_temp_file_write_size 64k;

        动静分离
                worker_processes  1;

events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

upstream static_pools {
    server 10.0.0.9:80 weight=5 max_fails=10 fail_timeout=10s;
}

upstream dynamic_pools {
    server 10.0.0.10:80 weight=5;
}
    server {
        listen       80;
        server_name  www.etiantian.org;
        location / {
            root   html;
            index  index.html index.htm;
            proxy_pass http://dynamic_pools;
            include proxy.conf;
        }
        location ~ .*.(gif|jpg|jpeg|png|bmp|swf|css|js)$ {
        proxy_pass http://static_pools;
        include proxy.conf;
}
    }
}

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

upstream static_pools {
    server 10.0.0.9:80 weight=5 max_fails=10 fail_timeout=10s;
}

upstream dynamic_pools {
    server 10.0.0.10:80 weight=5;
}
    server {
        listen       80;
        server_name  www.etiantian.org;
        location / {
            root   html;
            index  index.html index.htm;
            proxy_pass http://dynamic_pools;
            include proxy.conf;
        }
       location /image/ { 
        proxy_pass http://static_pools;
        include proxy.conf;
       }

       location /dynamic/ {
        proxy_pass http://dynamic_pools;
        include proxy.conf;
       }

    }
}

//另一种分离,通过if条件达到和location同样的效果
        if ($request_uri   ~   "..(php|php5)$")
        {
                proxy_pass http://php_server_pools;
        }
        if ($request_uri   ~   "..(jsp|jsp|do|do)$")
        {
                proxy_pass http://java_server_pools;
        }

根据扩展名实现服务器的动静分离
location ~ ..(gif|jpg|jpeg|png|bmp|swf|css|js)$ {
proxy_pass http://static_pools;
include proxy.conf;
}
location ~ .
.(php|php3|php5)$ {
proxy_pass http://dynamic_pools;
include proxy.conf;
}

server {
        listen       80;
        server_name  www.etiantian.org;
        location / {
        if ($http_user_agent ~ "MSIE")
          {
            proxy_pass http://dynamic_pools;
          }
        if ($http_user_agent ~
 "Firefox")
          {
            proxy_pass http://static_pools;
            }
        proxy_pass http://dynamic_pools;
        include proxy.conf;
    }

    extra/proxy.conf

#允许客户端请求的最大的单个文件字节数
        client_max_body_size     10m;
        #缓冲区代理缓冲用户端请求的最大字节数 可以理解为先保存到本地再传给用户
        client_body_buffer_size  128k;
        #跟后端服务器连接的超时时间_发起握手等候响应超时时间
        proxy_connect_timeout    600;
        #连接成功后_等候后端服务器响应时间_其实已经进入后端的排队之中等候处理
        proxy_read_timeout       600;
        #后端服务器数据回传时间_就是在规定时间之内后端服务器必须传完所有的数据
        proxy_send_timeout       600;
        #代理请求缓存区_这个缓存区间会保存用户的头信息以供Nginx进行规则处理_一般只要能保存下头信息即可
        proxy_buffer_size        8k;
        #同上 告诉Nginx保存单个用的几个Buffer 最大用多大空间
        proxy_buffers            4 32k;
        #如果系统很忙的时候可以申请更大的proxy_buffers 官方推荐*2
        proxy_busy_buffers_size 64k;
        #proxy缓存临时文件的大小
        proxy_temp_file_write_size 64k;

一些优化参数
net.ipv4.tcp_fin_timeout = 2
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_keepalive_time = 600
net.ipv4.ip_local_port_range = 4000    65000
net.ipv4.tcp_max_syn_backlog = 16384
net.ipv4.tcp_max_tw_buckets = 36000
net.ipv4.route.gc_timeout = 100
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_synack_retries = 1
net.core.somaxconn = 16384
net.core.netdev_max_backlog = 16384
net.ipv4.tcp_max_orphans = 16384
#以下参数是对iptables防火墙的优化,防火墙不开会提示,可以忽略不理。
net.ipv4.ip_conntrack_max = 25000000
net.ipv4.netfilter.ip_conntrack_max=25000000
net.ipv4.netfilter.ip_conntrack_tcp_timeout_established=180
net.ipv4.netfilter.ip_conntrack_tcp_timeout_time_wait=120
net.ipv4.netfilter.ip_conntrack_tcp_timeout_close_wait=60
net.ipv4.netfilter.ip_conntrack_tcp_timeout_fin_wait=120

http://zyan.cc/nginx_cache/
  #设置Web缓存区名称为cache_one,内存缓存空间大小为200MB,1天没有被访问的内容自动清除,硬盘缓存空间大小为30GB。
  proxy_cache_path  /data0/proxy_cache_dir  levels=1:2   keys_zone=cache_one:200m inactive=1d max_size=30g;
  

转载于:https://blog.51cto.com/13375232/2059147

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值