nginx-root-alias-详解

最近在研究前后端分离站点配置在同一域名下,发现root,alias有区别,而且所有的root如果都放置在location下面访问无效的问题,才有此总结,本文只是作者自己的个人见解,非喜勿喷

root , alias 区别

  • root的配置 带匹配路径的
location /i/ {
root /data/w3/;
}
### 如果访问 www.domain.com/i/test.png 最后返回的结果为 /data/w3/i/test.png ,root 配置最后的/要不要都行
  • alias的配置 不带匹配路径的,而且最后一定要加/,nginx可以处理多个////为一个/
location /i/ {
alias /data/w3/;
}
### 如果访问 www.domain.com/i/test.png 最后返回的结果为 /data/w3/test.png ,root 配置最后的/一定是要的,否则返回结果会变成 /data/w3test.png 而返回的404

server root , location root 区别

root 指的是请求的根目录,引用nginx官网的解释:

Sets the root directory for requests . A path to the file is constructed by merely adding a URI to the value of the root directive 翻译:设置请求的根目录,设置的文件路径要加上root后面匹配的URI

Note that the root directive is placed in the server context. Such root directive is used when the location block selected for serving a request does not include own root directive. 如果匹配的location里面没有自己的root指令,才用server里面的root指令

总结:location里面的root优先级高于server

 ### 如果不想使用root的目录转换功能而需要做重定向,应该提取root在location的外面
root "C:/Users/flint/PhpstormProjects/basic/web/";
location ~ ^/admin/ {
index test.php ;
autoindex on;
try_files $uri $uri/ /test.php?$args;
}

贴上完整的 xc.com 站点配置文档 xc.com.conf

server {
listen 80;
server_name xc.com;
charset utf-8;
root "C:/Users/flint/PhpstormProjects/basic/web/";
location ~ ^/admin/ {
index test.php ;
autoindex on;
try_files $uri $uri/ /test.php?$args;
} location / {
root "E:/vue-admin-template/dist";
set $cors_origin "";
if ($http_origin ~* "^xc.com:9528$") {
set $cors_origin $http_origin;
}
if ($http_origin ~* "^xc.com$") {
set $cors_origin $http_origin;
}
if ($request_method = 'OPTIONS') {
add_header Access-Control-Allow-Origin $cors_origin;
add_header Access-Control-Allow-Headers *;
add_header Access-Control-Allow-Methods POST,OPTIONS;
add_header Access-Control-Allow-Credentials true;
return 204;
}
index index.html ;
autoindex on;
} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
} #error_page 404 /404.html; # redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
} }

【nginx】root alias 区别,以及server root , location root 区别的更多相关文章

  1. nginx 图片访问404 (使用location中使用 root,alias的区别)

    问题描述: 在/data/code_img/文件下有很多验证码图片,想将他们展示出来 希望通过 http://127.0.0.1/img/1.png 这种形式访问到对应图片,刚开始nginx中配置如下 ...

  2. nginx文件路径配置(root|alias)

    nginx指定文件路径主要有两种方式:root|alias. 那么他们究竟有什么区别呢? 指令的使用方法和作用域: [root] 语法:root path 默认值:root html 配置段:http ...

  3. nginx.conf 配置解析之 server配置

    server{} 包含在http{}内部,每一个server{}都是一个虚拟主机(站点) 以下为nginx.conf配置文件中server{  }部分的内容. server { listen ; // ...

  4. nginx的location root alias指令以及区别

    原文:http://blog.csdn.net/bjash/article/details/8596538 location /img/ { alias /var/www/image/; } #若按照 ...

  5. Nginx设置alias实现虚拟目录 alias与root的用法区别

    Nginx 貌似没有虚拟目录的说法,因为它本来就是完完全全根据目录来设计并工作的.如果非要给nginx安上一个虚拟目录的说法,那就只有alias标签比较"像",干脆来说说alias ...

  6. Nginx的alias的用法及与root的区别

    以前只知道Nginx的location块中的root用法,用起来总是感觉满足不了自己的一些想法.然后终于发现了alias这个东西. 先看toot的用法 location /request_path/i ...

  7. Nginx(alias 和 root的区别)

    Nginx(alias 和 root的区别)1.alias 和 root 的区别: location /request_path/image { root /local_path/image/; } ...

  8. 【03】Nginx:location / root / alias

    写在前面的话 前面我们谈了 nginx 基础的 WEB 服务配置以及定制我们的日志显示格式,接下来我能更加详细的说说 server 字段. location 字段 在 Server 中,如果我们只是一 ...

  9. nginx之location(root/alias)

    location配置 1. 语法规则(按优先级) =        表示精确匹配,优先级最高 ^~      表示uri以某个常规字符串开头,用于匹配url路径(而且不对url做编码处理,例如请求/s ...

随机推荐

  1. 装配bean,基于xml

    一.bean的实例化方式 1.默认构造 <bean id="" class=""></bean> 必须提供默认构造方法 2.静态工厂 用 ...

  2. apache安装 windows

    进入cmd cd apache目录 httppd.exe ?显示全部命令 httppd.exe -k install 安装apache httppd.exe -k start 启动 检测是否运行 浏览 ...

  3. 如何在K3 WISE BOS集成开发工具中自定义字段过滤条件

    1.结论 对于输入过滤条件后BOS报“列名不正确”的过滤条件,要在列名前增加x2标识 无效的过滤 FNumber ,,,,,) 正确的过滤 x2.FNumber ,,,,,) 2.完全可以不看的探索过 ...

  4. java环境变量配置备忘录

    用鼠标右击“我的电脑”->属性->高级->环境变量系统变量->新建->变量名:JAVA_HOME 变量值:D:\Program Files\Java\jdk1.6.0_1 ...

  5. centos 卸载 docker

    yum list installed | grep docker //查看安装过的包 docker-engine.x86_64                 17.03.0.ce-1.el7.cen ...

  6. So you want to write a desktop app in Python

    So you want to write a desktop app in Python Thomas Kluyver 2014-06-16 23:55 51 Comments Source This ...

  7. API:access_token

    access_token存在意义:   1.身份验证(一个channel_id一般有0个或1个有效的access_token) 2.限制用户访问服务器数据的有效期 3.限制用户访问权限 access_ ...

  8. 概念:详细讲解url和路由概念

    例如:一个网址为 http://www.abc.com/aa 定义:/aa = bb/cc/dd 那么:http://www.abc.com/aa就是一个url,那么我们可以得出:网址=url 而当我 ...

  9. sublime打开txt文件乱码的问题

    我们使用Sublime打开TXT文件的时候,会经常因为编码的问题造成乱码. 这是因为TXT记事本的默认保存编码格式是GBK,而Sublime text不支持GB2312和GBK编码. 我们可以通过安装 ...

  10. Linux MySQL单实例源码编译安装5.6

    cmake软件 tar -zxvf cmake-2.8.11.2.tar.gz cd cmake-2.8.11.2 ./bootstrap make make install cd ../   依赖包 ...