Docker Nginx 反向代理

今天药忘吃喽~ 2024-03-25 08:58 125阅读 0赞

最近在系统性梳理网关的知识,其中网关的的功能有一个是代理,正好咱们常用的Nginx也具备次功能,今天正好使用Nginx实现一下反向代理,与后面网关的代理做一个对比,因为我使用的docker安装的Nginx,与直接部署Nginx不太一样正好记录下遇到的问题,希望可以帮助到学习的同学。废话不多说直接上案例。

环境准备:mac、docker、spring-boot(两个微服务)

第一步:启动Nginx容器

  1. docker ps -a
  2. docker start 容器ID

27b0d5b6b8112065d9c2edee34065dee.png

第二步:进入容器修改Nginx配置

  1. docekr exec -it 容器ID /bin/bash

e29f5a408831bf65b21cc9f2abc1faf3.png

第三步:找到Nginx配置

  1. cd /etc/nginx/

35831c1faec319fbf67eccf547cc97eb.png

不能使用 vim 命令编辑该文件,因为docker里的镜像容器特别小,没有该命令,咱们只能先查看一下该文件。cat 之后注意看我用绿框框起来的部分,nginx.conf文件里使用的是/etc/nginx/conf.d/*.conf下的配置文件,那咱们在去修改/etc/nginx/conf.d/*.conf下的配置。我们会发现就一个default.conf配置,不用怀疑就是它了。

09116b769603dd990926c6e68eca6c3d.png

  1. cd /etc/nginx/conf.d/

056eb06fd7fb6483fd3395cf21db2eda.png

初始的文件是这样的,监听的80端口,location 指向的是nginx默认的index.html页面。

bf48e81e91861bd70a6535c30c75709a.png

347d017ebb9b0165cdbf52344f1cf563.png

第四步:修改配置

怎么修改配置呢,前面咱们提到的是容器里去少vim工具无法直接编辑,当然有三种方式我大概提一下,第一种是安装相关工具,第二方式是copy到咱们自己的本地修改完以后在copy 到容器中重启,第三种是将容器的挂在切换到本地,那么就可以直接在本地修改。当然最好的方式是第三种,我们本次先使用第二种方式修改配置。

4.1:将容器的配置文件copy到本地并修改

  1. sudo docker cp 34fb22321ad3:/etc/nginx/conf.d/default.conf /Users/liluyang/mydocker

在本地就可以看到容器上copy下来的文件了,咱们动手改改它。要实现反向代理也跟简单就是修改nginx的server模块。我直接贴代码:路由demo1服务,路由demo2服务是咱们本次修改的重点。

ead939bd845e218690f3aa50df0b8216.png

  1. server {
  2. listen 80;
  3. listen [::]:80;
  4. server_name localhost;
  5. #access_log /var/log/nginx/host.access.log main;
  6. #location / {
  7. # root /usr/share/nginx/html;
  8. # index index.html index.htm;
  9. #}
  10. # 路由demo1服务
  11. location /demo1 {
  12. proxy_pass http://10.33.148.22:8081;
  13. }
  14. # 路由demo2服务
  15. location /demo2 {
  16. proxy_pass http://localhost:8082;
  17. }
  18. #error_page 404 /404.html;
  19. # redirect server error pages to the static page /50x.html
  20. #
  21. error_page 500 502 503 504 /50x.html;
  22. location = /50x.html {
  23. root /usr/share/nginx/html;
  24. }
  25. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  26. #
  27. #location ~ \.php$ {
  28. # proxy_pass http://127.0.0.1;
  29. #}
  30. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  31. #
  32. #location ~ \.php$ {
  33. # root html;
  34. # fastcgi_pass 127.0.0.1:9000;
  35. # fastcgi_index index.php;
  36. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  37. # include fastcgi_params;
  38. #}
  39. # deny access to .htaccess files, if Apache's document root
  40. # concurs with nginx's one
  41. #
  42. #location ~ /\.ht {
  43. # deny all;
  44. #}
  45. }

4.2:将修改好的配置文件copy到容器中

  1. sudo docker cp /Users/liluyang/mydocker/default.conf 容器ID:/etc/nginx/conf.d/

4.3:重启容器并进入容器查看是否是咱们最新的文件

  1. docker restart 容器ID
  2. docker ps
  3. docker exec -it 7a616a5079de /bin/bash
  4. cd /etc/nginx/conf.d
  5. cat default.conf

下图就是最新生效的配置文件,我用红色框框起来的是重点,因为容器中访问localhost的时候其实是访问的容器中的localhost,docker容器中的localhost:8081,localhost:8082肯定是没有对应的服务的,后面在实际看一下是对应的现象。

64f9cf2e1c8d4198229bcb02710a1739.png

第五步:启动微服务

启动要反向代理的微服务,我是在本地启动了两个微服务:demo1,demo2,端口分别为8081,8082。

  1. @GetMapping("demo1/api/demo1/start")
  2. public String demo1() {
  3. String str = "demo1 start ...";
  4. log.info(str);
  5. System.out.println(str);
  6. return str;
  7. }
  8. @GetMapping("demo2/api/demo2/start")
  9. public String demo1() {
  10. String str = "demo2 start ...";
  11. log.info(str);
  12. System.out.println(str);
  13. return str;
  14. }

d52197eab4d1f5ad1f790d8250024297.png

启动咱们的服务之后如果没有反向代理的话需要怎么访问咱们的服务呢,可以在浏览器中输入

http://localhost:8081/demo1/api/demo1/start

30be46e2346c21ed99ebd0a9ff8f792a.png

http://localhost:8082/demo2/api/demo2/start

c1bd9e16510883c29342ab8c02036102.png

第六步:验证Nginx反响代理

我们想要使用反向代理的话就可通过一个统一的域名端口来访问咱们不同应用了。可以看到我通过

http://localhost:8088/demo1/api/demo1/start可以访问到服务demo1,并且Nginx的日志也是没有问题。

c1864e50c5ea0beb86dc1c9d0c607793.png

f4746344bcaa86fe24f10f9ac9c901cc.png

但是如果我想反问demo2呢,前面咱们看到demo2在Nginx里的配置是有些许区别的,来来实验一下看看效果。http://localhost:8088/demo2/api/demo2/start,可以看到容器中的日志是链接refused。至于为啥前面应说了要区分容器和本地的localhost的区别。咱们Nginx是采用的docker容器部署,微服务是本地不熟的。

96c76951e225d293d1c8cd4c20f67100.png

25eb3f07c3705ac6dd10525adc538e0a.png

那再次修改一下配置看看效果,完美解决。

  1. # 路由demo1服务
  2. location /demo1 {
  3. proxy_pass http://10.33.148.22:8081;
  4. }
  5. # 路由demo2服务
  6. location /demo2 {
  7. proxy_pass http://10.33.148.22:8082;
  8. }

efacfc42d049877214e9881b13e8c605.png

发表评论

表情:
评论列表 (有 0 条评论,125人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Docker Nginx 反向代理

    最近在系统性梳理网关的知识,其中网关的的功能有一个是代理,正好咱们常用的Nginx也具备次功能,今天正好使用Nginx实现一下反向代理,与后面网关的代理做一个对比,因为我使用的

    相关 Nginx反向代理

    什么是反向代理? 通常的代理服务器,只用于代理内部网络对Internet的连接请求,客户机必须指定代理服务器,并将本来要直接发送到Web服务器上的http请求发送到代理服

    相关 nginx 反向代理

    1、反向代理     nginx反向代理的指令不需要新增额外的模块,默认自带proxy\_pass指令,只需要修改配置文件就可以实现反向代理。     配置前的准备工作