配置demo:html
location xxx { root yyy }
浏览器访问 xxx,实际访问的是 yyy/xxx
浏览器访问 xxx/abc.html,实际访问的是 yyy/xxx/abc.html
浏览器访问 xxx/ccc/abc.html,实际访问的是 yyy/xxx/ccc/abc.htmlnginx
配置demo:浏览器
locaiton xxx { # alias必须以 / 结束,不然无效 alias yyy/ }
浏览器访问 xxx,实际访问的是 yyy
浏览器访问 xxx/abc.html,实际访问的是 yyy/abc.html
浏览器访问 xxx/ccc/abc.html,实际访问的是 yyy/ccc/abc.htmlcode
nginx的目录结构以下:server
nginx/ -html/ -index.html -logs/ - access.log -conf/ -nginx.conf
1) 这种配置,http://localhost:8086/access.log,能看到 nginx/logs/access.log,但就别期望能访问 html目录下的文档了htm
server { listen 8086; server_name localhost; location / { root logs; } }
2) 这种配置,访问 http://localhost:8086/log/access.log,能看到 nginx/logs/access.log;
访问 http://localhost:8086/, 能看到 nginx/html/index.htmlblog
server { listen 8086; server_name localhost; location / { root html; index index.html index.htm; } # 配置成 location /log/ 或 location /log 均可以 location /log/ { # 不能写成logs, 必须已 / 结束 alias logs/; # 如下配置没用也能够,只是方便你输入 localhost:8086/log/ 后能,看到nginx/logs/目录下的全部文件 autoindex on; } }
3) 这种配置,访问 http://localhost:8086/logs/access.log,能看到 nginx/logs/access.log;
访问 http://localhost:8086/, 能看到 nginx/html/index.html文档
server { listen 8086; server_name localhost; # http://localhost:8086/ 访问的是 # nginx/html/ (而后会自动显示 index.html 或 index.htm,若是存在这两个文件之一) # 啰嗦的注释: nginx/html(html是root的值)/(/是location的值) location / { root html; index index.html index.htm; } # http://localhost:8086/logs/ 访问的是 # nginx/./logs/ # .是root的值,logs是location的值 # 请与第4种错误配置进行比较,深刻理解root属性 location /logs/ { # 写成./也能够 root .; } }
4) 错误的配置get
server { listen 8086; server_name localhost; location / { root html; index index.html index.htm; } # 这样子配置是错的, 请与第三种配置比较一下 # 关键点:root属性会把root的值加入到最终路径以前 # 即: http://localhost:8086/logs/access.log访问的是: # nginx/logs/logs/access.log # 由于: nginx/logs(root的值)/logs(locaition的值)/access.log, location /logs/ { root /logs/; } }
节选:https://www.cnblogs.com/zhang... 这段话:
root属性指定的值是要加入到最终路径的,因此访问的位置变成了 root的值/locaiton的值。而我不想把访问的URI加入到路径中。因此就须要使用alias属性,其会抛弃URI,直接访问alias指定的位置it
参考:
https://www.cnblogs.com/zhang...
https://www.cnblogs.com/kevin...