  The discussions on the connection between gravity and thermodynamics attract much attention recently. We consider a static self-gravitating perfect fluid system in $f(R)$ gravity, which is an important theory could explain the accelerated expansion of the universe. We first show that the Tolman-Oppenheimer-Volkoff equation of $f(R)$ theories can be obtained by thermodynamical method in spherical symmetric spacetime. Then we prove that the maximum entropy principle is also valid for $f(R)$ gravity in general static spacetimes beyond spherical symmetry. The result shows that if the constraint equation is satisfied and the temperature of fluid obeys Tolmans law, the extrema of total entropy implies other components of gravitational equations. Conversely, if $f(R)$ gravitational equation hold, the total entropy of the fluid should be extremum. Our work suggests a general and solid connection between $f(R)$ gravity and thermodynamics. 
General proof of the entropy principle for self-gravitating fluid in   f(R) Gravity