There are several ways to do it.
1:
ssh user@remote_server 'bash -s' < localfile 2:
cat localfile | ssh user@remote_server 3:
ssh user@remote_server "$(< localfile)" number 3 is my prefered way, it allows interactive commands e.g. susudo -S service nginx restart
(#1 and #2 will consume the rest of the script as input for the password question when you use susudo -S.)