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

    }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM