nginx 配置之 proxy_pass 神器

服务器zkbhj 发表了文章 • 0 个评论 • 1599 次浏览 • 2017-03-03 10:00 • 来自相关话题

proxy 模块是 nginx 中最碉堡的模块之一。就是有了 proxy 模块,nginx 才能和其它 http 服务器关联起来,极大程度地提高了 nginx 的可用性。proxy 这个模块比较大,光是指令就有一大堆。这里我们只是简单地介绍一下它最核心的指令 —— proxy_pass 。

当我们遇到跨域问题,而且客户端无法支持 CORS 时,最好的办法就是让服务器来做代理。在前端页面所在的服务器 nginx 配置上开一个路由,然后使用 proxy 去请求另一个域名下的资源。如果跨域资源也部署在同一台机器上,我们甚至可以 proxy 到 127.0.0.1,比如:location /api { proxy_pass http://127.0.0.1:1234; }
当客户端请求 /api 这个路径下的资源时服务器就会帮助我们去 127.0.0.1 的 1234 端口上取资源,解决了跨域的问题。proxy_pass 会将当前的 $uri 带过去,所以如果 /api 这个路由是我们擅自加的,在发送到目标服务前可以使用 rewrite 来处理掉这个多余的路由,比如:location /api/ { rewrite ^/api/(.*) /$1 break; proxy_pass http://127.0.0.1:1234; }
rewrite 的作用是修改 $uri,但要注意 rewrite 要有个重新匹配 location 的副作用。由于 proxy_pass 的处理阶段比 location 处理更晚,所以这里需要 break 掉,以防止 rewrite 进入下一次 location 匹配而丢失 proxy_pass。

另外还有一个值得注意的地方,proxy_pass 后面的 host 如果填写一个域名的话,这个域名将会在 nginx 启动时解析。如果 nginx 启动时域名无法解析将会抛出异常无法启动,比如: location /api { proxy_pass http://xxx; } nginx: [emerg] host not found in upstream "xxx"
而且由于 nginx 解析域名是在启动时做的,所以在 nginx 启动之后修改域名的解析对 nginx 是不会生效的。

如果觉得让 nginx 启动时去查询 DNS 这件事不靠谱(我就不推荐这么做,因为 DNS 确实是不可控的),那么可以在 proxy_pass 时到某个 IP 上,hostname 可以通过 porxy_set_header 指令强制设置 proxy 的 HTTP 请求中的 Host 字段来修改它,比如:location /api { proxy_set_header Host api.web-tinker.com; proxy_pass http://127.0.0.1:8080; } 
除了设置 Host 这个请求头之外,proxy_set_header 还能设置别的头,只要你的脑洞够大就可以用它来做更多奇怪的事情!

另外还有个要注意的点。proxy_pass 默认使用的是 http 1.0,可以通过 proxy_http_version 指令让它使用 http 1.1,以便开启 keepalive 之类的功能。location /api { proxy_http_version 1.1; proxy_pass http://127.0.0.1:8080; } 查看全部
proxy 模块是 nginx 中最碉堡的模块之一。就是有了 proxy 模块,nginx 才能和其它 http 服务器关联起来,极大程度地提高了 nginx 的可用性。proxy 这个模块比较大,光是指令就有一大堆。这里我们只是简单地介绍一下它最核心的指令 —— proxy_pass 。

当我们遇到跨域问题,而且客户端无法支持 CORS 时,最好的办法就是让服务器来做代理。在前端页面所在的服务器 nginx 配置上开一个路由,然后使用 proxy 去请求另一个域名下的资源。如果跨域资源也部署在同一台机器上,我们甚至可以 proxy 到 127.0.0.1,比如:
location /api { proxy_pass http://127.0.0.1:1234; } 

当客户端请求 /api 这个路径下的资源时服务器就会帮助我们去 127.0.0.1 的 1234 端口上取资源,解决了跨域的问题。proxy_pass 会将当前的 $uri 带过去,所以如果 /api 这个路由是我们擅自加的,在发送到目标服务前可以使用 rewrite 来处理掉这个多余的路由,比如:
location /api/ { rewrite ^/api/(.*) /$1 break; proxy_pass http://127.0.0.1:1234; } 

rewrite 的作用是修改 $uri,但要注意 rewrite 要有个重新匹配 location 的副作用。由于 proxy_pass 的处理阶段比 location 处理更晚,所以这里需要 break 掉,以防止 rewrite 进入下一次 location 匹配而丢失 proxy_pass

另外还有一个值得注意的地方,proxy_pass 后面的 host 如果填写一个域名的话,这个域名将会在 nginx 启动时解析。如果 nginx 启动时域名无法解析将会抛出异常无法启动,比如:
 location /api { proxy_pass http://xxx; } nginx: [emerg] host not found in upstream "xxx" 

而且由于 nginx 解析域名是在启动时做的,所以在 nginx 启动之后修改域名的解析对 nginx 是不会生效的

如果觉得让 nginx 启动时去查询 DNS 这件事不靠谱(我就不推荐这么做,因为 DNS 确实是不可控的),那么可以在 proxy_pass 时到某个 IP 上,hostname 可以通过 porxy_set_header 指令强制设置 proxy 的 HTTP 请求中的 Host 字段来修改它,比如:
location /api { proxy_set_header Host api.web-tinker.com; proxy_pass http://127.0.0.1:8080; }
 
除了设置 Host 这个请求头之外,proxy_set_header 还能设置别的头,只要你的脑洞够大就可以用它来做更多奇怪的事情!

另外还有个要注意的点。proxy_pass 默认使用的是 http 1.0,可以通过 proxy_http_version 指令让它使用 http 1.1,以便开启 keepalive 之类的功能。
location /api { proxy_http_version 1.1; proxy_pass http://127.0.0.1:8080; }

什么是“惊群现象”?

回复

服务器zkbhj 回复了问题 • 1 人关注 • 1 个回复 • 2165 次浏览 • 2017-03-02 17:00 • 来自相关话题

yum方式安装PHP比较齐全的安装方式

PHPzkbhj 发表了文章 • 0 个评论 • 1168 次浏览 • 2017-03-02 14:15 • 来自相关话题

yum install php70w.x86_64 php70w-bcmath.x86_64 php70w-cli.x86_64 php70w-common.x86_64 php70w-dba.x86_64 php70w-devel.x86_64 php70w-embedded.x86_64 php70w-enchant.x86_64 php70w-fpm.x86_64 php70w-gd.x86_64 php70w-imap.x86_64 php70w-ldap.x86_64 php70w-mbstring.x86_64 php70w-mcrypt.x86_64 php70w-mysqlnd.x86_64 php70w-odbc.x86_64 php70w-opcache.x86_64 php70w-pdo.x86_64 php70w-pdo_dblib.x86_64 php70w-pear.noarch php70w-pecl-apcu.x86_64 php70w-pecl-apcu-devel.x86_64 php70w-pecl-imagick.x86_64 php70w-pecl-imagick-devel.x86_64 php70w-pecl-mongodb.x86_64 php70w-pecl-redis.x86_64 php70w-pecl-xdebug.x86_64 php70w-pgsql.x86_64 php70w-xml.x86_64 php70w-xmlrpc.x86_64 nginx php70w-intl -y

php70w-mysqlnd.x86_64 查看全部
yum install php70w.x86_64 php70w-bcmath.x86_64 php70w-cli.x86_64 php70w-common.x86_64 php70w-dba.x86_64 php70w-devel.x86_64 php70w-embedded.x86_64 php70w-enchant.x86_64 php70w-fpm.x86_64 php70w-gd.x86_64 php70w-imap.x86_64 php70w-ldap.x86_64 php70w-mbstring.x86_64 php70w-mcrypt.x86_64 php70w-mysqlnd.x86_64 php70w-odbc.x86_64 php70w-opcache.x86_64 php70w-pdo.x86_64 php70w-pdo_dblib.x86_64 php70w-pear.noarch php70w-pecl-apcu.x86_64 php70w-pecl-apcu-devel.x86_64 php70w-pecl-imagick.x86_64 php70w-pecl-imagick-devel.x86_64 php70w-pecl-mongodb.x86_64 php70w-pecl-redis.x86_64 php70w-pecl-xdebug.x86_64 php70w-pgsql.x86_64 php70w-xml.x86_64 php70w-xmlrpc.x86_64 nginx php70w-intl -y

php70w-mysqlnd.x86_64

linux开启nscd服务缓存加速

服务器zkbhj 发表了文章 • 0 个评论 • 1278 次浏览 • 2017-03-01 17:51 • 来自相关话题

    在我使用的阿里云主机上有观察到开启了一个服务nscd ,后来谷哥了下该服务的作用。了解到nscd会缓存三种服务passwd group hosts,所以它会记录三个库,分别对应源/etc/passwd, /etc/hosts 和 /etc/resolv.conf每个库保存两份缓存,一份是找到记录的,一份是没有找到记录的。每一种缓存都保存有生存时间(TTL)。其作用就是在本当中增加cache ,加快如DNS的解析等的速度。
 
一、nscd的配置
通过编辑/etc/nscd.conf文件,在其中增加如下一行可以开启本地DNS cache:
enable-cache hosts yes阿里云主机上的配置如下:
[root@361way ~]# cat /etc/nscd.conf
#logfile /var/log/nscd.log
threads 6
max-threads 128
server-user nscd
debug-level 5
paranoia no
enable-cache passwd no
enable-cache group no
enable-cache hosts yes
positive-time-to-live hosts 5
negative-time-to-live hosts 20
suggested-size hosts 211
check-files hosts yes
persistent hosts yes
shared hosts yes
max-db-size hosts 33554432相关参数的解释如下:

logfile debug-file-name

指定调试信息写入的文件名。

debug-level value

设置希望的调试级别。

threads number

这是启动的等待请求的线程数。最少将创建5个线程。

server-user user

如果设置了该选项,nscd将作为该用户运行,而不是作为root。如果每个用户都使用一个单独的缓存(-S参数),将忽略该选项。

enable-cache service <yes|no>

启用或禁用制定的 服务 缓存。

positive-time-to-live service value

设置 service 在指定缓存中正的项目(成功的请求)的TTL(存活时间)。 Value 以秒为单位。较大的值将增加缓存命中率从而减低平均响应时间,但是将增加缓存的一致性问题。

negative-time-to-live service value

设置 service 在指定缓存中负的项目(失败的请求)的TTL(存活时间)。 Value 以秒为单位。如果存在由不在系统数据库中的uid(用户ID)(例如在以root身份解包linux 内核源代码时)所拥有的文件将明显改善性能;应该维持较小的值以降低缓存一致性问题。

suggested-size service value

这是内部散列表的大小, value 应该保持一个素数以达到优化效果。

check-files service <yes|no>

启用或禁用检查属于指定 服务 的文件的改变。这些文件是 /etc/passwd, /etc/group, 以及/etc/hosts。 
二、nscd 服务查看和清除
 
默认该服务在redhat或centos下是关闭的,可以通过services nscd start开启。缓存DB文件在/var/db/nscd下。可以通过nscd -g查看统计的信息,这里列出部分:
 
[root@361way ~]# nscd -g
nscd configuration:
5 server debug level
34d 23h 14m 18s server runtime
6 current number of threads
128 maximum number of threads
0 number of times clients had to wait
no paranoia mode enabled
3600 restart internal
5 reload count
passwd cache:
no cache is enabled
no cache is persistent
no cache is shared
0 suggested size
0 total data pool size
0 used data pool size
3600 seconds time to live for positive entries
20 seconds time to live for negative entries
0 cache hits on positive entries
0 cache hits on negative entries
0 cache misses on positive entries
0 cache misses on negative entries
0% cache hit rate
0 current number of cached values
0 maximum number of cached values
0 maximum chain length searched
0 number of delays on rdlock
0 number of delays on wrlock
0 memory allocations failed
yes check /etc/passwd for changes清除缓存
nscd -i passwd
nscd -i group
nscd -i hosts除了上面的方法,重启nscd服务同样可以达到清理cache的目的。
 
三、nscd的效果
 
首先要看网络和dns服务器的能力,dns解析越慢,dns缓存的优势就越大.比如我们在北京用的dns服务器202.106.0.20和google的dns服务器8.8.8.8速度会差不少.

如果dns服务器比较稳定,那它对效率的影响就是一个常数.这个常数有多大呢?

我简单试了一下.在局域网内进行压力测试,压一个nginx下的静态页面,使用202.106.0.20这个dns服务器,不用dns缓存.平均一分钟可以访问27万次.压一个简单的php页面,平均一分钟可以访问22万次.加上nscd服务后,静态页面平均一分钟可以访问120万次,要快4倍多.php页面平均一分钟可以访问50万次,快一倍多.

如果是做搜索引擎或是一些代理服务类的项目,比如短信通道,数据推送服务,这个性能提升还是比较可观的.但在一般的项目中,一台服务器每分钟发22万次请求的情况是很少见的,所以这个性能提升也微呼其微.

但在追求极限的道路上,每一小步都至关重要噢~
 
原文链接:http://www.361way.com/linux-ns ... .html
  查看全部
    在我使用的阿里云主机上有观察到开启了一个服务nscd ,后来谷哥了下该服务的作用。了解到nscd会缓存三种服务passwd group hosts,所以它会记录三个库,分别对应源/etc/passwd, /etc/hosts 和 /etc/resolv.conf每个库保存两份缓存,一份是找到记录的,一份是没有找到记录的。每一种缓存都保存有生存时间(TTL)。其作用就是在本当中增加cache ,加快如DNS的解析等的速度。
 
一、nscd的配置
通过编辑/etc/nscd.conf文件,在其中增加如下一行可以开启本地DNS cache:
enable-cache hosts yes
阿里云主机上的配置如下:
[root@361way ~]# cat /etc/nscd.conf
#logfile /var/log/nscd.log
threads 6
max-threads 128
server-user nscd
debug-level 5
paranoia no
enable-cache passwd no
enable-cache group no
enable-cache hosts yes
positive-time-to-live hosts 5
negative-time-to-live hosts 20
suggested-size hosts 211
check-files hosts yes
persistent hosts yes
shared hosts yes
max-db-size hosts 33554432
相关参数的解释如下:

logfile debug-file-name

指定调试信息写入的文件名。

debug-level value

设置希望的调试级别。

threads number

这是启动的等待请求的线程数。最少将创建5个线程。

server-user user

如果设置了该选项,nscd将作为该用户运行,而不是作为root。如果每个用户都使用一个单独的缓存(-S参数),将忽略该选项。

enable-cache service <yes|no>

启用或禁用制定的 服务 缓存。

positive-time-to-live service value

设置 service 在指定缓存中正的项目(成功的请求)的TTL(存活时间)。 Value 以秒为单位。较大的值将增加缓存命中率从而减低平均响应时间,但是将增加缓存的一致性问题。

negative-time-to-live service value

设置 service 在指定缓存中负的项目(失败的请求)的TTL(存活时间)。 Value 以秒为单位。如果存在由不在系统数据库中的uid(用户ID)(例如在以root身份解包linux 内核源代码时)所拥有的文件将明显改善性能;应该维持较小的值以降低缓存一致性问题。

suggested-size service value

这是内部散列表的大小, value 应该保持一个素数以达到优化效果。

check-files service <yes|no>

启用或禁用检查属于指定 服务 的文件的改变。这些文件是 /etc/passwd, /etc/group, 以及/etc/hosts。 
二、nscd 服务查看和清除
 
默认该服务在redhat或centos下是关闭的,可以通过services nscd start开启。缓存DB文件在/var/db/nscd下。可以通过nscd -g查看统计的信息,这里列出部分:
 
[root@361way ~]# nscd -g
nscd configuration:
5 server debug level
34d 23h 14m 18s server runtime
6 current number of threads
128 maximum number of threads
0 number of times clients had to wait
no paranoia mode enabled
3600 restart internal
5 reload count
passwd cache:
no cache is enabled
no cache is persistent
no cache is shared
0 suggested size
0 total data pool size
0 used data pool size
3600 seconds time to live for positive entries
20 seconds time to live for negative entries
0 cache hits on positive entries
0 cache hits on negative entries
0 cache misses on positive entries
0 cache misses on negative entries
0% cache hit rate
0 current number of cached values
0 maximum number of cached values
0 maximum chain length searched
0 number of delays on rdlock
0 number of delays on wrlock
0 memory allocations failed
yes check /etc/passwd for changes
清除缓存
nscd -i passwd
nscd -i group
nscd -i hosts
除了上面的方法,重启nscd服务同样可以达到清理cache的目的。
 
三、nscd的效果
 
首先要看网络和dns服务器的能力,dns解析越慢,dns缓存的优势就越大.比如我们在北京用的dns服务器202.106.0.20和google的dns服务器8.8.8.8速度会差不少.

如果dns服务器比较稳定,那它对效率的影响就是一个常数.这个常数有多大呢?

我简单试了一下.在局域网内进行压力测试,压一个nginx下的静态页面,使用202.106.0.20这个dns服务器,不用dns缓存.平均一分钟可以访问27万次.压一个简单的php页面,平均一分钟可以访问22万次.加上nscd服务后,静态页面平均一分钟可以访问120万次,要快4倍多.php页面平均一分钟可以访问50万次,快一倍多.

如果是做搜索引擎或是一些代理服务类的项目,比如短信通道,数据推送服务,这个性能提升还是比较可观的.但在一般的项目中,一台服务器每分钟发22万次请求的情况是很少见的,所以这个性能提升也微呼其微.

但在追求极限的道路上,每一小步都至关重要噢~
 
原文链接:http://www.361way.com/linux-ns ... .html
 

PHP如何实现几天前、几小时前、几秒前的功能?

回复

PHPzkbhj 回复了问题 • 1 人关注 • 1 个回复 • 4418 次浏览 • 2017-03-01 17:37 • 来自相关话题

CSS怎么让整个页面变暗?

回复

前端开发zkbhj 回复了问题 • 1 人关注 • 1 个回复 • 4708 次浏览 • 2017-03-01 14:48 • 来自相关话题

user agent stylesheet 是什么鬼?

回复

前端开发zkbhj 回复了问题 • 1 人关注 • 1 个回复 • 4751 次浏览 • 2017-03-01 14:45 • 来自相关话题

PHP中把stdClass Object转array的几个方法

PHPzkbhj 发表了文章 • 0 个评论 • 1359 次浏览 • 2017-03-01 14:36 • 来自相关话题

PHP和JS通讯通常都用json,但用 json 传过来的数组并不是标准的array,而是 stdClass 类型。那么我们可以参考下面的几个方法进行转换。
 
方法一:
//PHP stdClass Object转array
function object_array($array) {
if(is_object($array)) {
$array = (array)$array;
} if(is_array($array)) {
foreach($array as $key=>$value) {
$array[$key] = object_array($value);
}
}
return $array;
}方法二:
$array = json_decode(json_encode(simplexml_load_string($xmlString)),TRUE);方法三:
function object2array_pre(&$object) {
if (is_object($object)) {
$arr = (array)($object);
} else {
$arr = &$object;
}
if (is_array($arr)) {
foreach($arr as $varName => $varValue){
$arr[$varName] = $this->object2array($varValue);
}
}
return $arr;
}```


如果是10W的数据量的话,执行要进1s,结构再复杂些,可以达到3s, 性能太差了
可以用以下替换:

function object2array(&$object) {

$object = json_decode( json_encode( $object),true);
return $object;
}


但是对json的特性,只能是针对utf8的,否则得先转码下。 查看全部
PHP和JS通讯通常都用json,但用 json 传过来的数组并不是标准的array,而是 stdClass 类型。那么我们可以参考下面的几个方法进行转换。
 
方法一:
//PHP stdClass Object转array  
function object_array($array) {
if(is_object($array)) {
$array = (array)$array;
} if(is_array($array)) {
foreach($array as $key=>$value) {
$array[$key] = object_array($value);
}
}
return $array;
}
方法二:
$array = json_decode(json_encode(simplexml_load_string($xmlString)),TRUE);
方法三:
 function object2array_pre(&$object) {
if (is_object($object)) {
$arr = (array)($object);
} else {
$arr = &$object;
}
if (is_array($arr)) {
foreach($arr as $varName => $varValue){
$arr[$varName] = $this->object2array($varValue);
}
}
return $arr;
}```


如果是10W的数据量的话,执行要进1s,结构再复杂些,可以达到3s, 性能太差了
可以用以下替换:

function object2array(&$object) {

$object = json_decode( json_encode( $object),true);
return $object;
}


但是对json的特性,只能是针对utf8的,否则得先转码下。

什么是快速排序?

回复

专业名词zkbhj 回复了问题 • 1 人关注 • 1 个回复 • 2772 次浏览 • 2017-03-01 14:15 • 来自相关话题

什么是冒泡排序?

回复

专业名词zkbhj 回复了问题 • 1 人关注 • 1 个回复 • 2492 次浏览 • 2017-03-01 11:51 • 来自相关话题