## 安装

mkdir build/
tar xf nginx-x.y.z.tar.gz -C build/
cd build/nginx-x.y.z
./configure --prefix=/opt/nginx
make && make install


## 查看版本信息、启动、停止、检查配置文件、重加载配置、分割日志文件

/opt/nginx/sbin/nginx -V


/opt/nginx/sbin/nginx > /dev/null 2>&1 &


/opt/nginx/sbin/nginx -s stop


/opt/nginx/sbin/nginx -t


/opt/nginx/sbin/nginx -s reload


mv /opt/nginx/logs/access.log /opt/nginx/logs/access_20160430.log && mv /opt/nginx/logs/error.log /opt/nginx/logs/error_20160430.log
/opt/nginx/sbin/nginx -s reopen


## 配置

nginx consists of modules which are controlled by directives specified in the configuration file. Directives are divided into simple directives and block directives. A simple directive consists of the name and parameters separated by spaces and ends with a semicolon (;). A block directive has the same structure as a simple directive, but instead of the semicolon it ends with a set of additional instructions surrounded by braces ({ and }). If a block directive can have other directives inside braces, it is called a context (examples: events, http, server, and location). Directives placed in the configuration file outside of any contexts are considered to be in the main context. The events and http directives reside in the main context, server in http, and location in server.

### 常用指令

#### location

location的优先级顺序比较复杂，见官方文档的三段话

A location can either be defined by a prefix string, or by a regular expression. Regular expressions are specified with the preceding “~*” modifier (for case-insensitive matching), or the “~” modifier (for case-sensitive matching). To find location matching a given request, nginx first checks locations defined using the prefix strings (prefix locations). Among them, the location with the longest matching prefix is selected and remembered. Then regular expressions are checked, in the order of their appearance in the configuration file. The search of regular expressions terminates on the first match, and the corresponding configuration is used. If no match with a regular expression is found then the configuration of the prefix location remembered earlier is used.

If the longest matching prefix location has the “^~” modifier then regular expressions are not checked.

Also, using the “=” modifier it is possible to define an exact match of URI and location. If an exact match is found, the search terminates.

if判断，详见这里

#### rewrite

last相当于重写URL后，该URL重新开始location匹配搜索

break相当于中断在当前location里的rewrite处理

redirect是302临时重定向

permanent是301永久重定向

ssl的证书，详见这里

ssl的私钥文件，详见这里

## 示例

nginx官方有一个完整的示例

http {
server {
listen          80;
server_name     www.domain1.com;
access_log      logs/domain1.access.log main;
location / {
index index.html;
root  /var/www/domain1.com/htdocs;
}
}
server {
listen          80;
server_name     www.domain2.com;
access_log      logs/domain2.access.log main;
location / {
index index.html;
root  /var/www/domain2.com/htdocs;
}
}
}


http {
upstream myproject {
server 127.0.0.1:8000 weight=3;: server 127.0.0.1:8001;
server 127.0.0.1:8002;
server 127.0.0.1:8003;
}

server {
listen 80;
server_name www.domain.com;
location / {
proxy_pass http://myproject;
}
}
}


## 更多

nginx反向代理WebSockets的示例

nginx反向代理WebSockets的示例

nginx利用image_filter动态生成缩略图的示例

nginx使用tcp代理实现HA的示例