Nginx实现统一端口反向代理多个服务

  最近在云服务搭建java项目环境,用nginx部署一些自己开发的东西,在实现80端口代理前端页面后试着想不再对外开通额外端口给到通过云服务访问另一个项目。

网上对于同一端口代理多个服务简单明了,但是自己通过配置后存在许多误区做以记录。

  default.conf(nginx配置文件之一,或者其他配置文件中对应server项如下)

    server{

      listen  80;

      server_name  testdemo;

      #access_log  /var/log/nginx/host.access.log  main;

      location /  {

        root  /usr/share/nginx/html;

        index  index.html  index.htm;

      }#这项location配置是转发到nginx自带的首页,我们做代理需要新增一项location配置如下

      

      location /cn  {#根据访问nginx同一端口下的指定路径做转发代理,我们这里是‘/cn’

        proxy_pass  http://localhost:8080;

        #也就是如果nginx访问路径是‘http://www.baidu.com/’到nginx首页面,那么访问这个为‘http://www.baidu.com/cn’;

        #而且遇到后端服务问题是,需要设置contextPath进行路径映射,这里nginx会通过该配置请求到后端‘http://localhost:8080/cn’,如果后端

        #服务没有‘/cn’这个映射路径是无正常代理成功!

      }

    }

posted @ 2022-02-26 22:31  luoheyuan  阅读(3515)  评论(0编辑  收藏  举报