首頁 > 軟體

Nginx實現Nacos反向代理的專案實踐

2022-03-25 13:01:13

1.win10安裝Nginx

nginx下載地址

nginx: download

下載後解壓,進入bin目錄,根據你的系統執行相應的命令

1.1 windows系統啟動和停止的命令

啟動

start nginx.exe

終止

nginx.exe -s stop //停止nginx

nginx.exe -s reload //重新載入nginx

nginx.exe -s quit //退出nginx

2.win10安裝nacos

nacos官網網址

Nacos 快速開始

2.1 搭建三臺nacos步驟

1.複製三份解壓後的nacos檔案包分別命名如下

  • nacos8848
  • nacos8849
  • nacos8850

 2.以nacos8848為例,進入該目錄,進入conf目錄修改application.properties檔案,使用外接資料來源

### Default web server port:
server.port=8848
 
#*************** Network Related Configurations ***************#
### If prefer hostname over ip for Nacos server addresses in cluster.conf:
# nacos.inetutils.prefer-hostname-over-ip=false
 
### Specify local server's IP:
# nacos.inetutils.ip-address=
#*************** Config Module Related Configurations ***************#
### If use MySQL as datasource:
spring.datasource.platform=mysql
### Count of DB:
db.num=1
### Connect URL of DB:
db.url.0=jdbc:mysql://127.0.0.1:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC
db.user.0=root
db.password.0=root

3.將conf/cluster.conf.example改為cluster.conf,新增節點設定

#2022-03-23T10:56:12.825
localhost:8849
localhost:8850

4.另外幾臺也照這個設定修改,注意埠號的修改

建立mysql資料庫,sql檔案位置:confnacos­mysql.sql

5.分別啟動三臺nacos,啟動命令為進入到bin目錄,cmd執行startup.cmd

startup.cmd

6.設定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 nacoscluster {
		server localhost:8848;
		server localhost:8849;
		server localhost:8850;
	}
	
	   server {
        listen       8847;
        server_name  localhost;
		
		
		location /nacos/ {
            proxy_pass http://nacoscluster/nacos/;
        }
 
        location = /50x.html {
            root   html;
        }
        error_page   500 502 503 504  /50x.html;
    }
	
 
    server {
        listen       80;
        server_name  localhost;
 
        location / {
            root   html;
            index  index.html index.htm;
        }
 
 
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
 
 
}

7.執行nginx

start nginx.exe

我們監聽的是8847埠,所以我們登入nacos直接使用nginx進行代理

http://localhost:8847/nacos

我們可以看到當你重新整理的時候,分配到的是不同的伺服器上

 到此這篇關於Nginx實現Nacos反向代理的專案實踐的文章就介紹到這了,更多相關Nginx Nacos反向代理內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com