@@ -186,16 +186,28 @@ func NewApp(params []*param.Param) *App {
186186 }
187187 }
188188
189- // handler for each listen item
189+ // init default vhost for each listen item
190190 for _ , l := range app .listens {
191- item := l
191+ for _ , vh := range l .vhosts {
192+ if len (vh .Hostnames ) == 0 {
193+ l .defaultVHost = vh
194+ break
195+ }
196+ }
197+
198+ if l .defaultVHost == nil {
199+ l .defaultVHost = l .vhosts [0 ]
200+ }
201+ }
192202
193- if len (item .vhosts ) == 1 {
194- item .handler = item .vhosts [0 ].Mux
203+ // handler for each listen item
204+ for _ , l := range app .listens {
205+ if len (l .vhosts ) == 1 {
206+ l .handler = l .defaultVHost .Mux
195207 continue
196208 }
197209
198- item .handler = http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
210+ l .handler = http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
199211 var serveVHost * vhost.VHost
200212
201213 hostname := r .Host
@@ -204,14 +216,15 @@ func NewApp(params []*param.Param) *App {
204216 hostname = hostname [:colonIndex ]
205217 }
206218
207- for _ , vh := range item .vhosts {
219+ for _ , vh := range l .vhosts {
208220 if vh .MatchHostname (hostname ) {
209221 serveVHost = vh
210222 break
211223 }
212224 }
225+
213226 if serveVHost == nil {
214- serveVHost = item . vhosts [ 0 ]
227+ serveVHost = l . defaultVHost
215228 }
216229
217230 serveVHost .Mux .ServeHTTP (w , r )
0 commit comments