  Let $S$ be a $p$-subgroup of the $\mathbb K$-automorphism group $Aut(\mathcal
X)$ of an algebraic curve $\mathcal X$ of genus $g\ge 2$ and $p$-rank $\gamma$
defined over an algebraically closed field $\mathbb{K}$ of characteristic
$p\geq 3$. Nakajima proved that if $\gamma \ge 2$ then $|S|\leq
\textstyle\frac{p}{p-2}(g-1)$. If equality holds, $\mathcal X$ is a Nakajima
extremal curve. We prove that if $$|S|>\textstyle\frac{p^2}{p^2-p-1}(g-1)$$
then one of the following cases occurs:
  (i) $\gamma=0$ and the extension $\mathbb K(\mathcal X)|\mathbb K(\mathcal
X)^S$ completely ramifies at a unique place, and does not ramify elsewhere.
  (ii) $|S|=p$, and $\mathcal X$ is an ordinary curve of genus $g=p-1$.
  (iii) $\mathcal X$ is an ordinary, Nakajima extremal curve, and $\mathbb
K(\mathcal X)$ is an unramified Galois extension of a function field of a curve
given in (ii). There are exactly $p-1$ such Galois extensions. Moreover, if
some of them is an abelian extension then $S$ has maximal nilpotency class.
  The full $\mathbb{K}$-automorphism group of any Nakajima extremal curve is
determined, and several infinite families of Nakajima extremal curves are
constructed by using their pro-$p$ fundamental groups.
