Nginx作为Nacos集群的代理的基本原理是将客户端的请求发送到Nginx服务器上,然后由Nginx负责将请求转发到后端的Nacos集群中的一个或多个节点上。
具体来说,原理如下:
1. 客户端发送请求:客户端通过指定的域名或IP地址发送请求到Nginx服务器。
2. Nginx接收请求: Nginx服务器接收到客户端的请求。
3. Nginx根据配置转发请求: 根据Nginx的配置文件,Nginx将收到的请求转发到Nacos集群中的一个或多个节点上。这通常通过`proxy_pass`指令实现,该指令将请求转发到指定的后端服务器地址。
4. Nacos集群处理请求:Nginx将请求转发给Nacos集群中的节点后,Nacos节点会处理该请求,执行相应的操作,如服务注册、服务发现、配置管理等。
5. Nacos节点返回响应:Nacos节点处理完请求后,将相应的结果返回给Nginx服务器。
6. Nginx返回响应给客户端: Nginx接收到来自Nacos节点的响应后,将响应返回给客户端。
通过这样的方式,Nginx作为代理,能够有效地分担Nacos集群的负载,提高系统的性能和稳定性。同时,Nginx还能提供负载均衡、反向代理、缓存、安全等功能,增强了系统的灵活性和可靠性。
下面是nginx的文件配置,在D:\Program Files\nginx-1.18.0\nginx-1.18.0\conf找到nginx.conf文件
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#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;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
upstream nacos-cluster {
server 192.168.0.4:8848;
server 192.168.0.4:8850;
server 192.168.0.4:8846;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
location /nacos {
proxy_pass http://nacos-cluster;
}
#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;
}
# 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://127.0.0.1:8080;
# 前端应用运行的地址和端口
# 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;
#}
# 后端应用的代理
location /api {
proxy_pass http://127.0.0.1:10086;
# 后端应用运行的地址和端口
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;
}
# MinIO 的代理
location /minio/ {
proxy_pass http://127.0.0.1:9000/;
# MinIO 运行的地址和端口
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;
# 以下用于解决 MinIO 的跨域问题
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain; charset=utf-8';
}
}
# 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;
# }
#}
}
然后分别配置集群