I thought Tolkien wrote with WW2 in mind. Mostly Nazi influence.
He began thinking about and working on it long before World War II; recall that he actually fought in WWI, and some of the imagery is drawn from his recollections of the ruined fields of France. Frodo's reaction after his triumphant homecoming is also typical of what he saw veterans of WWI experience, as their sacrifice was not the physical loss of life, but an emotional loss--Frodo tells this to Sam at the end, when he is preparing to go to the West.
There is Communist as well as Nazi ideology represented in the books. Neither the Communists nor the Nazis suddenly rose in 1939, and anyone who was paying attention could see them coming a long way off. Some Catholics like Tolkien had their ears up about the coming horrors in the early 1920s. He stated that LOTR is an explicitly Catholic work.