了解location
1、location 区段
- location 是在 server 块中配置,根据不同的 URI 使用不同的配置,来处理不同的请求。
- location 是有顺序的,会被第一个匹配的 location 处理。
- 先后顺序以及基本语法如下:
location [=|~|~*|^~|@] pattern{……}
2、location 前缀含义
= 表示精确匹配,优先级也是最高的
^~ 表示 url 以某个常规字符串开头,理解为匹配url路径即可
~ 表示区分大小写的正则匹配
~* 表示不区分大小写的正则匹配
!~ 表示区分大小写不匹配的正则
!~* 表示不区分大小写不匹配的正则
/ 通用匹配,任何请求都会匹配到
@ 内部服务跳转
动静分离
网页分静态网页(操作简单的css,js等)以及动态网页(涉及数据库的变化),我们负载均衡的时候就需要给这些网页区分以下才能更高效解决问题,简单来说,就是使用正则表达式匹配过滤,然后交个不同的服务器。
下面我们就实现一个有负载均衡的动静分离的代理Nginx
1.虚拟好网卡信息
998 ifconfig enp0s25:1 192.168.152.101
999 ifconfig enp0s25:2 192.168.152.102
1000 ifconfig enp0s25:3 192.168.152.103
1001 ifconfig enp0s25:4 192.168.152.104
1002 ifconfig enp0s25:5 192.168.152.105
2.配置四个app
server {
listen 192.168.152.101:80;
server_name www.app1.com;
root /usr/share/nginx/mytest1;
access_log /var/log/www.mytest1.com.log main;
error_log /var/log/www.mytest1.com.error.log;
location / {
index index.html;
}
}
其他的修改以下ip就完事
3.配置app所展示的服务
cd /usr/share/nginx/
mkdir mytest1{1..4}
echo " static 192.168.152.101" > mytest1/index.html
echo " static 192.168.152.102" > mytest2/index.html
echo " move 192.168.152.103" > mytest3/index.php
echo " move 192.168.152.104" > mytest4/index.php
4.配置代理
vim proxy.conf
upstream htmlserver {
server 192.168.152.101;
server 192.168.152.102;
}
upstream phpserver {
server 192.168.152.103;
server 192.168.152.104;
}
server {
listen 192.168.152.105:80;
server_name www.proxy.com;
root /usr/share/nginx/proxy;
access_log /var/log/www.proxy.com.log main;
error_log /var/log/www.proxy.com.error.log;
location ~ .*\.(html|gif|jpg|png|bmp|swf|jpeg)$ {
index index.html;
proxy_pass http://htmlserver;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-NginX-Proxy true;
proxy_connect_timeout 30;
proxy_send_timeout 60; proxy_read_timeout 60;
proxy_buffering on;
proxy_buffer_size 32k;
proxy_buffers 4 128k;
proxy_busy_buffers_size 256k;
proxy_max_temp_file_size 256k;
}
location ~ \.php$ {
index index.html;
proxy_pass http://phpserver;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-NginX-Proxy true;
proxy_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_buffering on;
proxy_buffer_size 32k;
proxy_buffers 4 128k;
proxy_busy_buffers_size 256k;
proxy_max_temp_file_size 256k;
}
}
5启动服务,访问
curl 192.168.152.105/index.html
curl 192.168.152.105/index.html
curl 192.168.152.106/index.php
curl 192.168.152.106/index.php
正常显示内容即可
rewirite
在app.conf中
server {
listen 10.0.122.147:80;
server_name www.rourou.com;
root /usr/share/nginx/jump;
access_log /var/log/www.jump.com.log main;
error_log /var/log/www.jump.com.error.log;
location / {
index index.html;
}
#替换了/abc
#location /abc {
# rewrite .* /ccc/bbb/2.html permanent;
#}
#区分大小写替换
#location ~ /abc {
# rewrite .* /ccc/bbb/2.html permanent;
#}
#以abc为开头
#location ^~ /abc {
# rewrite .* /ccc/bbb/2.html permanent;
#}
#不区分大小写匹配
#location *~ /ABC {
# rewrite .* /ccc/bbb/2.html permanent;
#}
#精确匹配
#location = /abc {
# rewrite .* /ccc/bbb/2.html permanent;
#}
#以2015为开头,匹配后面所有的,$1为分组
#location /2015 {
# rewrite ^/2015/(.*)$ /2014/$1 permanent;
#}
#$host为域名,匹配全部内容跳到京东
#location /ccc/bbb {
# index index.html index.htm;
#}
#if ( $host ~* www.360buy.com ) {
# rewrite .* http://jd.com permanent;
#}
#$request_url为端口后面的所有的,注意修改/etc/hosts
#location /ccc/bbb {
# index index.html index.htm;
#}
#if ( $host ~* www.rourou.com ) {
# rewrite .* https://item.jd.com$request_uri permanent;
#}
#给url结尾加/ , $2为目录的最后的一个字母
#if (-d $request_filename) {
# rewrite ^(.*)([^/])$ http://$host$1$2/ permanent;
#}
#将数字以短横线拆分,拼接成路径
#location /qf {
# rewrite ^/qf/([0-9]+)-([0-9]+)-([0-9]+)(.*)$ /qf/$1/$2/$3$4 permanent;
#}
#将url中某部分移到其他位置
#if ($host ~* "^www.rourou.com$" ) {
# break;
#}
#break必须写,
#if ($host ~* "^(.*)\.rourou\.com$" ) {
# set $user $1;
# rewrite .* http://www.rourou.com/$user permanent;
#}
}
正则

rewrite中if的判断符

rewrite中urls的截取

set,return,last,break
set
set 指令是用于定义一个变量,并且赋值
if ($host ~* "^(.*)\.rourou\.com$" ) {
set $user $1;
rewrite .* http://www.rourou.com/$user permanent;
}
将匹配到的(,*)取到赋值给变量user
return
return 指令用于返回状态码给客户端
location / {
return 200
}
结果
[root@rourou conf.d]# curl -I 10.0.122.147
HTTP/1.1 200 OK
Server: nginx/1.16.1
Date: Thu, 10 Oct 2019 11:49:20 GMT
Content-Type: application/octet-stream
Content-Length: 0
Connection: keep-alive
last,break
last 标记在本条 rewrite 规则执行完后,会对其所在的 server { … } 标签重新发起请求;
break 标记则在本条规则匹配完成后,停止匹配,不再做后续的匹配;
使用 alias 指令时,必须使用 last;
使用 proxy_pass 指令时,则必须使用break
location /break {
root /usr/share/nginx;
rewrite .* /test/break.html break;
}
location /last {
#root /usr/share/nginx;
rewrite .* /test/last.html last;
}
location /test {
root /usr/share/nginx;
rewrite .* /test/test.html break;
}
当访问/last时,rewrite会跳转到/test/last.html而且遇到last,last会重新访问一次server整体,就会匹配到/test,于是就会返回test.html。
网友评论