ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

nginx2

2022-01-09 17:35:41  阅读:199  来源: 互联网

标签:10.0 http nginx2 nginx proxy conf root


5.1.4 return指令

return用于完成对请求的处理,并直接向客户端返回相应状态码

 

 

 

 

503与404的区别

301永久,势必缓存重定向信息

302临时重定向,不会缓存网站信息

 

 

last与break的区别

 

 

n'ginx变量名不支持短横杠

 

 

 

 

面试

5.2.6:其他案列

image-20211214115747239

 

 

 

6.NGINX反向代理

负责负载调度问题

image-20211214150823694

nginx实现后端健康性检查问题

LVS特性:

一个机器坏掉,后边机器无法解决,存在单点失败问题

缺失后端服务器的健康性检查

反向代理:reverse proxy,指的是代理外网用户的请求到内部的指定的服务器,并将数据返回给用户的一种方式,这是用的比较多的一种方式。

Nginx 除了可以在企业提供高性能的web服务之外,另外还可以将 nginx 本身不具备的请求通过某种预定义的协议转发至其它服务器处理,不同的协议就是Nginx服务器与其他服务器进行通信的一种规范,主要在不同的场景使用以下模块实现不同的功能

ngx_http_proxy_module: #将客户端的请求以http协议转发至指定服务器进行处理
ngx_http_upstream_module #用于定义为proxy_pass,fastcgi_pass,uwsgi_pass等指令引用的后端服务器分组
ngx_stream_proxy_module:#将客户端的请求以tcp协议转发至指定服务器处理
ngx_http_fastcgi_module:#将客户端对php的请求以fastcgi协议转发至指定服务器助理
ngx_http_uwsgi_module: #将客户端对Python的请求以uwsgi协议转发至指定服务器处理

逻辑调用关系:

生产环境部署结构:

两道防火墙进来虚拟地址,然后访问外部服务,转发带内部服务

访问逻辑图

image-20211214151547785

同构协议:两边协议一样

后端端口出问题连不上:502

LVS与NGINX的区别:

image-20211218153720427

LVS:客户端发送请求目标地址为lvs的VIP,LVS收到请求后,将请求再次转发,注意,lvs只是转发,并不参与,干预,LVS最后端口可以看模式,nat可以用8080,dr与taner就不支持8080.

nginx:握手两次,nginx重新封装报文,并发送

6.1 实现 http 反向代理

官方文档: https://nginx.org/en/docs/http/ngx_http_proxy_module.html

6.1.1 http 协议反向代理

6.1.1.1 反向代理配置参数

#官方文档:https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass 
proxy_pass;
#用来设置将客户端请求转发给的后端服务器的主机,可以是主机名(将转发至后端服务做为主机头首部)、IP地址:端口的方式
#也可以代理到预先设置的主机群组,需要模块ngx_http_upstream_module支持
#示例:
location /web {
  index index.html;
  proxy_pass http://10.0.0.18:8080; #8080后面无uri,即无 / 符号,需要将location后面url 附加到proxy_pass指定的url后面,此行为类似于root
 #proxy_pass指定的uri不带斜线将访问的/web,等于访问后端服务器
http://10.0.0.18:8080/web/index.html,即后端服务器配置的站点根目录要有web目录才可以被访问
   # http://nginx/web/index.html ==> http://10.0.0.18:8080/web/index.html
  proxy_pass http://10.0.0.18:8080/;   #8080后面有uri,即有 / 符号,相当于置换,即访问/web时实际返回proxy_pass后面uri内容.此行为类似于alias
   #proxy_pass指定的uri带斜线,等于访问后端服务器的http://10.0.0.18:8080/index.html 内容返回给客户端
}  # http://nginx/web/index.html ==> http://10.0.0.18:8080
   
#重启Nginx测试访问效果:
#curl -L http://www.magedu.org/web
#如果location定义其uri时使用了正则表达式模式(包括~,~*,但不包括^~),则proxy_pass之后必须不能使用uri; 即不能有/ ,用户请求时传递的uri将直接附加至后端服务器之后
server {
...
server_name HOSTNAME;
location ~|~* /uri/ {
proxy_pass http://host:port; #proxy_pass后面的url 不能加/
}
...
}
http://HOSTNAME/uri/ --> http://host/uri/

 

 

