Skip to content

Commit

Permalink
Fix server names search for block
Browse files Browse the repository at this point in the history
  • Loading branch information
r2dtools committed Sep 27, 2024
1 parent 86445e4 commit 1be36bc
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
9 changes: 6 additions & 3 deletions config/serverblock.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,18 @@ type ServerBlock struct {

func (s *ServerBlock) GetServerNames() []string {
serverNames := []string{}

directives := s.FindDirectives("server_name")

if len(directives) == 0 {
return serverNames
}

for _, value := range directives[0].GetValues() {
serverNames = append(serverNames, strings.TrimSpace(value))
for _, directive := range directives {
serverNames = append(serverNames, directive.GetValues()...)
}

for index, serverName := range serverNames {
serverNames[index] = strings.TrimSpace(serverName)
}

return serverNames
Expand Down
2 changes: 1 addition & 1 deletion config/serverblock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestServerBlock(t *testing.T) {

block := serverBlocks[0]
assert.Equal(t, "server", block.GetName())
assert.ElementsMatch(t, block.GetServerNames(), []string{"example2.com", "www.example2.com"})
assert.ElementsMatch(t, block.GetServerNames(), []string{"example2.com", "www.example2.com", "alias.example2.com"})
assert.Equal(t, true, block.HasSSL())
assert.Equal(t, "/var/www/html", block.GetDocumentRoot())

Expand Down
1 change: 1 addition & 0 deletions test/nginx/sites-enabled/example2.com.conf
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ server { # inline comment
index index.html index.htm index.nginx-debian.html;

server_name example2.com www.example2.com;
server_name alias.example2.com;

location / {
# First attempt to serve request as file, then
Expand Down

0 comments on commit 1be36bc

Please sign in to comment.