提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
提示:这里可以添加本文要记录的大概内容:
解决我windows服务器上Nginx代理和Nacos的问题,我的目的是用Nginx来代理我的若依微服务项目,但是若依微服务是需要靠nacos来管理服务的,所以我就在想是不是可以用nginx来反向代理nacos集群,再由nacos集群来管理各服务。查找资料和实现过程我就不叙述了,直接展示实现流程。此文章针对windows系统,windows系统,windows系统!!!!(这是我第一次写博客,在服务器方面我也是刚接触,只为记录自己从零实现的喜悦,有什么地方不对可以指正,但勿喷)
提示:以下是本篇文章正文内容,下面案例可供参考
一、实现流程
1.安装环境
如果光是实现上述的功能的话是不需要安装其他环境只需要
mysql>5.7
nacos 2.0.3
nginx 1.2.0
2.安装mysql
参考此文章https://blog.csdn.net/weixin_44051608/article/details/85163823
3.安装nginx
参考此文章
https://dujian.blog.csdn.net/article/details/105380882
4.安装nacos
参考此文章
https://blog.csdn.net/qq_42583263/article/details/123323615?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522165292892216782246472481%2522%252C%2522scm%2522%253A%252220140713.130102334…%2522%257D&request_id=165292892216782246472481&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2blogsobaiduend~default-1-123323615-null-null.nonecase&utm_term=%E5%AE%89%E8%A3%85nacos&spm=1018.2226.3001.4450
5.针对上述的修改
修改nacos中的 application.properties
。配置nacos的端口为8848端口,服务器地址为xx.xx.xx.xx(自己的服务器地址),这样在你的服务器上就可以对外访问到nacos集群。运行的时候因为没有配置多服务器一定要使用startup.cmd -m standalone命令,外网访问xx.xx.xx.xx:8848
erver.servlet.contextPath=
### 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=xx.xx.xx.xx(自己的服务器地址)
#*************** Config Module Related Configurations ***************#
### If use MySQL as datasource:
#spring.datasource.platform=mysql
### Count of DB:
#db.num=1
### Connect URL of DB:
spring.datasource.platform=mysql
db.num=1
db.url.0=jdbc:mysql://xx.xx.xx.xx(自己的服务器地址):3306/自己的数据库名称?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC
#账号密码不要忘记修改!!!!!!!!!
db.user=root
db.password=root
修改nginx中的nginx.conf文件。upstream nacos里添加nginx代理的ip端口,我这边写的是nacos的路径端口,这样nginx的80端口就能代理访问到我的nacos端口,(猜想)我这样做的方法肯定是不对的,因为正式使用肯定是要访问项目的,所以应该是要写自己的项目路径,这样就能代理访问到自己的项目,但是我还没有做到那一步所以只能先摸爬滚打到这一步。在location /中添加 proxy_pass http://nacos;,这里就是代理访问的完整路径,nacos代表的是upstream nacos这个nacos里的路径,这说明upstream +名字只是个辅助语法,为了减少复制粘贴能够全局插入的配置。所以也可以不写upstream语法,直接在 proxy_pass里写完整的访问路径。
#gzip on;
upstream nacos {
server xx.xx.xx.xx(自己的服务器地址):8848;
}
server {
listen 80;
server_name xx.xx.xx.xx(自己的服务器地址);
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://nacos;
}
#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;
}
}
总结
最后结果就是能够通过外网访问xx.xx.xx.xx:80访问到nacos的页面