美文网首页
Nacos 2.0 使用 Nginx 做代理

Nacos 2.0 使用 Nginx 做代理

作者: Rami | 来源:发表于2023-08-31 17:49 被阅读0次

    参考官网:https://nacos.io/zh-cn/docs/v2/upgrading/2.0.0-compatibility.html

    1. 部署nacos集群

    2. 安装nginx

    3.配置nginx

    3.1 需求

    要在Nginx中代理Nacos 2.0的HTTP和gRPC请求,你需要在Nginx配置文件中分别设置HTTP和stream块,并将它们配置为代理到相应的Nacos服务端口。

    3.2 配置如下

    第一个http的配置

    cd /etc/nginx/conf.d/ #一般都是这个路径
    vim dev_nacos.conf
    upstream nacos-http {
        server  ip:8848;  # Nacos HTTP服务的IP和端口
    }
    
    server {
        listen 80;
        server_name nacos.example.com;  # 将此域名替换为你的域名
    
        location / {
            proxy_pass http://nacos-http;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }
    

    第二个grpc的配置

    mkdir /etc/nginx/stream.d && cd  /etc/nginx/stream.d
    vim dev_nacos_grpc.conf
    upstream nacos-grpc {
        server ip:9848;  # Nacos gRPC服务的IP和端口
    }
    
    server {
        listen 1080; 这里的端口需要在80基础上加1000
        proxy_pass nacos-grpc;
    }
    

    在Nginx的主配置文件中包含单独的Nacos配置文件。找到Nginx的主配置文件(通常为nginx.conf),并在合适的位置添加include指令,引入刚刚创建的dev_nacos.conf、dev_nacos_grpc.conf两个文件。

    # nginx.conf
    user  nginx;
    worker_processes  auto;
    
    error_log  /var/log/nginx/error.log notice;
    pid        /var/run/nginx.pid;
    
    
    events {
        worker_connections  1024;
    }
    
    http {
        include /etc/nginx/conf.d/*.conf;  # 其他HTTP配置文件
        include /etc/nginx/sites-enabled/*;  # 如果有站点配置文件,也可以包含它们
    }
    
    stream {
        include /etc/nginx/stream.d/*.conf;  # 其他stream配置文件
    }
    
    

    相关文章

      网友评论

          本文标题:Nacos 2.0 使用 Nginx 做代理

          本文链接:https://www.haomeiwen.com/subject/zjuzmdtx.html