4-1.3、Nginx如何转发http请求?

时间:2025-03-19 21:31:55

在开发中,nginx服务器往往需要处理来自于多个不同服务器的请求,我们有没有多余的服务器来部署应用,因此就需要用到nginx来转发不同服务器的请求。

原路径: /callback/test/test?username=xx

转发到:http://10.1.9.1:8088/callback/test/test?username=xx

配置如下:

server {

        listen      80;

        server_name ;

      # 匹配callback

        location /callback/ {

            proxy_pass http://10.1.9.1:8088;

        }

# 默认其他

        location / {

          proxy_pass http://10.2.2.1:8088;

          # root  html;

          # index   ;

        }

}

注意:在配置中http://10.1.9.1:8088,后面不用加任何目录,/callback/test/test,这一串都不需要加,/ 符号也不需要,

这是因为proxy_pass参数中如果不包含url的路径,则会将location的pattern识别的路径作为绝对路径。

重启:

nginx -s reload