After completing a thorough education in literature, art, and military training, Nagai entered the ranks of the Tokugawa bureaucracy. He served from 1851 to 1852 as an instructor at the
Kitenkan, a branch of the Shogunate's
Shoheizaka academy, located in
Kōfu,
Kai Province. Shortly after the arrival of the
Perry Expedition challenging Japan
national isolation policy, Nagai was appointed as a
metsuke and was placed in charge of casting new cannons for coastal defense. In 1855, Nagai was transferred to the newly formed
Nagasaki Naval Training Center, where he served as its director, overseeing a group of Dutch military advisors and students from various domains around Japan in studies of western warship technologies.
Katsu Kaishū later credited Nagai for much of the training center's progress, as well as the construction of the Nagasaki Iron Works, and one of its training ships, the
Kottoru. In 1857, with the opening of the
Tsukiji Naval Training Center, Nagai returned to
Edo on board the
Kankō Maru, Japan's first steam warship, together with 103 of his students. In December 1857, Nagai was appointed
Kanjō-bugyō. In July 1858, Nagai (along with
Iwase Tadanari) was appointed one of the first
Gaikoku bugyō (Commissioner for Foreign Affairs). He served from August 1858 through March 1859., during which time the difficult negotiations for the
Ansei Treaties took place with the United States, United Kingdom, Russia and France. In February 1859, he added
Gunkan-bugyō to his list of titles. However, in August, following the death of Shōgun
Tokugawa Iesada, Nagai was
purged from office by the
Tairō Ii Naosuke for his support of the
Hitotsubashi faction over the
Kishu faction for the shogunal succession. After
Ii's assassination, Nagai was recalled to public office and was appointed to serve as one of the city magistrates of
Kyoto (
Kyoto Higashimachi-bugyō) from August 1862 through March 1864. This was a difficult and violent period in Kyoto, with many daimyo establishing residences in Kyoto and maneuvering for political connections with various of the nobility. The city was filled with numerous
rōnin, many of whom supported the
sonnō jōi movement and who did not hesitate to use assassination or terrorist tactics to further their political agenda. During his tenure, the
Shinsengumi became active in Kyoto. Following his two-year stint in the capital, he was made
ōmetsuke from March 1864 through May 1865. He served again as
Gaikoku bugyō from November 1865 through April 1867. Nagai was promoted to the position of
wakadoshiyori-kaku (aide to the junior counselors) from April 1867 through January 1868 and was in Kyoto on November 9, 1867 when Shōgun
Tokugawa Yoshinobu formally surrendered his office; thereafter, Nagai served as one of the
wakadoshiyori from January to March 1868 when the
Meiji Restoration signaled the end of the Tokugawa shogunate. ==Under the Ezo Republic==