How should I insulate and block moisture inside rim joists where foamboard doens't fit?
Closed cell foam is an insulator and vapor barrier in one. 

You can buy a kit at the  blue box store that contains the two parts
that get mixed together with the enclosed spray tip. 

I have used this one  (Available at the blue box store ). but this one looks good also. 

You could do all of your gaps and hard to reach places.