Jun13

ETag与Expires的区别

Author: leeon  Click: 188   Comments: 0 Category: 优化  Tag: etag,expires
ETag是用在向服务器GET请求的时候判断文件是否过期。
而Expires是用来判断本地缓存的组件是否过期。
Etag 和 If-Modified-Since 均是用来判断服务器端的文件是否过期。
如果页面已经缓存,当页面刷新(F5)的时候浏览器不会向服务器GET请求。
如果页面(Ctrl+F5)的时候浏览器会向服务器发起询问,If-Modified-Since提交本地记录的组件的日期给服务器,如果服务器查询组件未做修改,那么返回304(Not Modified),如果服务器端文件有做修改那么服务器会返回新的组件给客户端。
Expires 和 Cache-Control用来判断本地电脑中的缓存文件是否过期。

通常的页面访问中,如果是访问一个全新的页面那么ETag,Expires的作用如下:
第一次访问服务器会将ETag和Expires发送给浏览器。
再次访问浏览器会根据缓存来加载组件,此时如果有缓存的就不发起HTTP请求。如果本地有缓存但是依旧强制向服务器提交组件的GET请求,那么ETag就会派上用场。

Cache-ControlExpires 有更高的优先级。
If-None-Match(ETag) If-Modified-Since(Last-Modified)有更高的优先级。
Feb17

nginx的rewrite 参数和例子

