nginx配置微服务的网关和负载均衡

利用Nginx配置上游服务器

配置位置:[http块]配置upstream:

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;
}

拿出http块代码:

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;

######配置聚焦看这里
##上游服务器组 整个组起名字 servername
upstream servername{

##每个服务器试用server标签 写上服务地址
##微服务网关gateway的访问 这里不要写http 直接ip:port
server 192.168.31.173:88;

}

include /etc/nginx/conf.d/*.conf;
}

修改server块反向配置(负载均衡):

server块代码:

server {
listen 80;
server_name gulimall.com;

#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;

#######修改聚焦 看这里
location / {
###上游服务器组 整个组起名字 servername
proxy_pass http://servername;
}

#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;
#}
}


与server对应的gateway网关的配置路由配置文件application.yml:

    ## 增加gateway的路由规则  **.xxx.com 任意子域名定向访问值nacos的nacosServerName服务
- id: host_route
uri: lb://nacosServerName
predicates:
- Host=**.xxx.com,xxx.com

断言规则:复杂的靠前写,简单的往后写。放置覆盖

坑点:接口通讯 而页面没有成功。

至此api接口可以通讯!但是页面不能正常访问。

[server块] 配置 nginx代理转发至网关的时候会丢失请求的host信息(域名) proxy_set_header Host $host 。而gateway的断言规则

predicates:

-Host=**.xxx.com,xxx.com

是基于域名,导致gateway网关路由分发失败。api的断言规则正常。所以,api接口正常,页面木有打开。

中心作用点:gateway的路由断言规则

修改补充server的location反向代理配置:

    #######修改聚焦 看这里
location / {
#######方向代理转发网关时补充头信息
###补充转发是header的host信息 后续缺少什么这里补充
proxy_set_header Host $host;

###上游服务器组 整个组起名字 servername
proxy_pass http://servername;
}

持此配置完成!

修改后的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;

######配置聚焦看这里
##上游服务器组 整个组起名字 servername
upstream servername{

##每个服务器试用server标签 写上服务地址
##微服务网关gateway的访问
server 192.168.31.173:88;

}

include /etc/nginx/conf.d/*.conf;
}

修改后的server.conf

server {
listen 80;
server_name xxx.com;

#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;

#######修改聚焦 看这里
location / {
#######方向代理转发网关时补充头信息
###补充转发是header的host信息 后续缺少什么这里补充
proxy_set_header Host $host

##上游服务器组 整个组起名字 servername
proxy_pass http://servername;
}

#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;
#}
}

修改后的gateway断言规则application.yml

spring:
cloud:
gateway:
globalcors:
corsConfigurations:
'[/**]':
allowedOrigins: "*"
routes:
##接口的断言规则
- id: product_route
uri: lb://nacosServerName
predicates:
- Path=/api/product/**
filters:
- RewritePath=/api/(?<segment>.*),/$\{segment}

##页面的断言规则
## 增加gateway的路由规则 **.xxx.com 任意子域名定向访问值nacos的nacosServerName服务
- id: gulimall_host_route
uri: lb://nacosServerName
predicates:
- Host=**.xxx.com,xxx.com