To use natural gas as a feedstock alternative to coal and oil, its main constituent, methane, needs to be isolated with high purity1. In particular, nitrogen dilutes the heating value of natural gas and is, therefore, of prime importance for removal2. However, the inertness of nitrogen and its similarities to methane in terms of kinetic size, polarizability and boiling point pose particular challenges for the development of energy-efficient nitrogen-removing processes3. Here we report a mixed-linker metal-organic framework (MOF) membrane based on fumarate (fum) and mesaconate (mes) linkers, Zr-fum67-mes33-fcu-MOF, with a pore aperture shape specific for effective nitrogen removal from natural gas. The deliberate introduction of asymmetry in the parent trefoil-shaped pore aperture induces a shape irregularity, blocking the transport of tetrahedral methane while allowing linear nitrogen to permeate. Zr-fum67-mes33-fcu-MOF membranes exhibit record-high nitrogen/methane selectivity and nitrogen permeance under practical pressures up to 50 bar, removing both carbon dioxide and nitrogen from natural gas. Techno-economic analysis shows that our membranes offer thepotential to reduce methane purification costs by about 66% for nitrogen rejection and about 73% for simultaneous removal of carbon dioxide and nitrogen, relative to cryogenic distillation and amine-based carbon dioxide capture.