同一个Nginx服务器同一端口配置多个代理服务 朴灿烈づ我的快乐病毒、 2022-03-11 01:29 666阅读 0赞 # 一、建立两个服务 # ## 1.1 建立产品服务(product\_server) ## 1. 配置文件 server: port: 8080 servlet: context-path: /product 1. 创建访问接口 @SpringBootApplication @RestController public class ProductApplication { public static void main(String[] args) { SpringApplication.run(ProductApplication.class, args); } @GetMapping("/add") public String addProduct() { return "添加产品成功"; } @GetMapping("/update") public String updateProduct() { return "编辑产品成功"; } } 1. 启动,访问:[http://localhost:8080/product/add][http_localhost_8080_product_add] ![在这里插入图片描述][20190306220152482.png] 2. 访问:[http://localhost:8081/order/update][http_localhost_8081_order_update] ![在这里插入图片描述][20190306220217713.png] ## 1.2 创建订单服务(order\_server) ## 1. 配置文件 server: port: 8081 servlet: context-path: /order 1. 创建访问接口 @SpringBootApplication @RestController public class OrderApplication { public static void main(String[] args) { SpringApplication.run(OrderApplication.class, args); } @GetMapping("/add") public String addOrder() { return "添加订单成功"; } @GetMapping("/update") public String updateOrder() { return "编辑订单成功"; } } 1. 同上进行访问,肯定没问题。 ## 二、修改hosts文件 ## 添加以下三个映射 127.0.0.1 www.product.com 127.0.0.1 www.order.com 127.0.0.1 nginx.test.com ## 三、修改nginx配置文件 ## 1. 更换配置文件内容 #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 { 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"'; rewrite_log on; #设置访问的web应用列表 upstream product_server{ server www.product.com:8080; } upstream order_server{ server www.order.com:8081; } #HTTP服务器 server { #监听80端口,80端口是知名端口号,用于HTTP协议 listen 80; #定义使用www.xx.com访问 server_name nginx.test.com; #编码格式 charset utf-8; #代理配置参数 proxy_connect_timeout 180; proxy_send_timeout 180; proxy_read_timeout 180; proxy_set_header Host $host; proxy_set_header X-Forwarder-For $remote_addr; #默认指向product的server location / { proxy_pass http://product_server; } #使用location对不同请求做相应处理 location /product/{ proxy_pass http://product_server; } location /order/ { proxy_pass http://order_server; } } } 1. 在nginx根目录下输入命令:**nginx -tc conf/nginx.conf**测试配置文件,成功。 ![在这里插入图片描述][20190306220840120.png] 2. 输入命令:**start nginx**启动nginx。 # 四、测试 # ![\*\*加粗样式\*\*][20190306221014626.png] ![在这里插入图片描述][20190306221029139.png] ![在这里插入图片描述][20190306221042778.png] ![在这里插入图片描述][20190306221053838.png] **至此,我们便实现了使用nginx服务的80端口反向代理产品服务和订单服务** [http_localhost_8080_product_add]: http://localhost:8080/product/add [20190306220152482.png]: /images/20220311/173ccb35ef33491c9d3085568a540cd4.png [http_localhost_8081_order_update]: http://localhost:8081/order/update [20190306220217713.png]: /images/20220311/f06e41665e304cf0881bc116d9f387d1.png [20190306220840120.png]: /images/20220311/ebf75cb7bf8f47e5be8a8154329960bf.png [20190306221014626.png]: /images/20220311/9954a78589dc4f52a5fbace693d69764.png [20190306221029139.png]: /images/20220311/d75516892ea34bf4815d67f7638575cf.png [20190306221042778.png]: /images/20220311/a8b0857d9e8b4e88aa21649ae394c498.png [20190306221053838.png]: /images/20220311/5a39e353a91c408ea0246c1d64261cb1.png
还没有评论,来说两句吧...