【nginx】root alias 区别,以及server root , location root 区别

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;
        }

    }

转载于:https://www.cnblogs.com/china-flint/p/10313804.html

你可能感兴趣的:(运维,php)