We study the van der Waals interaction of a metallic or narrow-gap semiconducting nanowire with a surface, in the regime of intermediate wire-surface distances $(v_{F}/c)L ll d ll L $ or $L ll d ll (c/v_{F})L $, where $L$ is the nanowire length, $d$ is the distance to the surface, and $v_{F}$ is the characteristic velocity of nanowire electrons (for a metallic wire, it is the Fermi velocity). Our approach, based on the Luttinger liquid framework, allows one to analyze the dependence of the interaction on the interplay between the nanowire length, wire-surface distance, and characteristic length scales related to the spectral gap and temperature. We show that this interplay leads to nontrivial modifications of the power law that governs van der Waals forces, in particular to a non-monotonic dependence of the power law exponent on the wire-surface separation.