pkaramol
pkaramol

Reputation: 19352

Share directives between location blocks in nginx

I have an nginx location block that should be delegated to uwsgi backend and provide http --> https redirection, so it goes as follows:

location  ~ ^/(api/v1) {
    if ($http_x_forwarded_proto != 'https') {
        rewrite ^ https://$host$request_uri? permanent;
    }
     access_log my-access.log;
     error_log my-error.log;
     include uwsgi_params;
     uwsgi_read_timeout 300;
     uwsgi_send_timeout 300;
     uwsgi_param ..;
     uwsgi_param ..;
     etc ...
    }

What I want, is for a specific endpoint not to provide https redirection, so I am forced to do the following:

location  = /api/v1/my/more/specific/endpoint {
     access_log my-access.log;
     error_log my-error.log;
     include uwsgi_params;
     uwsgi_read_timeout 300;
     uwsgi_send_timeout 300;
     uwsgi_param ..;
     uwsgi_param ..;
     etc ...
    }

}

Is there a way nginx allows to avoid the above duplication of uwsgi parameters definition?

Upvotes: 3

Views: 1717

Answers (1)

Oleg Kuralenko
Oleg Kuralenko

Reputation: 11553

2 options:

  1. Move the params that have server context and must affect all locations our of the location block to the server block
  2. Use include to share common code to avoid copy & paste

Includes another file, or files matching the specified mask, into configuration. Included files should consist of syntactically correct directives and blocks.

Upvotes: 4

Related Questions