Nginx 基本使用
Nginx 文件结构
... #全局块
events { #events块
...
}
http #http块
{
... #http全局块
server #server块
{
... #server全局块
location [PATTERN] #location块
{
...
}
location [PATTERN]
{
...
}
}
server
{
...
}
... #http全局块
}
1、全局块:配置影响nginx全局的指令。一般有运行nginx服务器的用户组,nginx 进程 pid 存放路径,日志存放路径,配置文件引入,允许生成 worker process 数等。
2、events 块:配置影响 nginx 服务器或与用户的网络连接。有每个进程的最大连接数,选取哪种事件驱动模型处理连接请求,是否允许同时接受多个网路连接,开启多个网络连接序列化等。
3、http 块:可以嵌套多个 server,配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置。如文件引入,mime-type 定义,日志自定义,是否使用sendfile传输文件,连接超时时间,单连接请求数等。
4、server 块:配置虚拟主机的相关参数,一个 http 中可以有多个server。
5、location 块:配置请求的路由,以及各种页面的处理情况。
server {
#监听端口号
listen 80;
#主机名
server_name localhost;
#匹配路径
#文件根目录
location / {
root html;
#默认页名称
index index.html index.htm;
}
#报错编码对应页面
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
- server : 用于定义服务,http中可以有多个server块
- listen : 指定服务器侦听请求的IP地址和端口,如果省略地址,服务器将侦听所有地址,如果省略端口,则使用标准端口
- server_name : 服务名称,用于配置域名
- location : 用于配置映射路径uri对应的配置,一个server中可以有多个location, location后面跟一个uri,可以是一个正则表达式, / 表示匹配任意路径, 当客户端访问的路径满足这个uri时就会执行location块里面的代码
- root : 根路径
虚拟主机-多域名servername
原本一台服务器只能对应一个站点,通过虚拟主机技术可以虚拟化成多个站点同时对外提供服务
servername 匹配规则
我们需要注意的是 servername 匹配分先后顺序,写在前面的匹配上就不会继续往下匹配了。
完整匹配
我们可以在同一 servername 中匹配多个域名
server_name vod.coding3min.com www1.coding3min.com;
通配符匹配
server_name *.coding3min.com
通配符结束匹配
server_name vod.*;
正则匹配
server_name ~^[0-9]+\.coding3min\.com$
路由转发 location
增加多个location
location /css {
root /usr/local/nginx/static;
index index.html index.htm;
}
location /images {
root /usr/local/nginx/static;
index index.html index.htm;
}
location /js {
root /usr/local/nginx/static;
index index.html index.htm;
}
location 前缀
- / 通用匹配,任何请求都会匹配到。
- = 精准匹配,不是以指定模式开头 。
- ~ 正则匹配,区分大小写 。
- \* 正则匹配,不区分大小写。
- ^~ 非正则匹配,匹配以指定模式开头的location。
location匹配顺序
多个正则location直接按书写顺序匹配,成功后就不会继续往后面匹配
普通(非正则)location会一直往下,直到找到匹配度最高的(最大前缀匹配)
当普通location与正则location同时存在,如果正则匹配成功,则不会再执行普通匹配
所有类型location存在时,=匹配 > ^~匹配 > 正则匹配 > 普通(最大前缀匹配)
location ~*/(css|img|js) {
root /usr/local/nginx/static;
index index.html index.htm;
}
正则
. :匹配除换行符以外的任意字符
? :重复0次或1次
+ :重复1次或更多次
* :重复0次或更多次
\d :匹配数字
^ :匹配字符串的开始
$ :匹配字符串的结束
{n} :重复n次
{n,} :重复n次或更多次
[c] :匹配单个字符c
[a-z] :匹配a-z小写字母的任意一个
(a|b|c) : 属线表示匹配任意一种情况,每种情况使用竖线分隔,一般使用小括号括括住,匹配符合a字符 或是b字符 或是c字符的字符串
\ 反斜杠:用于转义特殊字符
小括号()之间匹配的内容,可以在后面通过$1来引用,$2表示的是前面第二个()里的内容。
alias与root
location /css {
alias /usr/local/nginx/static/css;
index index.html index.htm;
}
root用来设置根目录,而alias在接受请求的时候在路径上不会加上location。
比如上面如果是root 则会变成 /usr/local/nginx/static/css/css ,如果是alias则不会有变化。
- alias指定的目录是准确的,即location匹配访问的path目录下的文件直接是在alias目录下查找的;
- root指定的目录是location匹配访问的path目录的上一级目录,这个path目录一定要是真实存在root指定目录下的;
反向代理
最简单的反向代理
location / {
proxy_pass http://coding3min.com/;
}
代表访问/的时候是访问 http://coding3min.com/
这里是反向代理到另一个网站,也可以写成upstream转发到多个不同的网站/服务器。
动静分离
动静分离是让动态网站里的动态网页根据一定规则把不变的资源和经常变的资源区分开来,动静资源做好了拆分以后,我们就可以根据静态资源的特点将其做缓存操作,这就是网站静态化处理的核心思路。
upstream web_servers {
server localhost:8081;
server localhost:8082;
}
server {
listen 80;
server_name localhost;
set $doc_root /usr/local/var/www;
location ~* \.(gif|jpg|jpeg|png|bmp|ico|swf|css|js)$ {
root $doc_root/img;
}
location / {
proxy_pass http://web_servers;
# 指定Header Host后端才能拿到域名,不指定则拿不到Host
proxy_set_header Host $host:$server_port;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root $doc_root;
}
}
负载均衡
轮询 Round Robin
默认情况下使用轮询方式,逐一转发,这种方式适用于无状态请求。
weight(权重)
指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
upstream httpd {
server 127.0.0.1:8050 weight=10 down;
server 127.0.0.1:8060 weight=1;
server 127.0.0.1:8060 weight=1 backup;
}
- down:表示当前的server暂时不参与负载
- weight:默认为1 weight越大,负载的权重就越大。
- backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。
下面是比较常用的例子:
upstream web_servers {
server localhost:8081;
server localhost:8082;
}
server {
listen 80;
server_name localhost;
#access_log logs/host.access.log main;
location / {
proxy_pass http://web_servers;
# 必须指定Header Host
proxy_set_header Host $host:$server_port;
}
}
ip_hash
根据客户端的ip地址转发同一台服务器,可以保持回话。
upstream test {
ip_hash;
server localhost:8080;
server localhost:8081;
}
least_conn
最少连接访问
http{
upstream sampleapp {
least_conn;
server localhost:8080;
server localhost:8081;
}
....
server{
listen 80;
...
location / {
proxy_pass http://sampleapp;
}
}
rewrite URL重写
rewrite是实现URL重写的关键指令,根据regex (正则表达式)部分内容, 重定向到replacement,结尾是flag标记。
rewrite <regex> <replacement> [flag];
关键字 正则 替代内容 flag标记
例如下面内容,正则^/([0-9]+).html$匹配到的内容替换成替代内容/index.jsp?pageNum=$1,注意括号里的内容取出,替换到$1 位置。下面内容应该是一个翻页请求:
rewrite ^/([0-9]+).html$ /index.jsp?pageNum=$1 break;
flag标记:rewrite支持的flag标记
- last 本条规则匹配完成后,继续向下匹配新的location URI规则
- break 本条规则匹配完成即终止,不再匹配后面的任何规则
- redirect 返回302临时重定向,浏览器地址会显示跳转后的URL地址
- permanent 返回301永久重定向,浏览器地址栏会显示跳转后的URL地址
rewrite参数的标签段位
server,location,if
虚拟主机、路由、判断
速记
########### 每个指令必须有分号结束。#################
#user administrator administrators; #配置用户或者组,默认为nobody nobody。
#worker_processes 2; #允许生成的进程数,默认为1
#pid /nginx/pid/nginx.pid; #指定nginx进程运行文件存放地址
error_log log/error.log debug; #制定日志路径,级别。这个设置可以放入全局块,http块,server块,级别以此为:debug|info|notice|warn|error|crit|alert|emerg
events {
accept_mutex on; #设置网路连接序列化,防止惊群现象发生,默认为on
multi_accept on; #设置一个进程是否同时接受多个网络连接,默认为off
#use epoll; #事件驱动模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport
worker_connections 1024; #最大连接数,默认为512
}
http {
include mime.types; #文件扩展名与文件类型映射表
default_type application/octet-stream; #默认文件类型,默认为text/plain
#access_log off; #取消服务日志
log_format myFormat '$remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for'; #自定义格式
access_log log/access.log myFormat; #combined为日志格式的默认值
sendfile on; #允许sendfile方式传输文件,默认为off,可以在http块,server块,location块。
sendfile_max_chunk 100k; #每个进程每次调用传输数量不能大于设定的值,默认为0,即不设上限。
keepalive_timeout 65; #连接超时时间,默认为75s,可以在http,server,location块。
upstream mysvr {
server 127.0.0.1:7878;
server 192.168.10.121:3333 backup; #热备
}
error_page 404 https://www.baidu.com; #错误页
server {
keepalive_requests 120; #单连接请求上限次数。
listen 4545; #监听端口
server_name 127.0.0.1; #监听地址
location ~*^.+$ { #请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写。
#root path; #根目录
#index vv.txt; #设置默认页
proxy_pass http://mysvr; #请求转向mysvr 定义的服务器列表
deny 127.0.0.1; #拒绝的ip
allow 172.18.5.54; #允许的ip
}
}
}