nginx如何配置代理转发

Nginx是个厉害的服务器,可以配置多个服务器,一个server就是一个服务器



server {

      listen       80;
      server_name  *.yourdomain.com;
   ....

}

代理转发是在server下面的location进行配置



server {

   // 服务器配置

   location  / {

   // ...... 代理配置

   }

}

常见的Nginx代理配置




upstream my_server {                                                        

    server 10.0.0.2:8080;                                               

    keepalive 2000;

}

server {

    listen       80;                                                        

    server_name  10.0.0.1;                                              

    client_max_body_size 1024M;

 

    location /my/ {

        proxy_pass http://my_server/;

        proxy_set_header Host $host:$server_port;

    }

}

通过该配置,访问nginx地址http://10.0.0.1:80/my的请求会被转发到my_server服务地址http://10.0.0.2:8080/

需要注意的是,如果按照如下配置:




upstream my_server {                                                        

    server 10.0.0.2:8080;                                               

    keepalive 2000;

}

server {

    listen       80;                                                        

    server_name  10.0.0.1;                                              

    client_max_body_size 1024M;

 

    location /my/ {

        proxy_pass http://my_server;

        proxy_set_header Host $host:$server_port;

    }

}

那么,访问nginx地址http://10.0.0.1:80/my的请求会被转发到my_server服务地址http://10.0.0.2:8080/my。这是因为proxy_pass参数中如果不包含url的路径,则会将location的pattern识别的路径作为绝对路径。





posted @ 2020-09-29 19:20  牧之丨  阅读(13688)  评论(0编辑  收藏  举报