安装方法:
rpm 及源码安装:
- # ./configure --prefix=/usr/local/nginx --conf-path=/etc/nginx/nginx.conf --user=nginx --group=nginx --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module --with-http_mp4_module --http-client-body-temp-path=/var/tmp/nginx/client --http-proxy-temp-path=/var/tmp/nginx/proxy --http-fastcgi-temp-path=/var/tmp/nginx/fastcgi
- # make && make install
- Tmalloc, gperftools
配置段:
nginx:
main 配置段
http {
}
http 配置: http core 配置一个静态 web 服务器
ngx_http_core_module
配置框架:
- http {
- upstream {
- .,..
- }
- server {
- listen IP:PORT;
- # 虚拟主机
- location /URL {
- if ...{
- ...
- } #类似于 httpd 中的 < Location>, 用于定义 URL 与本地文件系统的映射关系;
- root "/path/to/somewhere";
- ...
- }
- } #每个 server 类似于 httpd 中的一个 < VirtualHost>
- server {
- ,,.
- }
- }
注意: 与 http 配置相关的指令必须放在 http,server,location,upstream,if 块中;
虚拟主机相关的配置:
1,server {}
定义一个虚拟主机;
- server {
- listen 8080;
- server_name www.zhanx.wang;
- root "/vhosts/web1";
- }
- 2,listen
监听的端口
完整格式 :listen address[:port] [default_server] [ssl] [spdy] [proxy_protocol] [setfib=number] [fastopen=number] [backlog=number] [rcvbuf=size] [sndbuf=size] [accept_filter=filter] [deferred] [bind] [ipv6only=on|off] [so_keepalive=on|off|[keepidle]:[keepintvl]:[keepcnt]];
listen address[:port] [default_server] ssl
backlog=number: 指明 TCP 协议 backlog 队列的大小. 默认为 - 1, 表示不设置;
rcvbuf=size: 设定监听句柄的 SO_RCVBUF 参数;
例如:
- listen 172.16.100.8:8080
- 3,server_name name [...];
后可跟多个主机名; 名称还可以使用通配符和正则表达式(~);
(1) 先做精确匹配; www.zhanxwang:
(2) 左侧通配符匹配, 例如:*.zhanx.wang;
(3) 右侧通配符匹配, 例如: www.*;
(4) 正则表达式匹配, 例如: ~^.*\.zhanx\.wang$
- (5) default_server
- 4,location [=|~|~*|^~] /uri {...}
- location @name
功能: 允许根据用户请求的 URI 来匹配定义的各 location, 匹配到时, 此请求将被相应的 location 块中的配置所处理;
=: 精确匹配检查;
~: 正则表达式模式匹配, 区分字符大小写;
~*: 正则表达式模式 匹配, 不区分字符大小写;
^~:URI 的前半部分匹配, 不检查正则表达式;
匹配优先级: 精确匹配(=),^~,~ 和~*, 由不带符号的 URL 进行左侧匹配;
- server {
- listen 80
- server_name www.zhanx.wang
- location / {
- root "/vhosts/web1";
- }
- location /images/ {
- root "vhosts/images";
- }
- location ~*\.PHP${
- fcgipass
- }
- }
- 5,root
设置 Web 资源路径映射; 用于指明请求的 URL 所对应的文档的根目录路径;
- location /images/ {
- root "/web/imgs/";
- }
- 6,alias path
用于 location 配置段, 定义路径别名
- location /images/ {
- alias /www/pictures/;
- }
- http://www.zhanx.wang/image/a.jpg <--- /www/pictures/a.jpg
注意: root 表示指明路径为对应 location 的 "/" URL;
alias 表示路径映射, 即 location 中的 URL 是相对于 alias 所指明的路径而言;
7,index file
默认主页面
- index index.PHP index.html;
- 8,error_page code [...] [=code] URI | @name
根据 http 状态码重定向错误页面
- error_page 404 /404.HTML
- =
: 以指定的响应码进行响应; 省略 code 表示以新资源的响应码为响应码;
- 9,try_files
- try_files path1[,path2,...] URI
10, 基于 IP 地址的访问控制;
- allow IP/Network;
- deny IP/Network;
11, 基于用户的访问控制;
- basic,digest
- auth_basic "";
- auth_basic_user_file "/PATH/TO/PASSWORD_FILE"
账号密码文件建议使用 htpasswd 来创建;
12,https 服务
生成私钥
13,stub_status {on|off};
仅能用于 location 上下文;
- location /status {
- stub_status on;
- allow 172.16.0.0/16;
- deny all;
- }
结果示例:
- Active connection: 6 #当前所有处于打开状态的连接数;
- server accepts handled requests
- 241 241 431
(1)已经接受的连接
(2)已经处理过的连接数
(3)已经处理过的请求数: 在 "保持连接" 模式下, 请求数量可能会多于连接数量;
Reading:0 Writing:1 Waiting:5
Reading: 正处于接收请求状态的连接数;
Writing: 请求已经接收完成, 正在处理请求或发送响应的过程中的连接数;
waiting: 保持连接模式, 且处于活动状态的连接数;
14,rewrite regex replacement flag;
例如:
- ...
- rewrite ^/images/(.*\.jpg)$ /imgs/$1 last;
- rewrite ^/imgs/(.*\.jpg)$ /images/$1 last;
- ...
- rewrite ^/images/(.*\.jpg)$ /imgs/$1 break;
- ...
- http://www.zhanx.wang/image/a/b/c/1.jpg --> /imgs/a/b/c/1.jpg
- flag:
last: 一旦此 rewrite 规则重写完成后, 就不再被后面其它的 rewrite 规则进行处理, 而是由 user
Agent 重新对重写后的 url 再一次发起请求, 并从头开始执行类似的过程
break: 一旦此 rewrite 规则重写完成后, 由 user
Agent 对新的 URl 重新发起请求, 且不再会被当前 location 内的任何 rewrite 规则所检查
redirect: 以 302 响应码 (临时重定向) 返回新的 URL;
permanent: 以 301 响应码 (永久重定向) 返回新的 URL;
15,if
语法: if(condition){...}
应用环境: server,location
condition:
(1) 变量名:
变量值为空串, 或者以 "0" 开始, 则为 false; 其他的均为 true;
(2) 以变量为操作数构成的比较表达式
可使用 =,!= 类似的比较操作符进行测试
(3) 正则表达式的模式匹配操作
~: 区分大小写的模式匹配检查
~*: 不区分大小写的模式匹配检查
!~ 和!~*: 对上面两种测试取反
(4) 测试路径为文件可能性:-f,!-f
(5) 测试指定路径为目录的可能性:-d, !-d
(6) 测试文件的存在性: -e, !-e
(7) 检查文件是否有执行权限:-x,!-x
例如:
- if ($http_user_agent ~* MSIE) {
- rewrite ^(.*)$ /msie/$1 break;
- }
16, 防盗链
- location ~* \.(jpg|gif|jpeg|PNG)$ {
- valid_referer none blocked www.zhanx.wang
- if ($invalid_referer) {
- rewrite ^/ http://www.zhanx.wang/403.html #(盗链提示)
- }
- }
17, 定制访问日志格式
- log_format main '$remote_addr - $remote_user [$time_local]"$request" '
- '$status $body_bytes_sent"$http_referer" '
- '"$http_user_agent" "$http_x_forwarded_for"';
- access_log /var/log/nginx/access.log main;
注意: 此处可用变量为 Nginx 各模块内建变量;
网络连接相关的配置:
1,keepalive_timeout time;
保持连接的超时时长, 默认为 75s;
2,keepalive_requests #;
在一次保持连接上允许承载最大资源请求数;
3,keepalive_disable [msie6|Safari|none]
为指定类型的浏览器禁用长连接;
4,tcp_nodelay on|off
对长连接是否使用 TCP_NODELAY 选项;
5,client_header_timeout time;
读取 http 请求报文首部的超时时长;
6,client_body_timeout time;
读取 http 请求报文 body 部分的超时时长;
7,send_timeout time;
发送响应报文的超时时长;
Nginx 配置段(3)
来源: http://www.bubuko.com/infodetail-2912619.html