Nginx = / layout pattern not working
I am trying to set up nginx to serve a static html page on the root domain and a proxy for uwsgi. As a quick test, I tried to redirect two different static pages:
server {
server_name *.example.dev;
index index.html index.htm;
listen 80;
charset utf-8;
location = / {
root /www/src/;
}
location / {
root /www/test/;
}
}
It seems that http://nginx.org/en/docs/http/ngx_http_core_module.html#location says you can do this. But I am always sent to the test site, even on request /
, by visiting http://www.example.dev
in my browser.
Curl output:
$ curl http://www.example.dev -v
* Rebuilt URL to: http://www.example.dev/
* Hostname was NOT found in DNS cache
* Trying 192.168.50.51...
* Connected to www.example.dev (192.168.50.51) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.37.1
> Host: www.example.dev
> Accept: */*
>
< HTTP/1.1 200 OK
* Server nginx/1.8.0 is not blacklisted
< Server: nginx/1.8.0
< Date: Tue, 19 May 2015 01:11:10 GMT
< Content-Type: text/html; charset=utf-8
< Content-Length: 415
< Last-Modified: Wed, 15 Apr 2015 02:53:27 GMT
< Connection: keep-alive
< ETag: "552dd2a7-19f"
< Accept-Ranges: bytes
<
<!DOCTYPE html>
<html>
...
And the output from the nginx access log:
192.168.50.1 - - [19/May/2015:01:17:05 +0000] "GET / HTTP/1.1" 200 415 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36" "-"
So, I decided to comment on the testing site. So I only have a block location = / { ...
. Nginx is now 404s and logs the following error:
2015/05/19 01:24:12 [error] 3116#0: *6 open() "/etc/nginx/html/index.html" failed (2: No such file or directory), client: 192.168.50.1, server: *.example.dev, request: "GET / HTTP/1.1", host: "www.example.dev"
What is the default root in the source nginx config file? I guess this confirms my location = /
pattern does not .
I added $uri
to the access log and see that it shows /index.html
which I assume means the first location matchesbut then it goes to the second location block? So now I just need to figure out how to serve my index.html from a block, /
or just add another block, like this:location =/index.html
source to share
as per @Alexey Ten commented in the ngx_http_index doc :
It should be noted that using the index file causes internal redirection and the request may be processed elsewhere. For example, with the following configuration:
location = / {
index index.html;
}
location / {
...
}
The "/" request will actually be processed in the second place as "/index.html".
In your case, the request "/" will not get /www/src/index.html, but / www / test / index.html.
source to share