Hardy wrote Under the Greenwood Tree in 1871 when he was living at Weymouth and Higher Bockhampton. He set the story in Mellstock, a village he imagined based on real places near his home.