1

I have two sites that I need to serve: a static one and a dynamic one (Rails app, doesn't really matter actually). I'm thinking about the following:

requests to domain.com/ => Static website

requests to domain.com/anything => Dynamic website

Nginx's location directive seems perfect for this situation:

server { listen 80; server_name www.domain.com *.domain.com; # Static match. Any exact match enters here location = / { root /link/to/static/folder; index index.html; } # Dynamic match location / { root /link/to/dynamic/folder; proxy_pass unix_socket_defined_above; } } 

But whenever I make a request to domain.com it gets directed to the dynamic match. What am I missing?

Edit: Although it is not optimal, I'm achieving the desired functionality with the following declarations:

server { listen 80; server_name domain.com .domain.com; # Static match. Any exact match enters here location = / { root /link/to/static/folder; index index.html; } # Dynamic match, make sure the URL has # characters after the server name location ~ ^/..* { root /link/to/dynamic/folder; proxy_pass unix_socket_defined_above; } } 

but I'm sure there's a "right way" to accomplish this.

2
  • 2
    Are you sure you restarted nginx? Cleared your browser cache? Commented Mar 25, 2015 at 16:34
  • Yes, full restart (sudo service nginx restart), testing with curl (doesn't have a cache, right?) and a browser with private window. Commented Mar 25, 2015 at 17:21

1 Answer 1

4

Your server_name directive doesn't match domain.com.

The *.domain.com form is redundant with www.domain.com and doesn't match domain.com. Use server_name .domain.com instead.

As a consequence, if you have an explicit default server block or a server block handling requests for other domains included before this one, then your requests are processed in it.

Now if that's not the case, then it does indeed process domain.com/ and domain.com/anything requests, implicitely being your default server block. In this case, the index.html file is served by the second location block since the index directive will issue an internal redirect.

So you need to change this :

location = / { root /link/to/static/folder; index index.html; } 

To this :

location ~ /(?:index.html)?$ { root /link/to/static/folder; index index.html; } 
2
  • Changed to server_name .domain.com;, I don't have a default server, /etc/nginx$ grep -nrI default . returns nothing. No luck so far. Commented Mar 25, 2015 at 17:37
  • @jlhonora See the updated answer. Commented Mar 25, 2015 at 18:14

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.