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/*.conf

主配置文件(全局配置)

使用nginx时需要使用docker吗 nginx必须用root启动吗_后端


GeoIP的用法

使用nginx时需要使用docker吗 nginx必须用root启动吗_nginx_02


虚拟主机

使用nginx时需要使用docker吗 nginx必须用root启动吗_使用nginx时需要使用docker吗_03


基于用户的访问控制

  1. 建立口令文件
  2. 实现认证

    Nginx的状态

    Active connections: 22 当前 nginx 正处理的活动的连接数
    server accepts handled requests 27 27 434 说明: 27 nginx 从启动到现在总共处理的连接数 27 nginx 从启动
    到现在成功创建的三次握手次数(如果与前一个值相同,说明中间过程没有失败连接) 434 nginx 共处理的请求数
    (requests);以上说明nginx每次成功连接后都处理了多个(434/27)请求 connection 连接数,tcp连接 request http
    请求(GET/POST/DELETE/HEAD等各种方法的请求)
    Reading: 2 Writing: 1 Waiting: 19 说明: Reading: 2 nginx读取客户端Header的个数(nginx正在读取客户端请求
    首部的个数) Writing: 1 返回给客户端的header的个数(nginx正在读取客户端请求主体部分的个数、正处理着其请求
    内容的请求的个数或者正在向其客户发送响应的个数) --响应头 Waiting: 19 等待的请求数(长连接模式的保持连接个
    数)c
    proxy代理
    1、基本反向代理

    其中 op 包括: = 路径精确匹配,只包含当前路径,不包含其它任何路径。 ^~ 不做正则表达式元字符匹配,做逐个
    字符搜索匹配 ~* 不区分大小写,元字符匹配 ~ 区分大小写,元字符匹配
    例1:

    如果location的URI前没有任何操作符[op],上游服务器的URL中必须以"/"结尾或明确指定URI;否则nginx会将客
    户端输入的URI追加到上游服务器的URL上,导致访问异常。 如果location的URI是通过模式匹配定义的,其URI将直
    接被传递至上游服务器,而不能为上游服务器指定转换后的URI; 而且后端服务器URL中不能包含任何URI信息。
    例2:

    例3:

    2、upstream 定义后端服务器组
    Syntax: upstream name { ... } Default: — Context: http
    upstream模块常用的指令有: ip_hash:基于客户端IP地址完成请求的分发,它可以保证来自于同一个客户端的请
    求始终被转发至同一个upstream服务器; keepalive:每个worker进程为发送到upstream服务器的连接所缓存的
    个数; least_conn:最少连接调度算法; server:定义一个upstream服务器的地址,还可包括一系列可选参数,
    如: weight:权重; max_fails:最大失败连接次数,默认为1,失败连接的超时时长由fail_timeout指定;
    fail_timeout:等待请求的目标服务器发送响应的时长; backup:用于fallback的目的,所有服务均故障时才启动
    此服务器,类似于LVS Sorry Server; down: 暂停对该服务器的调度 upstream模块的负载均衡算法主要有三
    种,轮调(round-robin)、ip哈希(ip_hash)和最少连接(least_conn)三种。注:当使用ip_hash时可以解决client
    session问题,但服务器状态不可使用weight和backup。 fair: 可以根据请求页面的大小和加载时间长短进行调
    度,使用第三方的upstream_fair模块 url_hash: 按请求的url的hash进行调度,从而使每个url定向到同一服务
    器,使用第三方的hash模块 例1:

    例2:

    例3:

例4:

upstream模块也能为非http类的应用实现负载均衡,如下面的示例定义了nginx为memcached服务实现负载均衡

之目的。

使用nginx时需要使用docker吗 nginx必须用root启动吗_后端_04


传送client IP

使用nginx时需要使用docker吗 nginx必须用root启动吗_后端_05


Rewrite相关指令

Nginx Rewrite相关指令有if、rewrite、set、return等。 1、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 防盗链

使用nginx时需要使用docker吗 nginx必须用root启动吗_运维_06


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:

使用nginx时需要使用docker吗 nginx必须用root启动吗_memcached_07


例2:

使用nginx时需要使用docker吗 nginx必须用root启动吗_后端_08


例3:

使用nginx时需要使用docker吗 nginx必须用root启动吗_nginx_09


转载于:https://blog.51cto.com/13575680/2096225