个人使用nginx的一些经验分享

根据自定义http header控制nginx选择反向代理服务器

说下核心:客户端自定义的http header,在nginx的配置文件里能直接读取到。 条件:header必须用减号“-”分隔单词,nginx里面会转换为对应的下划线“_”连接的小写单词。 nginx配置:

location / {
  if ($http_my_custom_header ~ (\d+) ) {
    rewrite / http://google.com/ last;
  }
}

测试:

wget --header="my-custom-header:1" -d http://bianbian.org

DEBUG output created by Wget 1.13.4 on linux-gnu.

URI encoding = `UTF-8'
--2014-04-28 09:48:40--  http://bianbian.org/
Resolving bianbian.org (bianbian.org)... 172.31.30.70
Caching bianbian.org => 172.31.30.70
Connecting to bianbian.org (bianbian.org)|172.31.30.70|:80... connected.
Created socket 3.
Releasing 0x084e3e18 (new refcount 1).

---request begin---
GET / HTTP/1.1
User-Agent: Wget/1.13.4 (linux-gnu)
Accept: */*
Host: bianbian.org
Connection: Keep-Alive
my-custom-header: 1

---request end---
HTTP request sent, awaiting response...
---response begin---
HTTP/1.1 302 Moved Temporarily
Server: nginx/1.1.19
Date: Mon, 28 Apr 2014 09:48:40 GMT
Content-Type: text/html
Content-Length: 161
Connection: keep-alive
Location: http://google.com

nginx的worker_processes设为多少才合适?

搜索到原作者的话: 一般一个进程足够了,你可以把连接数设得很大。如果有SSL、gzip这些比较消耗CPU的工作,而且是多核CPU的话,可以设为和CPU的数量一样。或者要处理很多很多的小文件,而且文件总大小比内存大很多的时候,也可以把进程数增加,以充分利用IO带宽(主要似乎是IO操作有block)。

As a general rule you need the only worker with large number of
worker_connections, say 10,000 or 20,000.

However, if nginx does CPU-intensive work as SSL or gzipping and
you have 2 or more CPU, then you may set worker_processes to be equal
to CPU number.

Besides, if you serve many static files and the total size of the files
is bigger than memory, then you may increase worker_processes to
utilize a full disk bandwidth.

Igor Sysoev

©2019 bbdd.tld 粤ICP备19153268号 粤公网安备44030602004149号