6.1.1.2 实战案例: 反向代理单台 web 服务器

要求:将用户对域 www.magedu.org 的请求转发给后端服务器处理

[root@centos8 ~]# cat /apps/nginx/conf/conf.d/pc.conf
server {
listen 80;
server_name www.magedu.org;
location / {
    proxy_pass http://10.0.0.18/; # http://10.0.0.18/ 最后的 / 可加或不加
}
}
#重启Nginx 并访问测试

image-20211214155927345

如果后端服务器无法连接((比如:iptables -AINPUT -s nginx_ip -j REJECT或者systemctl stop httpd)), 会出现下面提示

image-20211214155935846

默认在1分钟内后端服务器无法响应(比如:iptables -AINPUT -s nginx_ip -j DROP),会显示下面的504超时提示

image-20211214160006196

后端服务器出现问题。502,504

 

 

6.1.1.3 实战案例: 指定 location 实现反向代理

6.1.1.3.1 针对指定的 location
server {
listen 80;
server_name www.magedu.org;
location / {
  index index.html index.php;
  root /data/nginx/html/pc;
  }
location /static {
  #proxy_pass http://10.0.0.18:80/; #注意有后面的/, 表示置换到原来根的位置
  proxy_pass http://10.0.0.18:80;  #后面没有 / , 表示附加
}
}
#后端web服务器必须要有相对于的访问URL
[root@centos8 ~]# mkdir /var/www/html/static
[root@centos8 ~]# echo "web1 page for apache" > /var/www/html/index.html
[root@centos8 ~]# echo "web2 page for apache" > /var/www/html/static/index.html
#重启Nginx并访问测试:
[root@centos8 ~]# curl -L http://www.magedu.org/
pc web
[root@centos8 ~]# curl -L http://www.magedu.org/static
web2 page for apache
#Apache的访问日志:
[root@centos8 ~]# tail -f /var/log/httpd/access_log
10.0.0.8 - - [04/Mar/2019:18:52:00 +0800] "GET /static/ HTTP/1.1" 200 21 "-"
"curl/7.29.0"
location /static {
  #proxy_pass http://10.0.0.18:80/; #注意有后面的/, 表示置换到原来根的位置

 

6.1.1.3.2 针对特定的资源实现代理

image-20211214160320215

[root@centos8 ~]#vim /apps/nginx/conf/conf.d/pc.conf
server {
......
location ~ \.(jpe?g|png|bmp|gif)$ {
  proxy_pass http://10.0.0.18:8080;     #如果加/ 语法出错,置换不了                            
                                         
}
}
#如果 http://10.0.0.18/ 有 / 语法出错    
[root@centos8 ~]#nginx -t
nginx: [emerg] "proxy_pass" cannot have URI part in location given by regular
expression, or inside named location, or inside "if" statement, or inside
"limit_except" block in /apps/nginx/conf/conf.d/pc.conf:19
nginx: configuration file /apps/nginx/conf/nginx.conf test failed

6.1.1.4 反向代理示例: 缓存功能

缓存功能默认关闭状态,需要先动配置才能启用

proxy_cache zone_name | off; 默认off
#指明调用的缓存,或关闭缓存机制;Context:http, server, location
#zone_name 表示缓存的名称.需要由proxy_cache_pat h事先定义

缓存索赢标记

proxy_cache_key string;
#缓存中用于“键”的内容,默认值:proxy_cache_key $scheme$proxy_host$request_uri   #判断页面被缓存过的依据

将缓存的路劲做hash运算,得出的hash值缓存在nginx的目录树中,按照特征缓存到不同的hash目录中。比如有100万个文件,放入16*3目录中,则并发将会变小。image-20211218181327166

proxy_cache_valid [code ...] time;
#定义对特定响应码的响应内容的缓存时长,定义在http{...}中
示例:
proxy_cache_valid 200 302 10m;
proxy_cache_valid 404 1m;
proxy_cache_path;
#定义可用于proxy功能的缓存;Context:http
proxy_cache_path path [levels=levels] [use_temp_path=on|off]
keys_zone=zone_name:size [inactive=time] [max_size=size] [manager_files=number]
[manager_sleep=time] [manager_threshold=time] [loader_files=number]
[loader_sleep=time] [loader_threshold=time] [purger=on|off]
[purger_files=number] [purger_sleep=time] [purger_threshold=time];
#示例:在http配置定义缓存信息
proxy_cache_path /var/cache/nginx/proxy_cache #定义缓存保存路径,proxy_cache会自动创建
  levels=1:2:2 #定义缓存目录结构层次,1:2:2可以生成2^4x2^8x2^8=2^20=1048576个目录
  keys_zone=proxycache:20m #指内存中缓存的大小,主要用于存放key和metadata(如:使用次数),一般1M可存放8000个左右的key
  inactive=120s  #缓存有效时间  
  max_size=10g; #最大磁盘占用空间,磁盘存入文件内容的缓存空间最大值
  #调用缓存功能,需要定义在相应的配置段,如server{...};或者location等
proxy_cache proxycache;
proxy_cache_key $request_uri; #对指定的数据进行MD5的运算做为缓存的key
proxy_cache_valid 200 302 301 10m; #指定的状态码返回的数据缓存多长时间
proxy_cache_valid any 1m;   #除指定的状态码返回的数据以外的缓存多长时间,必须设置,否则不会缓存
proxy_cache_use_stale error | timeout | invalid_header | updating | http_500 | 
http_502 | http_503 | http_504 | http_403 | http_404 | off ; #默认是off
#在被代理的后端服务器出现哪种情况下,可直接使用过期的缓存响应客户端
#示例
proxy_cache_use_stale error http_502 http_503;
proxy_cache_methods GET | HEAD | POST ...;
#对哪些客户端请求方法对应的响应进行缓存,GET和HEAD方法总是被缓存

扩展知识: 清理缓存

方法1: rm -rf 缓存目录
方法2: 第三方扩展模块ngx_cache_purge

 

 

image-20211218165805436

域名yum -y  install http

 

image-20211218165838439

image-20211218165905996

 

 

改后端端口8080

image-20211218170042682

贷理服务器端口

image-20211218170231047

 

 

image-20211218170936904

 

6.1.1.6 实现反向代理客户端 IP 透传

6.1.1.6.1 一级代理实现客户端IP透传

image-20211218183801687

[root@centos8 ~]# cat /apps/nginx/conf/conf.d/pc.conf
server {
listen 80;
server_name www.magedu.org;
location / {
  index index.html index.php;
  root /data/nginx/html/pc;
  proxy_pass http://10.0.0.18;
   #proxy_set_header X-Real-IP $remote_addr;           #只添加客户端IP到请求报文头部,转发至后端服务器
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #添加客户端IP和反向代理服务器IP到请求报文头部
}
}
#说明$proxy_add_x_forwarded_for
#此变量表示将客户端IP追加请求报文中X-Forwarded-For首部字段,多个IP之间用逗号分隔,如果请求中没 有X-Forwarded-For,就使用$remote_addr
#重启nginx
[root@centos8 ~]#systemctl restart nginx
#后端Apache配置:
[root@centos8 ~]#vim /etc/httpd/conf/httpd.conf
LogFormat "%{X-Forwarded-For}i %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined#重启apache访问web界面并验证apache日志
[root@centos8 ~]#cat /var/log/httpd/access_log
10.0.0.1 10.0.0.8 - - [05/Mar/2019:00:40:46 +0800] "GET / HTTP/1.0" 200 19 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36"
#Nginx配置:
[root@centos8 conf.d]# cat /apps/nginx/conf/nginx.conf
"$http_x_forwarded_for"' #默认日志格式就有此配置
#重启nginx访问web界面并验证日志格式:
10.0.0.8 - - [04/Mar/2019:16:40:51 +0800] "GET / HTTP/1.0" 200 24 "-"
"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/72.0.3626.119 Safari/537.36" "10.0.0.1"
6.1.1.6.2 多级代理实现客户端 IP 透传

范例: 多级代理实现IP透传

image-20211219104657046

配置三台机器、

10.0.0.9 nginx

10.0.0.29nginx

10.0.0.7 httpd

10.0.0.19修改hosts文件

10.0.0.9
[root@loaclhost ~]# vim /apps/nginx/conf/conf.d/pc.conf

server{
listen 80;
server_name www.bai.org;
root /data/nginx/html/pc;
default_type text/html;
location / {
proxy_pass http://10.0.0.29;
proxy_set_header X-M47-Real-IP $remote_addr;#加报头
}
}
10.0.0.29
[root@loaclhost conf]# vim /apps/nginx/conf/nginx.conf

#access_log logs/host.access.log main;

location / {
root html;
index index.html index.htm;
proxy_pass http://10.0.0.7;
proxy_set_header X-M47-Real-IP $remote_addr;#也可以写网址页面如1.0.0.9
}

#error_page 404 /404.html;
#10.0.0.7
[root@centos7f ~]# yum -y install httpd
[root@centos7f ~]# systemctl start httpd
[root@centos7f ~]# vim /etc/httpd/conf/httpd.conf#修改日志格式
# The following directives define some format nicknames for use with
# a CustomLog directive (see below).
#
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" \"%{X-M47-Real-IP}i\"" testlog
LogFormat "%h %l %u %t \"%r\" %>s %b" common

<IfModule logio_module>
# You need to enable mod_logio.c to use %I and %O
CustomLog "logs/access_log" testlog
</IfModule>
[root@centos7f ~]# systemctl restart httpd
10.0.0.19测试
[root@text ~]# curl www.bai.org
10.0.0.7
[root@centos7f ~]# cat /var/log/httpd/access_log#查看日志
10.0.0.29 - - [19/Dec/2021:15:04:40 +0800] "GET /m.txt HTTP/1.0" 404 203 "-" "curl/7.61.1" "10.0.0.9"
10.0.0.29 - - [19/Dec/2021:15:04:43 +0800] "GET /m.txt HTTP/1.0" 404 203 "-" "curl/7.61.1" "10.0.0.9"

 

6.1.2 http 反向代理负载均衡

在上一个节中Nginx可以将客户端的请求转发至单台后端服务器但是无法转发至特定的一组的服务器,而且不能对后端服务器提供相应的服务器状态监测,Nginx 可以基于ngx_http_upstream_module模块提供服务器分组转发、权重分配、状态监测、调度算法等高级功能

官方文档: https://nginx.org/en/docs/http/ngx_http_upstream_module.html

6.1.2.1 http upstream配置参数

#自定义一组服务器,配置在http块内
upstream name {
server .....
......
}
#示例
upstream backend {
  server backend1.example.com weight=5;
  server 127.0.0.1:8080  max_fails=3 fail_timeout=30s;
  server unix:/tmp/backend3;
  server backup1.example.com backup;
}
server address [parameters];
#配置一个后端web服务器,配置在upstream内,至少要有一个server服务器配置。
#server支持的parameters如下:
weight=number #设置权重,默认为1,实现类似于LVS中的WRR,WLC等
max_conns=number  #给当前后端server设置最大活动链接数,默认为0表示没有限制
max_fails=number  #后端服务器的下线条件,当客户端访问时,对本次调度选中的后端服务器连续进行检测多少次,如果都失败就标记为不可用,默认为1次,当客户端访问时,才会利用TCP触发对探测后端服务器健康性检查,而非周期性的探测
fail_timeout=time #后端服务器的上线条件,对已经检测到处于不可用的后端服务器,每隔此时间间隔再次进行检测是否恢复可用,如果发现可用,则将后端服务器参与调度,默认为10秒
backup  #设置为备份服务器,当所有后端服务器不可用时,才会启用此备用服务器
down    #标记为down状态,可以平滑下线后端服务器,新用户不再调度到此主机,旧用户不受影响
hash KEY [consistent];
#基于指定请求报文中首部字段或者URI等key做hash计算,使用consistent参数,将使用ketama一致性hash算法,适用于后端是Cache服务器(如varnish)时使用,consistent定义使用一致性hash运算,一致性hash基于取模运算
#示例
hash $request_uri consistent; #基于用户请求的uri做hash
hash $cookie_sessionid  #基于cookie中的sessionid这个key进行hash调度,实现会话绑定

 

ip_hash;
#源地址hash调度方法,基于的客户端的remote_addr(源地址IPv4的前24位或整个IPv6地址)做hash计算,以实现会话保持
#hash $remote_addr 则是对全部32bit的IPv4进行hash计算

 

least_conn;
#最少连接调度算法,优先将客户端请求调度到当前连接最少的后端服务器,相当于LVS中的WLC

添加节点后,会导致很多的调度的缓存信息失效

机器

curl10.0.0.19hosts
代理 10.0.0.8 nginx
后端 10.0.0.18 httpd
后端 10.0.0.28 httpd

 

10.0.0.8
[root@centous8 ~]# vim /apps/nginx/conf/nginx.conf
#gzip on;
upstream byfwebservers{
server 10.0.0.18 weight=3; #可以在此处加权重
server 10.0.0.28;
}

server {
listen 80;

# }
#}
include /apps/nginx/conf/conf.d/*.conf;
}
[root@centous8 ~]# vim /apps/nginx/conf/conf.d/pc.conf

server{
listen 80;
server_name www.byf.org;
root /data/nginx/html/pc;
location / {
proxy_pass http://byfwebservers;
}
[root@centous8 ~]# nginx -t
[root@centous8 ~]# systemctl -s nginx

10.0.0.18
[root@centous8 ~]# yum -y install httpd;systemctl enable --now httpd;hostname -I > /var/www/html/index.htm

10.0.0.28
[root@centous8 ~]# yum -y install httpd;systemctl enable --now httpd;hostname -I > /var/www/html/index.htm
[root@text ~]# curl www.byf.org
<h1>www.bai.org</h1>
[root@text ~]# curl www.byf.org
10.0.0.18
[root@text ~]# curl www.byf.org
10.0.0.28

根据hash值进行调度算法

权重也可以做hash

10.0.0.8
[root@centous8 ~]# vim /apps/nginx/conf/nginx.conf
#gzip on;
upstream byfwebservers{
hash $request_uri ;
server 10.0.0.18;
server 10.0.0.28;
}

server {
10.0.0.18
10.0.0.28
[root@centous8 ~]# for i in {1..10};do echo testpage$i on `hostname -I` > /var/www/html/test$i.html;done
[root@centous8 ~]# ls /var/www/html/
index.html test1.html test3.html test5.html test7.html test9.html
test10.html test2.html test4.html test6.html test8.html

10.0.0.19
[root@text ~]# curl www.byf.org/test4.html
testpage4 on 10.0.0.18
[root@text ~]# curl www.byf.org/test1.html
testpage1 on 10.0.0.28
[root@text ~]# curl www.byf.org/test5.html
testpage5 on 10.0.0.18

一致性hash算法

image-20211220202924076

0-2*32 -1=43亿

将用户访问的uri做hash运算,然后同过0-2*32 -1=43亿取模,结果为 2^32-1的其中一个,后端服务器四个节点同样的方法取模。然后在圆上找顺势针最近的服务器进行提供服务。

会出现hash环的偏斜,为解决这问题,将服务器权重乘以1000,换成1000节点解决为在request_uri后加consistent

第一步:密钥环0-2*32=42.97亿

第二步“主机hash计算

第三步:请求的hash计算

 

6.1.6调度算法有哪几种

轮询 rr

权重

IPhash键做hash

6.2 四层反向代理

10.0.0.19hosts,redis 
10.0.0.8 nginx  
10.0.0.18 redis  
10.0.0.28 redis  
10.0.0.8
[root@centous8 ~]# vim /apps/nginx/conf/nginx.conf
#}
include /apps/nginx/conf/conf.d/*.conf;
}


stream {
upstream redis {
server 10.0.0.18:6379;
server 10.0.0.28:6379;
}
server {
listen 6379;
proxy_pass redis;
}
}
10.0.0.18;10.0.0.28
[root@centous8 ~]# yum -y install redis;sed -i '/127.0.0.1/c bind 0.0.0.0' /etc/redis.conf
[root@centous8 ~]#systemctl enable --now redis
10.0.0.19
[root@text ~]# yum -y install redis
[root@text ~]# systemctl start redis
[root@text ~]# redis-cli -h 10.0.0.8 set name bai
[root@text ~]# redis-cli -h 10.0.0.8 get name
"bai"
[root@text ~]# redis-cli -h 10.0.0.8 get name
(nil)

LVS性能好,nginx看不出真实客户端

 

6.3实现FastCGI

LNMP架构

 

标签:10.0,http,nginx2,nginx,proxy,conf,root
来源: https://www.cnblogs.com/byf593/p/15781430.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有