nginx--反向代理配置(主要配置server)

案例1

访问http://localhost:8090时,重定向到http://localhost:8080

    server {
        #监听本地8090端口
        listen       8090;
        server_name  localhost;

        location / {
		    proxy_pass http://localhost:8080/;
            root   html;
            index  index.html index.htm;
        }
        
        # 重定向服务错误页面到静态页面50x.html
		# redirect server error pages to the static page /50x.html
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

案例2

① 访问http://localhost:9001/dev时,重定向到http://localhost:8080
② 访问http://localhost:9001/dev/swagger-ui.html时,重定向到http://192.168.1.111:8081/swagger-ui.html
③ 访问http://localhost:9001/test/swagger-ui.html时,重定向到http://192.168.1.112:8081/swagger-ui.html

	server{
		#监听本地8090端口
	    listen       9001;
        server_name  localhost;
			
		#  =开头 表示精确匹配,只有完全匹配上才能生效
		location =/dev {
		    #被代理的url
			proxy_pass http://localhost:8080/;
		}
		
		# ^~开头 表示对URL路径进行前缀匹配,并且在正则之前
		location  ^~/dev {
			proxy_pass http://192.168.1.111:8081/;
		}
		location ^~/test {
			proxy_pass http://192.168.1.112:8081/;
		}
				
		#不带任何修饰符,也表示前缀匹配,但是在正则匹配之后
		location /pro{
			proxy_pass http://192.168.1.113:8081/;
		}
	}

特别注意

当访问http://192.168.1.111:8081/dev/index.html时

代理到URL:http://127.0.0.1:8082/index.html

location /dev/ {
#proxy_pass中最后以/结尾
proxy_pass http://127.0.0.1:8082/;
}

代理到URL:http://127.0.0.1:8082/dev/index.html

location /dev/ {
#proxy_pass中最后不是以/结尾
proxy_pass http://127.0.0.1:8082;
}

代理到URL:http://127.0.0.1:8082/aaa/index.html

location /dev/ {
proxy_pass http://127.0.0.1:8082/aaa/;
}

代理到URL:http://127.0.0.1:8082/aaaindex.html

location /dev/ {
proxy_pass http://127.0.0.1:8082/aaa;
}

在nginx中配置proxy_pass代理转发时,如果在proxy_pass后面的url加/,表示绝对根路径;如果没有/,表示相对路径,实际访问的路径中包含匹配的路径。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值