nginx。服务器指令继承。

Nik*_*nyh 11 configuration nginx

有没有办法减少建设,如:

server { 
  server_name regular_site; 
  location /api/ { 
     proxy_pass 127.0.0.1:5000;
  } 
  location / { 
     proxy_pass 127.0.0.1:3000;
  } 
} 

server { 
  server_name mobile_site; 
  location /api/ { 
     proxy_pass 127.0.0.1:5000;
  } 
  location / { 
     proxy_pass 127.0.0.1:3001;
  } 
} 
Run Code Online (Sandbox Code Playgroud)

server api { 
  location /api/ { 
     proxy_pass 127.0.0.1:5000;
  } 
}

server extends api { 
  server_name regular_site;
  location / { 
     proxy_pass 127.0.0.1:3000;
  } 
} 

server extends api { 
  server_name mobile_site;
  location / { 
     proxy_pass 127.0.0.1:3001;
  } 
} 
Run Code Online (Sandbox Code Playgroud)

欢迎任何其他关于摆脱 api 部分的建议。

Ben*_*ani 14

你可以很容易地用一个include语句来做到这一点。

/etc/nginx/conf/api_defaults.conf

location /api/ { 
  proxy_pass 127.0.0.1:5000;
}    
Run Code Online (Sandbox Code Playgroud)

然后在你的主vhost配置中。

/etc/nginx/sites-enabled/my_new_api.conf

server my_new_api {  
  server_name mobile_site;

  include "/etc/nginx/conf/api_defaults.conf";

  location / { 
    proxy_pass 127.0.0.1:3001;
  }         
}
Run Code Online (Sandbox Code Playgroud)