In all criminal cases, either before or after conviction, and in cases of contempt, if it is shown to the court, judge or committing magistrate that the jail of the proper county is insecure or insufficient for the safekeeping of the prisoner or that there is no jail in the county, the commitment must be to the nearest sufficient jail and the reason of such change must be entered on the minutes of the court, or stated in the warrant or endorsed thereon and signed by the magistrate. The jailer of the county to which the commitment is made must receive and confine the prisoner on such commitment or a certified copy of such order.