nginx 的功能
http web 服务器
proxy (反向代理)
安装
源码安装
./configure \
--prefix=/usr \ --sbin-path=/usr/sbin/nginx \ --conf-path=/etc/nginx/nginx.conf \ --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 \ --user=nginx \ --group=nginx \ --with-
http_ssl_module \ --with-http_flv_module \ --with-http_stub_status_module \ --with-http_gzip_static_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/fcgi/ \ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \ --http-scgi-
temp-path=/var/tmp/nginx/scgi \ --with-pcre make && make install
rpm 安装
yum install nginx
Log 配置
默认位置/var/log/nginx ,须要重定向log的位置
/etc/logrotate.d/nginx, 修改 属组为nginx
配置文件
主配置文本 /etc/nginx/nginx.conf
片断配置文件 /etc/nginx/conf.d/*.confnginx
主配置文件(全局配置)
GeoIP的用法
虚拟主机
基于用户的访问控制web
例4:
upstream模块也能为非http类的应用实现负载均衡,以下面的示例定义了nginx为memcached服务实现负载均衡
之目的。
传送client IP
Rewrite相关指令
Nginx Rewrite相关指令有if、rewrite、set、return等。 一、if指令 if 的语法 if (condition) { ... } 应用于server和
location环境内(if 与条件之间必须有空格) if 能够支持以下条件判断匹配符号 ~ 为区分大小写匹配 ~ 为不区分大
小写匹配 !~和!~ 分别为区分大小写不匹配及不区分大小写不匹配 -f 和!-f 用来判断是否存在文件 -d 和!-d 用来判断
是否存在目录 -e 和!-e 用来判断是否存在文件或目录 -x 和!-x 用来判断文件是否可执行
在匹配过程当中能够引用一些Nginx的全局变量,更多的变量请参考 http://wiki.nginx.org/NginxHttpCoreModule
的 Variables 部分 $args 请求中的参数; $document_root 针对当前请求的根路径设置值; $host 请求信息中
的"Host",若是请求中没有Host行,则等于设置的服务器名; $limit_rate 对链接速率的限制; $request_method 请
求的方法,好比"GET"、"POST"等; $remote_addr 客户端地址; $remote_port 客户端端口号; $remote_user 客户
端用户名,认证用; $request_filename 当前请求的文件路径名(带root指定的路径,即网站的主目录)
$request_uri 当前请求的文件路径名(不带root指定的路径)
与 args相同;
$scheme 所用的协议,比
如http或者是https $server_protocol 请求的协议版本,"HTTP/1.0"或"HTTP/1.1"; $server_addr 服务器地址,如
果没有用listen指明服务器地址,使用这个变量将发起一次系统调用以取得地址(形成资源浪费); $server_name 请
求到达的服务器名;$server_port 请求到达的服务器端口号;
与 uri同样,URI地址;
例1 匹配访问的url地址是不是个目录
if (-d $request_filename) {
...;
}
例2 匹配访问的地址是否以www开头
if ($host ~ ^www) {
...;
}
例3 防盗链
referer指令 Syntax: valid_referers none | blocked | server_names | string ...; Default: — Context: server,
location none the“Referer” field is missing in the request header; (即直接在客户端浏览器地址栏中输入的)
blocked the“Referer” field is present in the request header, but its value has been deleted by a firewall or
proxy server; such values are strings that do not start with “http://” or “https://”; server_names the “Referer”
request header field contains one of the server names; arbitrary string defines a server name and an optional
URI prefix. A server name can have an “” at the beginning or end. During the checking, the server’s port in the
“Referer” field is ignored; regular expression the first symbol should be a “~”. It should be noted that an
expression will be matched against the text starting after the “http://” or “https://”
Rewrite 指令 Syntax: rewrite regex replacement [flag]; Default: — Context: server, location, if flag: last stops
processing the current set of ngx_http_rewrite_module directives and starts a search for a new location
matching the changed URI; 至关于Apache里的[L]标记,表示完成rewrite break stops processing the current set
of ngx_http_rewrite_module directives as with the break directive; 本条规则匹配完成后,终止匹配,再也不匹配后
面的规则 redirect returns a temporary redirect with the 302 code; used if a replacement string does not start
with “http://” or “https://”; 返回302临时重定向,浏览器地址会显示跳转后的URL地址 permanent returns a
permanent redirect with the 301 code.返回301永久重定向,浏览器地址会显示跳转后URL地址
last和break标记的区别在于,last标记在本条rewrite规则执行完后,会对其所在的server { ... } 标签从新发起请
求,而break标记则在本条规则匹配完成后,中止匹配,再也不作后续的匹配。另有些时候必须使用last,好比在使用
alias指令时,而使用proxy_pass指令时则必须使用break。
例1:
例2:
例3:正则表达式