Nginx Mac端安装
可以参考文章http://t.csdnimg.cn/i9Vb6
使用homebrew安装的话可以直接用brew install nginx
来下载
Nginx的启动和关闭
直接使用nginx
命令来启动的话要关闭就是用sudo nginx -s stop
如果使用brew services start nginx
命令来启动的话,就要用brew services stop nginx
来关闭,这两个不是同一个,要注意!
本机的安装位置为/opt/homebrew/Cellar/nginx/1.27.0
Docroot位置为 /opt/homebrew/var/www
/usr/local/var/wwww文件夹位置指向的就是本机的安装位置为/opt/homebrew/Cellar/nginx/1.27.0/html文件夹位置
nginx的nginx.conf文件在/opt/homebrew/etc/nginx里面
nginx.conf配置文件的说明
#user nobody;
# 启动进程,通常设置成和cpu的数量相等
worker_processes 1;
# 全局错误日志
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
# PID文件
#pid logs/nginx.pid;
# 工作模式及连接数上限
events {
# 支持最大连接数
worker_connections 1024;
}
http {
#文件扩展名与文件类型映射表
include mime.types;
#默认文件类型
default_type application/octet-stream;
# 日志格式
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
# 指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件
sendfile on;
#防止网络阻塞
#tcp_nopush on;
# 连接超时时间
#keepalive_timeout 0;
keepalive_timeout 65;
# 开启zip压缩
#gzip on;
# 虚拟主机配置,一个http中可以包含多个server
server {
# 监听端口
listen 8080;
# 请求地址(可以是域名或IP地址),多个地址之间用空格隔开
server_name localhost;
# 编码, 如果网页格式与当前配置的不同的话将会被自动转码
#charset koi8-r;
#虚拟主机访问日志定义
#access_log logs/host.access.log main;
# location对指定路径进行拦截处理(默认路径)
location / {
# 首页应用路径
root html;
# 首页对应root下的哪个文件
index index.html index.htm;
}
# 错误信息页面
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# 代理配置例子:访问URL以.php结尾则自动转交给127.0.0.1
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# 转发(类似代理Proxy): php脚本请求全部代理给FastCGI处理
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# 配置禁止访问 .htxxx文件
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
include servers/*;
}
基本概念说明
Gateway网关和Nginx网关的联系与不同
Gateway是对内的网关,是前端工程和后端工程的网关
Nginx是对外网关,是用户和前端工程的网关
相同点:
- 实际使用中两者都可以实现api接口的拦截、负载均衡和反向代理以及请求过滤
不同点:
- Nginx使用C语言编写,要实现功能的拓展需要使用lua脚本语言来实现,而Gateway使用Java语言来编写,可以很方便地进行功能的拓展
- Nginx实现负载均衡是在服务端实现的,也就是说Nginx是把请求转移到多个内网关服务器实例进行负载均衡,而Gateway是使用本地负载均衡
使用情景
大流量情景一般使用的Gateway来实现,对于中小型的话可以用Zuul,但是使用Nginx会更合适一些,因为Nginx在各个方面都比Zuul要好
功能
反向代理
Nginx可以实现反向代理,所谓的反向代理就是本身客户端是不知道要访问哪一个资源(服务器的地址端口),实行反向代理的服务器就可以帮助用户选择一个合适的服务器来获取资源然后返回资源给用户(只对外暴露了实现反向代理的服务器的地址,但是资源所在的目标服务器的地址是确隐藏的
正向代理
正向代理是指客户端知道要访问哪一个资源,但是有一个代理服务器来帮忙实现这个请求而去目标服务器获取资源然后把资源返回给用户,但这个时候目标服务器不知道谁才是真正的请求获取资源的对象,这相当于隐藏了客户端的(IP)信息
Nginx和Gateway(多Gateway节点)的内外网关结合
思路
本技术应用于内网关的集群环境
- 单一内网关如果出现故障会导致内部微服务之间的api调用无法利用内网关来动态/静态路由,那么就会导致微服务之间的请求失败,从而导致后端服务失败,
- 另外,如果全部内部微服务之间的api调用都使用单一内网关来实现动态/静态路由,在高并发情境下会导致压力过大而导致的网关服务器崩溃
- 使用集群能实现内网关的高可用(单一节点故障不会导致整个服务器集群失败)、分散内网关的压力,集群方案还可以实现横向拓展和伸缩性(可以根据需求自动伸缩调整节点的使用,提高资源的利用率)还能保证安全性,分散安全风险
使用Nginx实现反向代理和负载均衡,把用户的请求转发到各个Gateway节点,保证内网关层的负载均衡
之后内分配到的Gateway节点就执行他的application.yml里面的routes路由设置,把请求再次转发到对应的微服务
所以可以看得出两次网关里面需要两次路由转发,这样可以显著减小单一内网关的节点压力
架构图(帮助理解)
具体实现逻辑
创建多个内网关Gateway节点
修改他们的端口,要保证不同,但他们的其他内容可以一样,可以直接复制然后修改application.yml里面的server.port
配置Nginx的配置文件nginx.conf
要在这里面添加好每一个Gateway节点的url,设置里面的监听端口和路由转发的逻辑,要把所有的内网关Gateway节点的ip地址统一起来放在一起
[!CAUTION]
注意这里面的监听端口listen代表外网关Nginx的入口端口,server_name代表Nginx所在的服务器ip地址
location后面、{}之前的位置代表进入的请求要匹配的正则表达式,使用 / 代表匹配所有请求
location的{}大括号里面需要设置四个属性(一定要有
proxy_set_header Host $host;
否则会报400):proxy_pass http://backend_servers; proxy_set_header Host $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_pass http://backend_servers;
proxy_pass
指令将所有匹配到的请求代理到upstream
块中定义的backend_servers
,即三个Gateway内网关的服务器ip地址(9000, 9001, 9002 端口)。Nginx 将根据负载均衡策略将请求分配到这些服务器上。
proxy_set_header Host $host;
这行配置将原始请求的
Host
头传递给后端服务器。$host
是一个 Nginx 变量,表示客户端请求的主机名。
proxy_set_header X-Real-IP $remote_addr;
这行配置将客户端的真实 IP 地址传递给后端服务器。
$remote_addr
是客户端的 IP 地址。
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
这行配置添加或更新
X-Forwarded-For
头,包含了原始客户端的 IP 地址,以及通过的代理服务器的地址列表。$proxy_add_x_forwarded_for
包含了当前客户端 IP 地址及之前的X-Forwarded-For
头的内容。
proxy_set_header X-Forwarded-Proto $scheme;
这行配置将客户端请求的协议(HTTP 或 HTTPS)传递给后端服务器。
$scheme
表示请求使用的协议。
在http的{}里面任意位置添加以下的内容(http里面可以有多个server)
upstream backend_servers {
server 127.0.0.1:9000;
server 127.0.0.1:9001;
server 127.0.0.1:9002;
}
server {
listen 9999;
server_name 127.0.0.1;
location / {
proxy_pass http://backend_servers;
proxy_set_header Host $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;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
使用监听的端口调用api
http://localhost:9999/user-all/customer-login?username=czp&password=123