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. c# 读取图片文件

    /// <summary> /// 通过FileStream 来打开文件,这样就可以实现不锁定Image文件,到时可以让多用户同时访问Image文件 /// </summary> ...

  2. flash系统奔溃的主要原因

    1.内存泄露(内存超过系统允许的最大限制,11.4版本为2G) 2.脚本死循环 3.舞台内元件的大小超出了系统限制

  3. 多个ModelForm组合成一个表单

    打个比方: 我将用户的基本信息 如用户名密码存在继承了Django auth认证组件中的 AbstractUser 类的模型中,并和第二个存了Details模型中,此模型继承UserInfo模型 继承 ...

  4. Android OS Startup

    OS puts emphases on how to provide interfaces to user's APPs for using hardware device in the conven ...

  5. Unity3d开发集成Google Admob广告增加收入

    在Unity游戏中植入广告是Unity 游戏产品增加收入的一种重要方式,常用的广告有谷歌Admob,百度ssp,腾讯广点通,unity公司的unityads等等,而使用的最多的应该属于谷歌Admob, ...

  6. count group by 组合用法

    1 需求是 求订单表1个月内 订单累计费用超过500的有多少人 根据题意 最先写出的sql是这样的 SELECT SUM(totalfee)AS n FROM sendorder WHERE `add ...

  7. centos系统下安装Nginx

    参考链接 CentOS 7 用 yum 安装 Nginx Nginx负载均衡配置 下载并安装 #使用以下命令 sudo yum install -y nginx #sudo表示使用管理员权限运行命令 ...

  8. 一起来看看IOS内存泄漏的一个问题

    很多iOS开发的朋友都是比较关心内存泄漏的问题,在实际的开发工作中首先我们需要知道程序有没有内存泄露,然后定位到底是哪行代码出现内存泄露了,这样才能将其修复.最简单的方法当然是借助于专业的检测工具,比 ...

  9. vue短信验证码组件

    Vue.component('timerBtn',{ template: '<button v-on:click="run" :disabled="disabled ...

  10. PHP:global全局变量的使用

    global全局变量能够让我们更好的去运用,直接上例子: 1.一个函数,获取函数外的内容: 得到的结果: 2.两个函数,函数2获取函数1的全局变量内容:(重点) 结果: 以上就是我的总结啦 END