Nitrogen flooding can effectively develop lowpermeability and tight reservoirs. However, because of the low viscosity and density of nitrogen, gas channeling easily occurs, resulting in low oil recovery. Oil-based nitrogen foam can be used to control gas channeling during nitrogen flooding, but the foam stability directly affects the application effect. In this paper, the performance of the foaming agent was optimized by compounding, and a stable oil-based nitrogen foam was successfully built, with a foam volume exceeding 400 mL and a half-life of liquid drainage exceeding 40 min. The foam stability under an ultrahigh pressure of 60 MPa and an elevated temperature of 80 °C were investigated by an ultrahigh pressure and ultrahigh temperature PVT system for the first time. The influence of base oil viscosity and interfacial tension on foam stability was examined. The experimental results showed that the foam stability improved when the pressure increased and worsened when the temperature rose. The foam stability was the best at 20 °C and 60 MPa, and the half-life of liquid drainage was nearly 4 h. The foam stability was the worst at 80 °C and 0.1 MPa, and the half-life was <1 min. The higher was the base oil viscosity, the better was the foam stability. Although the rise of pressure and temperature reduces the nitrogen−diesel interfacial tension, the interfacial tension is not the dominant factor affecting foam stability at elevated temperatures. This paper has a certain guiding significance for perfecting the stability mechanism of oil-based nitrogen foam under ultrahigh pressure and elevated temperature and better controlling gas channeling during nitrogen flooding in low-permeability and tight reservoirs.