Nginx配置反向代理


Reverse Proxy

所谓Reverse Proxy是指对内部服务的代理,client的请求先到达Nginx,Nginx会根据配置文件来分发请求到内部的server上

client              server
                
    | domain           |
    | ------------>  nginx                     
    |                  | ----> index.html
    | domain/path1     |
    | ------------>    | -----> localhost: 9999 (nodejs server)   
    |                  |
    | domain/path2     |
    | ------------>    | -----> localhost: 8888 (python server)

例如,配置文件如下

events{}
http{
    server{
        listen 8888;
        location /php{
            proxy_pass 'http://localhost:9999'
        }
        location /nginxorg{
            proxy_pass 'https://nginx.org'
        }
    }
}

另一个使用Ngix做反向代理的好处是,可以在request的header中插入字段传递给内部的server,也可以在response的header中插入字段,传递给client

server{
    listen 8888;
    location /php{
        #set header on proxy request
        proxy_set_header proxied nginx;
        proxy_pass 'http://localhost:9999'
    }
    location /nginxorg{
        proxy_pass 'https://nginx.org'
    }
}

Resources