@@ -132,12 +132,13 @@ const internalNginx = {
132132 const renderEngine = utils . getRenderEngine ( ) ;
133133 let renderedLocations = "" ;
134134
135- for ( const location of host . locations ) {
135+ for ( const [ idx , location ] of ( host . locations || [ ] ) . entries ( ) ) {
136136 if ( location . npmplus_enabled === false ) {
137137 continue ;
138138 }
139139
140140 if (
141+ location . forward_host &&
141142 location . forward_host . indexOf ( "/" ) > - 1 &&
142143 ! location . forward_host . startsWith ( "/" ) &&
143144 ! location . forward_host . startsWith ( "unix" )
@@ -148,12 +149,75 @@ const internalNginx = {
148149 location . forward_path = `/${ split . join ( "/" ) } ` ;
149150 }
150151
152+ location . forward_upstream_name = `upstream_${ host . id } _location_${ idx } ` ;
151153 renderedLocations += await renderEngine . parseAndRender ( template , location ) ;
152154 }
153155
154156 return renderedLocations ;
155157 } ,
156158
159+ /**
160+ * Generates upstream blocks for main host and custom locations
161+ * @param {Object } host
162+ * @returns {Promise }
163+ */
164+ renderUpstreams : async ( host ) => {
165+ let template ;
166+
167+ try {
168+ template = await readFile ( `${ __dirname } /../templates/_upstream.conf` , {
169+ encoding : "utf8" ,
170+ } ) ;
171+ } catch ( err ) {
172+ throw new errs . ConfigurationError ( err . message ) ;
173+ }
174+
175+ const renderEngine = utils . getRenderEngine ( ) ;
176+ let renderedUpstreams = "" ;
177+
178+ if ( [ "http" , "https" , "grpc" , "grpcs" ] . includes ( host . forward_scheme ) ) {
179+ if (
180+ host . forward_host &&
181+ host . forward_host . indexOf ( "/" ) > - 1 &&
182+ ! host . forward_host . startsWith ( "/" ) &&
183+ ! host . forward_host . startsWith ( "unix" )
184+ ) {
185+ const split = host . forward_host . split ( "/" ) ;
186+ host . forward_host = split . shift ( ) ;
187+ host . forward_path = `/${ split . join ( "/" ) } ` ;
188+ }
189+
190+ host . forward_upstream_name = `upstream_${ host . id } ` ;
191+ renderedUpstreams += await renderEngine . parseAndRender ( template , host ) ;
192+ }
193+
194+ for ( const [ idx , location ] of ( host . locations || [ ] ) . entries ( ) ) {
195+ if ( location . npmplus_enabled === false ) {
196+ continue ;
197+ }
198+
199+ if ( ! [ "http" , "https" , "grpc" , "grpcs" ] . includes ( location . forward_scheme ) ) {
200+ continue ;
201+ }
202+
203+ if (
204+ location . forward_host &&
205+ location . forward_host . indexOf ( "/" ) > - 1 &&
206+ ! location . forward_host . startsWith ( "/" ) &&
207+ ! location . forward_host . startsWith ( "unix" )
208+ ) {
209+ const split = location . forward_host . split ( "/" ) ;
210+ location . forward_host = split . shift ( ) ;
211+ location . forward_path = `/${ split . join ( "/" ) } ` ;
212+ }
213+
214+ location . forward_upstream_name = `upstream_${ host . id } _location_${ idx } ` ;
215+ renderedUpstreams += await renderEngine . parseAndRender ( template , location ) ;
216+ }
217+
218+ return renderedUpstreams ;
219+ } ,
220+
157221 /**
158222 * @param {String } host_type
159223 * @param {Object } host
@@ -175,14 +239,25 @@ const internalNginx = {
175239 throw new errs . ConfigurationError ( err . message ) ;
176240 }
177241
178- // For redirection hosts, if the scheme is not http or https, set it to $scheme
242+ host . env = process . env ;
243+
179244 if (
180- nice_host_type === "redirection_host" &&
181- [ "http" , "https" ] . indexOf ( host . forward_scheme . toLowerCase ( ) ) === - 1
245+ host . forward_host &&
246+ host . forward_host . indexOf ( "/" ) > - 1 &&
247+ ! host . forward_host . startsWith ( "/" ) &&
248+ ! host . forward_host . startsWith ( "unix" )
182249 ) {
183- host . forward_scheme = "$scheme" ;
250+ const split = host . forward_host . split ( "/" ) ;
251+ host . forward_host = split . shift ( ) ;
252+ host . forward_path = `/${ split . join ( "/" ) } ` ;
184253 }
185254
255+ if ( host . domain_names ) {
256+ host . server_names = host . domain_names . map ( ( domain_name ) => domainToASCII ( domain_name ) || domain_name ) ;
257+ }
258+
259+ host . upstreams = await internalNginx . renderUpstreams ( host ) ;
260+
186261 if ( host . locations ) {
187262 _ . map ( host . locations , ( location ) => {
188263 if ( location . npmplus_auth_request === "anubis" ) {
@@ -208,32 +283,14 @@ const internalNginx = {
208283 host . locations = await internalNginx . renderLocations ( host ) ;
209284 }
210285
211- if (
212- host . forward_host &&
213- host . forward_host . indexOf ( "/" ) > - 1 &&
214- ! host . forward_host . startsWith ( "/" ) &&
215- ! host . forward_host . startsWith ( "unix" )
216- ) {
217- const split = host . forward_host . split ( "/" ) ;
218-
219- host . forward_host = split . shift ( ) ;
220- host . forward_path = `/${ split . join ( "/" ) } ` ;
221- }
222-
223- if ( host . domain_names ) {
224- host . server_names = host . domain_names . map ( ( domain_name ) => domainToASCII ( domain_name ) || domain_name ) ;
225- }
226-
227- host . env = process . env ;
228-
229286 try {
230287 const config_text = await renderEngine . parseAndRender ( template , host ) ;
231288
232289 await writeFile ( filename , config_text , { encoding : "utf8" } ) ;
233290 debug ( logger , "Wrote config:" , filename ) ;
234291
235292 if ( process . env . DISABLE_NGINX_BEAUTIFIER === "false" ) {
236- await utils . execFile ( "nginxbeautifier" , [ "-s" , "4 " , filename ] ) . catch ( ( ) => { } ) ;
293+ await utils . execFile ( "nginxbeautifier" , [ "-s" , "2 " , filename ] ) . catch ( ( ) => { } ) ;
237294 }
238295
239296 return true ;
0 commit comments