Not that simple. There are tons of reasons for being nice to others even if they are not nice to you. If that mess up with your head or not it is another issue. And if that's how you found to not mess up with your head or not it is another different issue.
You can be nice to people that wasn't nice to you because you know people sometimes have bad day, or got treated badly their whole life, or you want them to like you, or you want to be the better person, or you want to spread the world what you want the world to spread to you(basically kindness generates kindness and violence generates violence).
From where am from, that attitude will completely ruin your life.
people are born having no ill intent towards others, the ill intent comes with time, experience and morality.
saying Someone might have a rough day hence his/her “not nice” attitude towards you is a whole other concept, if I was a good guy who had a bad day and ended up bad Mouthing someone for no reason, the very next day I would probably go apologize and say I didn’t mean it, if I was a rude dude and had a bad day (or even on good days) and started verbally shitting on someone, even if they replied nicely to me, I wouldn’t go apologizing the next day cuz I’m a bad MF who is used to insulting people, it’s “normal” for me.
From personal experience, countering the insult game will always yield better results than simply replying nicely, both for yourself and the other guy. He gets to stop and realize how nuts he is behaving, and then both of you could talk it out and maybe even socialize if you feel like it, and you get to handle every situation how it should handled.
had a lot of people mistaking my kindness for weakness, which evolved for even regular dudes trying to harass me so they could try and join the “club of the strongs“
as I said maybe it’s something domestic and not worldwide but no one will ever have a happy life here acting nice in opposition of “not nice”