nginx代理本机多个端口应用

本文描述了如何在只有一个80端口授权的情况下,通过Nginx服务器配置实现不同服务(如8081、8082)的反向代理,确保所有请求根据根路径转发到对应的服务端口。
摘要由CSDN通过智能技术生成
说明:

需求:我当前只有一个端口的授权,比如80,但是我的服务有多个且在不同的端口,比如8081,8082,我希望所有的请求通过都通过80端口进来,然后根据不同的文根怼到不同的端口(或者说是服务)上去

应用带根路径的情况
  1. 应用1请求为:http://localhost:8091/app
  2. 应用2请求为:http://localhost:8092/app
    在这里插入图片描述

在这里插入图片描述

配置如下时:
通过8000端口做服务端的反向代理
当访问:http://localhost:8000/app1 相当于请求 http://localhost:8091/app
当访问:http://localhost:8000/app2 相当于请求 http://localhost:8092/app
在这里插入图片描述

将根路径直接去掉,此时为 /

即:

  1. 应用1请求为:http://localhost:8091/
  2. 应用2请求为:http://localhost:8092/
    在这里插入图片描述

【注意采坑】:最后的 / 不能少,否则会出现404
在这里插入图片描述
当访问:http://localhost:8000/app1 相当于请求 http://localhost:8091/
当访问:http://localhost:8000/app2 相当于请求 http://localhost:8092/

server {
        listen       8000;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        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;
        }

        # 为第一个应用配置路径
		location /app1 {
			proxy_pass http://localhost:8091/; # 转发到本机的8091端口
			proxy_set_header Host $host;
			proxy_set_header X-Real-IP $remote_addr;
			proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
			proxy_set_header X-Forwarded-Proto $scheme;
		}

		# 为第二个应用配置路径
		location /app2 {
			proxy_pass http://localhost:8092/; # 转发到本机的8092端口
			proxy_set_header Host $host;
			proxy_set_header X-Real-IP $remote_addr;
			proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
			proxy_set_header X-Forwarded-Proto $scheme;
		}
    }
  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值