B-cell Non-Hodgkin lymphomas are the malignancies of lymphocytes. CD20 is a membrane protein, which is highly expressed on the cell surface of the B-cells in NHL. Treatments using monoclonal antibodies (mAbs) have resulted in failure in some cases. Nanobodies (NBs), single-domain antibodies with low molecular weights and a high specificity in antigen recognition, could be practical alternatives for traditional mAbs with superior characteristics. To design an optimized NB as a candidate CD20 inhibitor with raised binding affinity to CD20, the structure of anti-CD20 NB was optimized to selectively target CD20. The 3D structure of the NB was constructed based on the optimal templates (6C5W and 5JQH), and the key residues were determined by applying a molecular docking study. After identifying the key residues, some mutations were introduced using a rational protocol to improve the binding affinity of the NB to CD20. The rational mutations were conducted using the experimental design (Taguchi method). Six residues (Ser27, Thr28, Phe29, Ile31, Asp99, and Asn100) were selected as the key residues, and five residues were targeted for rational mutation (Trp, Phe, His, Asp, and Tyr). Based on the mutations suggested by the experimental design, two optimized NB structures were constructed. NB2 showed a remarkable binding affinity to CD20 in docking studies with a binding energy of − 853 kcal/mol. The optimized NB was further evaluated using molecular dynamics simulation. The results revealed that CDR1 (complementarity determining regions1) and CDR3 are essential loops for recognizing the antigen. NB2 could be considered as a potential inhibitor of CD20, though experimental evaluations are needed to confirm it.