Placement of web proxy servers is an important avenue to save network bandwidth, alleviate server load, and reduce latency experienced by users. The general problem of web proxy placement is to compute the optimal locations for placing k web proxies in a network such that the objective concerned is minimized or maximized. In this paper, we address this problem for tree networks and propose a novel mathematical model for it. In our model, we consider maximizing the overall access gain as our objective and formulate this problem as an optimization problem. The optimal placement is obtained using a computationally efficient dynamic programming-based algorithm. Applying our mathematical model, we also present a solution to web proxy placement for autonomous systems (ASes), as a natural extension of the solution for tree networks. Our algorithms have been implemented. The simulation results show that our model significantly outperforms the random placement model.