location 模块
location模块是nginx中用的最多的,也是最重要的模块了,什么负载均衡啊、反向代理啊、虚拟域名都与location模块相关。
location 根据它字面意思就知道是来定位的,定位URL,解析URL,所以,它也提供了强大的正则匹配功能,也支持条件判断匹配,用户可以通过location指令实现Nginx对动、静态网页进行过滤处理。像我们的php环境搭建就是用到了它。
我们先来看这个,设定默认首页和虚拟机目录。
location / { root /Users/yangyi/www; index index.php index.html index.htm; }
location /表示匹配访问根目录。
root指令用于指定访问根目录时,虚拟主机的web目录,这个目录可以是相对路径(相对路径是相对于nginx的安装目录)。也可以是绝对路径。
#反向代理配置 location /itcast/ { proxy_pass http://127.0.0.1:12345; proxy_set_header X-real-ip $remote_addr; proxy_set_header Host $http_host; } #采用uwsgi方式 location /python/ { include uwsgi_params; uwsgi_pass 127.0.0.1:33333; } #访问nginx本机目录的文件 location / { root /home/itcast/xwp/itcast/; index index.html index.htm; } location /static/ { alias /var/static/; }