Author: leeon  Click: 430   Comments: 0 Category: 优化  Tag: nginx,rewrite
nginx的rewrite 参数和例子
正则表达式匹配,其中:
* ~ 为区分大小写匹配
*~* 为不区分大小写匹配
* !~和!~*分别为区分大小写不匹配及不区分大小写不匹配
文件及目录匹配,其中: * -f和!-f用来判断是否存在文件
* -d和!-d用来判断是否存在目录
* -e和!-e用来判断是否存在文件或目录
* -x和!-x用来判断文件是否可执行
flag标记有: * last 相当于Apache里的[L]标记,表示完成rewrite
* break 终止匹配, 不再匹配后面的规则
* redirect 返回302临时重定向 地址栏会显示跳转后的地址
* permanent 返回301永久重定向 地址栏会显示跳转后的地址
一些可用的全局变量有,可以用做条件判断(待补全) $args
$content_length
$content_type
$document_root
$document_uri
$host
$http_user_agent
$http_cookie
$limit_rate
$request_body_file
$request_method
$remote_addr
$remote_port
$remote_user
$request_filename
$request_uri
$query_string
$scheme
$server_protocol
$server_addr
$server_name
$server_port
$uri
结合QeePHP的例子
[code="plain"]
if (!-d $request_filename) {
rewrite ^/([a-z-A-Z]+)/([a-z-A-Z]+)/?(.*)$ /index.php?namespace=user&controller=$1&action=$2&$3 last;
rewrite ^/([a-z-A-Z]+)/?$ /index.php?namespace=user&controller=$1 last;
break;[/code]
多目录转成参数
abc.domian.com/sort/2 => abc.domian.com/index.php?act=sort&name=abc&id=2
[code="plain"]
if ($host ~* (.*)\.domain\.com) {
set $sub_name $1;   
rewrite ^/sort\/(\d+)\/?$ /index.php?act=sort&cid=$sub_name&id=$1 last;
}[/code]
目录对换
/123456/xxxx -> /xxxx?id=123456
[code="plain"]
rewrite ^/(\d+)/(.+)/ /$2?id=$1 last;[/code]
例如下面设定nginx在用户使用ie的使用重定向到/nginx-ie目录下:
[code="plain"]
if ($http_user_agent ~ MSIE) {
rewrite ^(.*)$ /nginx-ie/$1 break;
}[/code]
目录自动加“/”
[code="plain"]
if (-d $request_filename){
rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
}[/code]
禁止htaccess
[code="plain"]
location ~/\.ht {
         deny all;
     }[/code]
禁止多个目录
[code="plain"]
location ~ ^/(cron|templates)/ {
         deny all;
break;
     }[/code]
禁止以/data开头的文件
可以禁止/data/下多级目录下.log.txt等请求;
[code="plain"]
location ~ ^/data {
         deny all;
     }[/code]
禁止单个目录
不能禁止.log.txt能请求
[code="plain"]
location /searchword/cron/ {
         deny all;
     }[/code]
禁止单个文件
[code="plain"]
location ~ /data/sql/data.sql {
         deny all;
     }[/code]
给favicon.ico和robots.txt设置过期时间;
这里为favicon.ico为99天,robots.txt为7天并不记录404错误日志
[code="plain"]
location ~(favicon.ico) {
                 log_not_found off;
expires 99d;
break;
     }      location ~(robots.txt) {
                 log_not_found off;
expires 7d;
break;
     }[/code]
设定某个文件的过期时间;这里为600秒,并不记录访问日志
[code="plain"]
location ^~ /html/scripts/loadhead_1.js {
                 access_log   off;
                 root /opt/lampp/htdocs/web;
expires 600;
break;
       }[/code]
文件反盗链并设置过期时间
这里的return 412 为自定义的http状态码,默认为403,方便找出正确的盗链的请求
“rewrite ^/ http://leech.example.com/leech.gif;”显示一张防盗链图片
“access_log off;”不记录访问日志,减轻压力
“expires 3d”所有文件3天的浏览器缓存
[code="plain"]
location ~* ^.+\.(jpg|jpeg|gif|png|swf|rar|zip|css|js)$ {
valid_referers none blocked *.example.com *.example.net localhost 208.97.167.194;
if ($invalid_referer) {
    rewrite ^/ http://leech.example.com/leech.gif;
    return 412;
    break;
}
                 access_log   off;
                 root /opt/lampp/htdocs/web;
expires 3d;
break;
     }[/code]
只充许固定ip访问网站,并加上密码
[code="plain"]
root  /opt/htdocs/www;
allow   208.97.167.194;
allow   222.33.1.2;
allow   231.152.49.4;
deny    all;
auth_basic "C1G_ADMIN";
auth_basic_user_file htpasswd;[/code]
将多级目录下的文件转成一个文件,增强seo效果
/job-123-456-789.html 指向/job/123/456/789.html
[code="plain"]
rewrite ^/job-([0-9]+)-([0-9]+)-([0-9]+)\.html$ /job/$1/$2/jobshow_$3.html last;[/code]
将根目录下某个文件夹指向2级目录
如/shanghaijob/ 指向 /area/shanghai/
如果你将last改成permanent,那么浏览器地址栏显是/location/shanghai/
[code="plain"]
rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2 last;[/code]
上面例子有个问题是访问/shanghai 时将不会匹配
[code="plain"]
rewrite ^/([0-9a-z]+)job$ /area/$1/ last;
rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2 last;[/code]
这样/shanghai 也可以访问了,但页面中的相对链接无法使用,
如./list_1.html真实地址是/area/shanghia/list_1.html会变成/list_1.html,导至无法访问。
那我加上自动跳转也是不行咯
(-d $request_filename)它有个条件是必需为真实目录,而我的rewrite不是的,所以没有效果
[code="plain"]
if (-d $request_filename){
rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
}[/code]
知道原因后就好办了,让我手动跳转吧
[code="plain"]
rewrite ^/([0-9a-z]+)job$ /$1job/ permanent;
rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2 last;[/code]
文件和目录不存在的时候重定向:
[code="plain"]
if (!-e $request_filename) {
proxy_pass http://127.0.0.1;
}[/code]
域名跳转
[code="plain"]
server
     {
             listen       80;
             server_name  jump.example.com;
             index index.html index.htm index.php;
             root  /opt/lampp/htdocs/www;
             rewrite ^/ http://www.example.com/;
             access_log  off;
     }[/code]
多域名转向
[code="plain"]
server_name  www.example.com www.example.net;
             index index.html index.htm index.php;
             root  /opt/lampp/htdocs;
if ($host ~ "example\.net") {
rewrite ^(.*) http://www.example.com$1 permanent;
}[/code]
三级域名跳转
[code="plain"]
if ($http_host ~* "^(.*)\.i\.example\.com$") {
rewrite ^(.*) http://top.yingjiesheng.com$1;
break;
}[/code]
域名镜向
[code="plain"]
server
     {
             listen       80;
             server_name  mirror.example.com;
             index index.html index.htm index.php;
             root  /opt/lampp/htdocs/www;
             rewrite ^/(.*) http://www.example.com/$1 last;
             access_log  off;
     }[/code]
某个子目录作镜向
[code="plain"]
location ^~ /zhaopinhui {
  rewrite ^.+ http://zph.example.com/ last;
  break;
     }
[/code]
给discuz某版块单独配置域名
[code="plain"]
server_name  bbs.example.com news.example.com;      location = / {
        if ($http_host ~ news\.example.com$) {
  rewrite ^.+ http://news.example.com/forum-831-1.html last;
  break;
}
     }[/code]
Feb3

ApacheBench测试结果解析

Author: leeon  Click: 502   Comments: 0 Category: 优化  Tag: apache,apachebench

ab -n 10 -c 10 http://www.google.com/ 
这个命令的意思是启动ab ,向 www.google.com 发送10个请求(-n 10) ,并每次模拟10个并发用户请求(-c 10)——也就是说一次都发过去了。
-n是总共发送的请求数
-c是每次并发用户数

Benchmarking www.google.com (be patient).....done

 

 

Server Software:        GWS/2.1

Server Hostname:        www.google.com

Server Port:            80

 

Document Path:          /

Document Length:        230 bytes

 

Concurrency Level:      10

/*整个测试持续的时间*/

Time taken for tests:   3.234651 seconds

/*完成的请求数量*/

Complete requests:      10

/*失败的请求数量*/

Failed requests:        0

Write errors:           0

Non-2xx responses:      10

Keep-Alive requests:    10

/*整个场景中的网络传输量*/

Total transferred:      6020 bytes

/*整个场景中的HTML内容传输量*/

HTML transferred:       2300 bytes

/*相当于 LR 中的 每秒事务数 ,mean 表示这是一个平均值*/

Requests per second:    3.09 [#/sec] (mean)

/*相当于 LR 中的 平均事务响应时间,一次并发10个请求花费的时间 */

Time per request:       3234.651 [ms] (mean)

/*一次并发10个用户请求时的每个请求的平均响应时间*/

Time per request:       323.465 [ms] (mean, across all concurrent requests)

/*平均每秒网络上的流量,可以帮助排除是否存在网络流量过大导致响应时间延长的问题*/

Transfer rate:          1.55 [Kbytes/sec] received

/*网络上消耗的时间的分解,*/

Connection Times (ms)

              min  mean[+/-sd] median   max

Connect:       20  318 926.1     30    2954  //连接开销的时间

Processing:    40 2160 1462.0   3034    3154 //服务器端处理的时间与等待的时间相等

Waiting:       40 2160 1462.0   3034    3154 //等待的时间

Total:         60 2479 1276.4   3064    3184 //总共花费的时间

 

/*下面的内容为整个场景中所有请求的响应情况。在场景中每个请求都有一个响应时间,其中 50% 的用户响应时间小于 3064 毫秒,60 % 的用户响应时间小于 3094 毫秒,最大的响应时间小于 3184 毫秒*/


[code="bash"]
Percentage of the requests served within a certain time (ms)

50% 3064

66% 3094

75% 3124

80% 3154

90% 3184

95% 3184

98% 3184

99% 3184

100% 3184 (longest request)
[/code]


Jan5

lighttpd(lighty) 性能优化

Author: fcicq  Click: 289   Comments: 0 Category: 优化  Tag: lighttpd,lighty

1 最大连接数

默认是1024
修改 server.max-fds,大流量网站推荐2048.

因为lighttpd基于线程,而apache(MPM-prefork)基于子进程,
所以apache需要设置startservers,maxclients等,这里不需要

2 stat() 缓存

stat() 这样的系统调用,开销也是相当明显的.
缓存能够节约时间和环境切换次数(context switches)

一句话,lighttpd.conf加上
server.stat-cache-engine = “fam”

lighttpd还另外提供simple(缓存1秒内的stat()),disabled选项.
相信没人会选disabled吧.

3 常连接(HTTP Keep-Alive)

一般来说,一个系统能够打开的文件个数是有限制的(文件描述符限制)
常连接占用文件描述符,对非并发的访问没有什么意义.

(文件描述符的数量和许多原因有关,比如日志文件数量,并发数目等)

这是lighttpd在keep-alive方面的默认值.
server.max-keep-alive-requests = 128
server.max-keep-alive-idle = 30

换言之,lighttpd最多可以同时承受30秒长的常连接,每个连接最多请求128个文件.
但这个默认值确实不适合非并发这种多数情况.

lighttpd.conf 中减小
server.max-keep-alive-requests
server.max-keep-alive-idle
两个值,可以减缓这种现象.

甚至可以关闭lighttpd keep-alive.
server.max-keep-alive-requests = 0

4 事件处理

对于linux kernel 2.6来说,没有别的可说
lighttpd.conf中加上这一句足矣
server.event-handler = “linux-sysepoll”

另外,
linux 2.4 使用 linux-rtsig
solaris: solaris-devpoll
freebsd 使用 freebsd-kqueue
unix 使用 poll

5 网络处理

lighttpd 大量使用了 sendfile() 这样一个高效的系统调用.
减少了从应用程序到网卡间的距离.
(同时也减少了lighttpd对cpu的占用,这部分占用转嫁到内核身上了)

根据平台,可以设置不同的参数.
(linux) server.network-backend = “linux-sendfile”
solaris: solaris-sendfilev
freebsd: freebsd-sendfile
unix: writev

分类

标签

归档

最新评论

vls在23:17:41评论了
JavaScript之trim方法三则
vls在19:27:29评论了
【原创】获取jQuery中Ajax函数的返回值的方法
zxfredhat在12:28:58评论了
编译sasl2出现digestmd5.c报错信息的解决方法
zxfredhat在12:26:35评论了
编译sasl2出现digestmd5.c报错信息的解决方法
MR。扯在17:11:22评论了
修改windows7中的OEM信息

我看过的书

链接

其他

访问本站种子 本站平均热度:288 c° 本站链接数:41 个 本站标签数:154 个 本站被评论次数:20 次