It took me about 5 years, from 23 to 28 to identify everything. It was a lot of "I got a headache, what did I eat, what were the ingredients", then correlating that over a long period, then testing what ingredient I isolated. For sugar and caffeine it was also a conscious choice to try cutting them out, I think those were more withdrawal situations than the other ones.
An example, I was eating at a gluten free restaurant regularly. No gluten means I'm good, yea? Still getting sinus headaches though. Maybe I'm going crazy, so I ask them what they think. He points out the sodium nitrite in the pepperoni. Correlated that with bacon, other deli meats, "uncured" things that have celery powder instead (contains sodium nitrite!). Then there is the sauce, tomato has glutamate, also tomato sauce often has sugar, these were a different time scale of headache though.
Cutting out sugar was by far the hardest. I went back and forth on it many times. I eventually got sick of the full withdrawal when cutting it out fully and decided to not start again.
Most people asking about this have digestion issues. These are usually caused by FODMAPs [1]. I had digestion issues with gluten and high fructose foods (fructose malabsorption). My brother is lactose intolerant. If that's what you're looking to isolate I would start with those.
Good luck! I highly recommend giving it a shot. Changing my diet changed my life. It's not true for everyone but all of my health issues were coming from what I was eating.