我有两个站点,我需要服务:一个静态的和dynamic的(Rails应用程序,实际上并不重要)。 我正在考虑以下内容:
请求到domain.com/ =>静态网站
请求domain.com/anything =>dynamic网站
对于这种情况, Nginx的位置指令看起来非常完美:
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; } }
但是每当我向domain.com发出请求时,它都会被引导到dynamic匹配。 我错过了什么?
编辑:虽然它不是最佳的,我正在实现所需的function与以下声明:
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; } }
但我确信有一个“正确的方法”来完成这个任务。
您的server_name指令与domain.com不匹配。
*.domain.com表单与www.domain.com是多余的,与domain.com不匹配。 改用server_name .domain.com 。
因此,如果您有一个明确的默认服务器块或服务器块处理其他域之前包含的其他域的请求,那么您的请求将在其中进行处理。
现在,如果情况并非如此,那么确实会处理domain.com/和domain.com/anything请求,隐式地成为您的默认服务器块。 在这种情况下, index.html文件由第二个位置块提供服务,因为index指令将发出内部redirect 。
所以你需要改变这个:
location = / { root /link/to/static/folder; index index.html; }
对此:
location ~ /(?:index.html)?$ { root /link/to/static/folder; index index.html; }