工作中会存在配置多个location模块的情况,如果都配置在nginx.conf配置文件中不是特别容易管理,这种情况下就需要单独来定义一个模块的location,可以在nginx.conf配置中指定它的子配置文件,以及location模块中常用的参数,如何配置呢就来说给大家。
1.配置虚拟主机文件
include模块:位于http模块里面
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#tcp_nodelay off;
keepalive_timeout 65;
include /etc/nginx/conf.d/*.conf; #定义子配置文件
}
2.root和alias区别
root:指定web家目录,在定义location的时候,文件的绝对路径=root+location
alias:定义路径别名,会把访问的路径重新定义到其他指定的路径
location /about {
root /data/www/pc; # root访问路径/data/www/pc/about
index index.html;
}
location /test {
alias /data/www/pc; # alias访问路径/data/www/pc
index index.html;
}
3.location详细使用
没有使用正则表达式的时候,nginx会先在server中的多个location模块选取匹配度最高的uri。
uri:是用