一、root和alias
1.root用法
root是规定了nginx server的网页根目录的位置
[root@node1 vhost]# cat bybocom.conf
server {
server_name www.bybo.com;
root /bybo;
location /images {
}
}
这里说明这个server的网页根目录在linux 系统的/bybo下
当访问www.bybo.com/images/1.jpg的时候 nginx 寻找图片的时候是在nginx的根目录下找 images目录
真实的地址: root地址 + location地址
2.alias
[root@node1 ~]# ls /usr/local/pic/
1.jpg
[root@node1 vhost]# cat bybocom.conf
server {
server_name www.bybo.com;
root /bybo;
location /images {
alias /usr/local/pic; #这里pic后不要加/,如果加了会在error_log中看到报错原因
}
}
当访问www.bybo.com/images/1.jpg的时候 nginx 寻找图片的时候是在linux的 /usr/local/pic路径下找
这里的images 只是用户请求的一个uri路径,和真实存在图片的位置没有关系。
而root定义的路径和 loction 匹配的路径是有必然联系的
二、nginx常用变量
1.$request
完整的原始的请求行,GET /nginx/varindex?a=1&b=2 HTTP/1.1
2.$request_uri
完整的原始请求URI,访问的URL除去域名(或IP)和port,如/nginx/varindex?a=1&b=2
3.$args
请求行中的全部参数 $args等于a=1&b=2
4.$arg_参数名
$arg_a:表示a=1&b=2 中的a参数的值
$arg_b: 表示a=1&b=2 中的b参数的值
5.$remote_addr
客户端ip地址
6.$remote_port
客户端端口
还有很多变量,这些变量在日志中都使用到了nginx的变量,为了看到效果,我们在在浏览器中返回一些变量
server {
server_name www.bybo.com;
root /bybo;
location / {
default_type text/plain; #响应类型是文本
return 200 "$remote_addr $request_uri";
}
}
结果
[root@node1 vhost]# curl www.bybo.com/abc/a.html
127.0.0.1 /abc/a.html
7.设置一个变量
location / {
set $name "zhangsan";
return 200 "$name";
}
三、反向代理
主机 | IP |
---|---|
node1 | 192.168.1.103 |
node3 | 192.168.1.104 |
1.node1操作
[root@node1 ~]# cd /usr/local/nginx/html
[root@node1 html]# cat index.html
<h1>1.103</ht>
[root@node1 html]# cat server/index.html
<h1>1.103_server</h1>
[root@node1 ~]# cd /usr/local/nginx/conf/vhost
[root@node1 vhost]# cat bybocom.conf
server {
server_name localhost;
location /server {
index index.html;
}
}
浏览器访问:http://192.168.1.103/server/
2.node3操作
[root@node3 ~]# cd /usr/local/nginx/html/
[root@node3 html]# cat index.html
<h1>1.104</h1>
[root@node3 html]# cat server/index.html
<h1>192.168.1.104_server</h1>
3.反向代理proxy_pass
3.1 结尾不带/
#在node1上操作
server {
server_name localhost;
location /server {
proxy_pass http://192.168.1.104;
}
}
浏览器访问http://192.168.1.103/server/ 发现内容跳转到1.104的/server目录下的index.html
3.2 结尾带/
#在node1上操作
location /server {
proxy_pass http://192.168.1.104/;
}
浏览器访问http://192.168.1.103/server/ 发现转发到了1.104的网页根目录下index.html
4.proxy_set_header
该指令可以更改nginx服务器接收到的客户端请求的请求头信息,然后将新的请求头发送给代理的服务器
语法:
proxy_set_header 字段名 值
4.1 配置1.103
server {
server_name localhost;
location / {
proxy_pass http://192.168.1.104;
}
}
4.2 查看访问日志
1.103的访问日志
#这里的源地址是客户端的地址
192.168.1.105 - - [25/Sep/2021:21:39:40 +0800] "GET / HTTP/1.1" 304 0 "-" 4
1.104的访问日志
#这里的源地址是1.103
192.168.1.103 - - [25/Sep/2021:21:39:25 +0800] "GET / HTTP/1.0" 304 0
4.3 配置反向代理的header
在1.103上配置
server {
server_name localhost;
location / {
proxy_set_header zhangsan $remote_addr; #自定义header变量名为zhangsan
proxy_pass http://192.168.1.104;
}
}
4.4 引用自定义header中的变量
在1.104的日志中引用在1.103中的自定义header变量。
log_format main '$remote_addr **"$http_zhangsan"** - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ''"$http_user_agent" --"$http_x_forwarded_for"--';
location / {
root html;
index index.html index.htm;
}
access_log logs/access.log main;
特别注意:
(1)在引用转发过来的header中的自定义变量前边必须要加上http_
(2)自定义变量中尽量不要有- 或者_ 或者特殊字符。可能
例如:
$http_自定义变量名称
4.5 再次访问1.103
在1.104上查看访问日志,发现引用成功
192.168.1.103 **"192.168.1.105"** - -
四、负载均衡
1.upstream 服务池
upstream web {
server 192.168.1.100;
server 192.168.1.103;
}
配置反向代理给这个资源组
location / {
proxy_pass http://web;
}
2.负载均衡的状态
状态 | 概述 |
---|---|
down | 当前的server暂时不参与负载均衡 |
backup | 预留的备份服务器 |
max_fails | 允许请求失败的次数 |
fail_timeout | 经过max_fials失败后,服务暂停时间,默认暂停10秒 |
max_conns | 限制最大连接数 |
2.1 down
请求不会在发送到1.100服务器上了
upstream web {
server 192.168.1.100 down;
server 192.168.1.103;
}
2.2 backup
只有在1.100 服务不能正常使用的时候 请求就会负载到1.103上
upstream web {
server 192.168.1.100;
server 192.168.1.103 backup;
}
2.3 负载均衡算法
算法 | 说明 |
---|---|
rr(轮询) | 默认方式 |
weight | 权重 |
ip_hash | 根据ip分配方式 |
least_conn | 根据最少连接 |
url_hash | 依据url分配方式 |
fair | 依据响应时间方式 |
2.3.1 weight
upstream web {
server 192.168.1.100 weight=10;
server 192.168.1.103 weight=5;
}
2.3.2 ip_hash
upstream web {
ip_hash;
server 192.168.1.100;
server 192.168.1.103;
}
2.3.3 least_conn
适用于服务处理请求时间不一致的场景
upstream web {
least_conn;
server 192.168.1.100;
server 192.168.1.103;
}
五、rewrite功能
1.set 设置变量
在 二 中已经介绍过了
语法:
set $变量名 值
2.if判断
server {
set $name 'zhangsan';
location / {
default_type text/plain;
if ($name = zhangsan) {
return 200 '$name is empty';
}
}
}
server {
set $name 'zhangsan';
location / {
default_type text/html;
if (!-f $request_filename) {
return 200 '<h1>file not foud</h1>';
}
}
}