Jinceon
Jinceon

Reputation: 1342

how to merge nginx config block when only little difference between rules

location /a/ {
  ----same
  ----same
  ----same
  ----same
  ----same
  rewrite /a/(.*) /$1 break; 
}

location /b/ {
  ----same
  ----same
  ----same
  ----same
  ----same
  deny ip...; 
}

location /c/ {
  ----same
  ----same
  ----same
  ----same
  ----same
  proxy_pass http://.....; 
}

I have a lot of location rules in my nginx.conf file, they are very similar.

how can i optimize them, remove the duplicated block ?

Upvotes: 0

Views: 354

Answers (1)

Tarun Lalwani
Tarun Lalwani

Reputation: 146630

Create a file same.conf with below content

  ----same
  ----same
  ----same
  ----same
  ----same

Then change config as below

location /a/ {
  include <path to same.conf>;
  rewrite /a/(.*) /$1 break; 
}

location /b/ {
  include <path to same.conf>;
  deny ip...; 
}

location /c/ {
  include <path to same.conf>;
  proxy_pass http://.....; 
}

Upvotes: 1

Related Questions