Nginx Reverse Proxy Microservice
一、本地域名服务访问过程

二、正向代理和反向代理

三、Nginx+Windows搭建域名访问环境
1、修改C:\Windows\System32\drivers\etc下hosts文件,映射本地域名和192.168.56.10ip关系。
每次通过记事本修改很麻烦,可以通过SwitchHosts软件修改。
192.168.56.10 family.com
2、现在就可以通过family.com域名访问了。
想要访问Deployment在Server上的其它端口应用也可以通过Domain访问,比如:搭建的kibana是5601端口,通过Domainhttp://family.com:5601/即可访问了(kibana时Deployment在56.10Server上的)

四、nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
Nginx ConfigurationFile说明:
1、全局块:
Configuration影响nginx全局的指令。如:用户组、nginx进程pid存放Path,Logging存放Path,Configuration File引入,允许生成wordker process数等。
2、events块:
Configuration影响nginxServer或用户的网络链接。如:每个进程的最大连接数,选取那种Event Driven模型处理链接请求,是否允许同时接收多个网络链接,开启多个网络链接序列化等。
3、http块:
可以嵌套多个server,ConfigurationProxy,Cache,Logging定义等绝大多数功能和第三方模块的Configuration。如File引入,mime-type定义,是否Usagesendfile传输File,链接超时事件,单链接请求数等。
http块里面又包含:
**3.1、http全局块:**如upstream,Error页面,连接超时等。
3.2、server块: Configuration虚拟注解的相关Parameter,一个http中可以有多个wever。
server块里面又包含:
3.2.1、location:Configuration请求的路由,以及各种页面的处理情况。
注意: server块里面可以包含多个location。
4、include /etc/nginx/conf.d/*.conf;
注意:会包含所有include /etc/nginx/conf.d/这个Directory下的所有Configuration File。
比如/etc/nginx/conf.d/默认有个default.confConfiguration File,里面就只包含了server块。
server {
listen 80; -----------nginx监听的端口
server_name localhost; ------------nginx监听的端口
#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main; ----访问日志
location / { ---------------- /表示上面监听域名下所有的请求,都可以在root目录下找,包括首页index
root /usr/share/nginx/html
index index.html index.htm;
}
#error_page 404 /404.html; ----------404错误页面路径配置
# redirect server error pages to the static page /50x.html
# error_page 500 502 503 504 /50x.html; ---------500页面路径配置
location = /50x.html {
root /usr/share/nginx/html;
}
五、反向代理配置
ConfigurationExample:
因为上面修改了hostFile映射了Domain到192.168.56.10Server,所有通过Service上nginx访问本地127.0.0.1的springboot项目就可以通过ConfigurationReverse ProxyDomain访问。
将通过nginx访问family.com的所有请求,通过nginxReverse Proxy到本地的springboot项目地址本地springboot项目ip192.168.56.1:。
server {
listen 80;
server_name family.com;
#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;
location / {
proxy_pass http://192.168.56.1:40000;
}
#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 /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
# #location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# 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;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
# #location ~ /\.ht {
# deny all;
#}
}
梳理流程:
1、修改本地hostFile,目的是模拟类似访问外网的一个Domain,如果有ServerDomain绑定了ip,就可以不用这步骤了。
2、修改host映射family.com ------->192.168.56.10 (Service地址)。
3、访问family.com就相当于访问192.168.56.10 (Service地址)。
4、将所有family.com(这个Domain默认端口为80)的所有请求,通过nginxReverse Proxy到局域网内的其它Server,比如这里就是本地搭建的springboot项目端口40000。
5、通过Reverse ProxyConfiguration
listen 80; ------监控哪个端口
server_name family.com; ------监听哪个域名
location / { ---- /:表示所有请求
proxy_pass http://192.168.56.1:40000; --将family.com域名80端,反向代理到http://192.168.56.1:40000
}
6、现在family.com:80端口的所有请求就会Reverse Proxy到http://192.168.56.1:40000了。

Reverse Proxy后访问地址:

七、nginx代理网关
如果我们Service多了,每次都要修改Domain,修改nginxConfigurationReverse Proxy到多个Service,这样比较麻烦。
那我们就可以通过ConfigurationnginxProxy到Microservices的Gateway,让Gateway来Load Balancing到各Service。nginxLoad Balancing参考地址
nginxReverse Proxy局域网内GatewayService(192.168.56.1:88)。
nginxConfiguration如下:
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
upstream family {
server 192.168.56.1:88;
}
include /etc/nginx/conf.d/*.conf;
}
注意修改的地方:
upstream family { ----网关取的名字
server 192.168.56.1:88; -----网关服务地址
server srv2.example.com; ----可以配置多个网关地址,我这只有一个网关配一个即可
server srv3.example.com;
server srv4.example.com;
}
修改yanxizhu.nginxConfiguration:
server {
listen 80;
server_name family.com;
#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;
location / {
proxy_pass http://family;
}
#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 /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
# #location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# 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;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
# #location ~ /\.ht {
# deny all;
#}
}
注意修改点:
location / {
proxy_pass http://family; ---这里就反向代理到family这个网关了,也就是上面取的网关名字
}
接着就开始ConfigurationgetwayGatewayService请求:
注意:nginxProxy给Gateway的时候会Losthost信息。
解决办法;yanxizhu.conf添加Proxyhost的header。
proxy_set_header Host $proxy_host;
family.conf整体如下:
server {
listen 80;
server_name family.com;
#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;
location / {
proxy_set_header Host $proxy_host;
proxy_pass http://family;
}
#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 /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
# #location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# 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;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
# #location ~ /\.ht {
# deny all;
#}
}
getwayGatewayServiceLoad BalancingConfiguration:
- id: family_host_route
uri: lb://family-booking
predicates:
- Host=**.family.com
比如访问http://family.com/时,nginx就会将请求Reverse Proxy给GatewayService(192.168.56.1:88),而GatewayService通过Configuration的Load Balancing,又会将**.family.comDomain的请求,Load Balancing到family-bookingMicroservices的80端口。
以上就是nginx的Reverse ProxyConfiguration。注意将getwayGateway的Configuration,放在最后,不然全部Service都被Proxy到family_host_route了。
八、静态资源配置
将前端静态资源上传到nginx静态资源Directory,后端主页面Path加上staticPath。
server {
listen 80;
server_name family.com;
#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;
location /static/ {
root /var/www/html;
}
location / {
proxy_set_header Host $proxy_host;
proxy_pass http://family;
}
#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 /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
# #location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# 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;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
# #location ~ /\.ht {
# deny all;
#}
}
说明:
location /static/ {
root /var/www/html;
}
后端主页访问静态资源都添加上/static/静态资源Directory,当请求static资源Path时,会被nginx处理请求nginx下Configuration的静态资源。