The list of embassies of the spanish government states for Azerbaijan, that the competent Spanish embassy is the Turkish one
But, the Schengen Visa Code hanbook on 1.6 says :
Should a Member State consulate accept an application from an applicant
travelling to a Member State that is not present or represented in the third country
where the applicant resides?
Article 5(4) entails an obligation for Member States to cooperate in order to prevent such
situations of Member States not being present or represented and thus, this obligation is an
obligation of means, not an obligation of result. Therefore, Member States are not obliged to
accept visa applications that they are not competent to examine and take decisions on
according to the rules set out above where the competent Member State is not present or
represented.
However, considering that this provision is contained in the article concerning the ”Member
State competent for examining and deciding on an application”, a Member State may, in the
absence of the normally competent Member State, agree to examine such applications in
individual, exceptional circumstances and take a decision on it
– for reasons of justified urgency, and
– after having obtained the agreement of the normally responsible Member State.
This means that unless Spain has an agreement with another Schengen member (which is likely) or it has justified urgency, you can't apply for it in Azerbaijan.
I would suggest you to contact the Turkish embassy and ask for the guidance on how to apply from Azerbaijan