首先nginx安装好之后的缺省配置文件:nginx/conf/nginx.conf
        location / {
            root   html;
            index  index.html index.htm;
        }
这里定义的root地址是相对于nginx的根路径的;那么当用户通过浏览器访问根地址: http://<hostname>:<port>时,nginx试图返回的页面就是:nginx/html/index.html。
当然这里root也可以写全路径,例如 /home/<username>/tools/nginx/html,效果是一样的。
这里我们要讨论如何把一个静态页面配置到nginx里面。
假设静态页面内容放在文件夹 /app/testapp/www下面(同时假设/app/testapp/www/index.html也存在),我们如何配置nginx使得http://<hostname>:<port>/testapp能够访问到这些静态页面内容呢。
- 方法1:root
        location /testapp {
            root   /app/testapp/www;
            index  index.html index.htm;
        }
结果:404 Not Found
查看nginx日志(nginx/logs/error.log):
[error] 14914#0: *23 open() "/app/testapp/www/testapp" failed (2: No such file or directory), client: 127.0.0.1, server: localhost, request: "GET /testapp HTTP/1.1", host: "localhost"
原来nginx试图访问的文件路径是:/app/testapp/www/testapp,这个路径是”root“的内容再拼上location的值组成的;那我们给修改location和root的值:
        location /www {
            root /app/testapp;
            index  index.html index.htm;
        }
然后通过地址http://<hostname>:<port>/www就可以访问了;但是这里location必须用”www“不能用”testapp“,这就非常不可接受了,解决的办法可以是修改静态页面的地址,再加一层testapp路径,例如:"/app/testapp/www/testapp",然后再配置:
        location /testapp {
            root /app/testapp/www;
            index  index.html index.htm;
        }
这样是可以的。另一个方法是采用alias取代root。
- 方法2:alias
保留今天页面的地址"/app/testapp/www",配置nginx的配置文件:
        location /testapp {
            alias /app/testapp/www;
            index  index.html index.htm;
        }
关于alias和root的区别,请查阅nginx文档或者自行google,这里不再重复贴了。