nginx部署前端项目location时root和alias配置指南
作者:阿松哥哥2018
操作说明
1、nginx目录中html目录下放置green 前端项目
监听端口:8181
nginx配置文件配置location时使用root方式
# root 方式
# 方式1 域名直接可访问到 即 localhost:8181
#location / {
# root html;
# index green/index.html green/index.htm;
#}
# 方式2 域名直接可访问到 即 localhost:8181
#location / {
# root html/green/;
# index index.html index.htm;
#}
# 方式2.1 域名直接可访问到 即 localhost:8181
#location / {
# root html/green;
# index index.html index.htm;
#}
# 方式3 域名+/green 可访问到 即 localhost:8181/green
#location /green/ {
# root html;
# index index.html index.htm;
#}
# 方式3.1 访问不到green下任务资源
#location /green/ {
# root html/green/;
# index index.html index.htm;
#}
以上三种 方式结论验证 用root属性指定的值是要加入到最终路径中的,匹配条件会拼接到路径中
即最终获取的静态页面路径为:域名 + root + 区配条件 + index
即找到 localhost:8181/html/green/index.html
备注:方式2 和方式2.1 用于验证 root 属性的值最后的 “/“为非必须,有没有最后一个”/” 都可以访问到
nginx配置文件配置location时使用alias方式
# alias 方式
# 方式1 域名直接可访问到 即 localhost:8181
#location / {
# alias html/green/;
# index index.html index.htm;
#}
# 方式1.1 访问不到green下任务资源
#location / {
# alias html/green;
# index index.html index.htm;
#}
# 方式2 域名直接可访问到 即 localhost:8181
#location / {
# alias html/;
# index green/index.html green/index.htm;
#}
# 方式3 域名直接可访问到 即 localhost:8181/green
#location /green {
# alias html/green/;
# index index.html index.htm;
#}
# 方式3.1 域名直接可访问到 即 localhost:8181/green
#location /green/ {
# alias html/green/;
# index index.html index.htm;
#}
以上三种 方式结论验证 用alias属性指定的值,匹配条件不会拼接到路径中,会直接在alias属性的值下面去找资源
即最终获取的静态页面路径为:域名 + alias + index
即找到 localhost:8181/html/green/index.html
备注:方式1 和方式1.1 用于验证 alias 属性的值最后的 “/“为必须,没有最后一个”/” 访问不到
完整的nginx配置文件如下
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
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"';
map $time_iso8601 $logdate{
'~^(?<ymd>\d{4}-\d{2}-\d{2})' $ymd;
default 'date-not-found';
}
access_log logs/access-$logdate.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 8181;
server_name localhost;
access_log logs/access-$logdate.log main;
# root 方式
# 方式1 域名直接可访问到 即 localhost:8181
location / {
root html;
index green/index.html green/index.htm;
}
# 方式2 域名直接可访问到 即 localhost:8181
#location / {
# root html/green/;
# index index.html index.htm;
#}
# 方式2.1 域名直接可访问到 即 localhost:8181
#location / {
# root html/green;
# index index.html index.htm;
#}
# 方式3 域名+/green 可访问到 即 localhost:8181/green
#location /green/ {
# root html;
# index index.html index.htm;
#}
# 方式3.1 访问不到green下任务资源
#location /green/ {
# root html/green/;
# index index.html index.htm;
#}
# 以上三种 方式结论验证 用root属性指定的值是要加入到最终路径中的,匹配条件会拼接到路径中
# 即最终获取的静态页面路径为:域名 + root + 区配条件 + index
# 即找到 localhost:8181/html/green/index.html
# 备注:方式2 和方式2.1 用于验证 root 属性的值最后的 "/"为非必须,有没有最后一个"/" 都可以访问到
# alias 方式
# 方式1 域名直接可访问到 即 localhost:8181
#location / {
# alias html/green/;
# index index.html index.htm;
#}
# 方式1.1 访问不到green下任务资源
#location / {
# alias html/green;
# index index.html index.htm;
#}
# 方式2 域名直接可访问到 即 localhost:8181
#location / {
# alias html/;
# index green/index.html green/index.htm;
#}
# 方式3 域名直接可访问到 即 localhost:8181/green
#location /green {
# alias html/green/;
# index index.html index.htm;
#}
# 方式3.1 域名直接可访问到 即 localhost:8181/green
#location /green/ {
# alias html/green/;
# index index.html index.htm;
#}
# 以上三种 方式结论验证 用alias属性指定的值,匹配条件不会拼接到路径中,会直接在alias属性的值下面去找资源
# 即最终获取的静态页面路径为:域名 + alias + index
# 即找到 localhost:8181/html/green/index.html
# 备注:方式1 和方式1.1 用于验证 alias 属性的值最后的 "/"为必须,没有最后一个"/" 访问不到
# 后台服务;
location /fdiagnose/ {
proxy_ignore_client_abort on;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://localhost:9090;
}
}
}附:Nginx 配置中root和alias的区别分析
root和alias都可以定义在location模块中,都是用来指定请求资源的真实路径,比如:
location /i/ {
root /data/w3;
}请求 http://foofish.net/i/top.gif 这个地址时,那么在服务器里面对应的真正的资源
是 /data/w3/i/top.gif文件
注意:真实的路径是root指定的值加上location指定的值 。

而 alias 正如其名,alias指定的路径是location的别名,不管location的值怎么写,资源的 真实路径都是 alias 指定的路径 ,比如:
location /i/ {
alias /data/w3/;
}同样请求 http://foofish.net/i/top.gif 时,在服务器查找的资源路径是: /data/w3/top.gif

其他区别:
1、 alias 只能作用在location中,而root可以存在server、http和location中。
2、 alias 后面必须要用 “/” 结束,否则会找不到文件,而 root 则对 ”/” 可有可无。
总结
到此这篇关于nginx部署前端项目location时root和alias配置的文章就介绍到这了,更多相关nginx location用root和alias配置内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
