Home > Enterprise >  How can I move a validation into a common file and import it?
How can I move a validation into a common file and import it?

Time:10-18

I have multiple virtual hosts, managed by Nginx.

I received a ticket which asks me to validate the value of a request header for some virtual hosts and refuse serving requests if the header is absent or invalid. This is what I have added to each of the virtual hosts:

location / {
    if ($http_my_request_header_name != "<mytoken>") {
        return 406;
    }
}

And this is working as expected. However, I intend to make this solution more elegant and create a new config file, called common.conf, and, instead of pasting the same code into all virtual hosts, include this common file, as:

include conf.d/common.conf;

Yet, my tries were all failing:

First attempt

Pasting the if conditional and leave the location directive in the virtual host, which would include the file. The error I received was telling me in the logs that I cannot have an if by itself in the file.

Second attempt

I have moved the whole

location / {
    if ($http_my_request_header_name != "<mytoken>") {
        return 406;
    }
}

into the common file and replaced it with the include at the virtual host configs. The error I received was telling me that I need to wrap a server directive around the location directive.

Third attempt

I have wrapped a server directive around my location directive, but that errored out as well. I no longer have the error, because I destroyed the server since then and recreated it. But if the exact error message is needed, let me know, I will reproduce it and share it here then.

Question

How can I create a common config file that would validate for a request header and include it into multiple virtual host configs?

EDIT

In view of Rahul Sharma's answer, I'm providing further information of the current try. This is how the main conf looks alike currently:

server {
...
    include conf.d/common.conf;
...
}

and the include is a direct child of the server directive, that is, nothing else is wrapped around the include. Earlier, instead of the include` line, the main conf had its content, like

server {
...
    location / {
        if ($http_my_request_header_name != "<mytoken>") {
            return 406;
        }
    }
...
}

and it worked. I have literally moved the location / to the common.conf file and replaced it with the include. So, Nginx dislikes my other file for some reason. This is why it was strange to me and this is what led me to ask this question.

CodePudding user response:

I assume the contents of your conf.d/common.conf file is:

location / {
    if ($http_my_request_header_name != "<mytoken>") {
        return 406;
    }
}

The error

"location" directive is not allowed here in /etc/nginx/conf.d/common.conf

is because probably your include statement inside the main nginx.conf is misplaced. The line include conf.d/common.conf; should be inside a server block for this to work like this:

server {
    ...
    ...
    include conf.d/common.conf;
    ...
    ...
}

CodePudding user response:

It turned out that in nginx.conf there was a line of

include /etc/nginx/conf.d/*.conf;

which included common.conf at another place that I was not aware of and in that second place the code inside common.conf was invalid.

  • Related