There's a difference between a consonant letter and a consonant sound.
In "university", although we usually count "u" as a vowel letter, the sound the words starts with is actually a "y" sound -- we say "you-niversity".
In "X-ray", although "x" is a consonant letter, the sound the words starts with is actually a vowel -- we say "eks-ray".
The a/an rule is about pronunciation, not spelling.
- For Teachers