Nginx alias root and location pointing tutorial

Source: Internet
Author: User

A virtual server can have a home directory and any number of other directories, which are usually called virtual directories.

Nginx does not have a virtual directory, because nginx originally designed and worked according to the directory. If you want to pin a virtual directory into a virtual directory, only the alias label is similar. There is also a label similar to alias root. What is the difference between them?

Basic differences

The directory specified by alias is accurate and can be understood as ln in linux, and a directory is specified for location.

Root specifies the parent directory of the directory, and the parent directory must contain a directory with the same name as the name specified by locatoin.

Alias usage notes

When using alias, you must add "/" to the directory name.

The rewrite break cannot be used in directory blocks using the alias label.

When using regular expression matching, alias must capture the content to be matched and use it in the specified content.

Alias can only be located in the location block

Example

Location/abc /{
Alias/home/html/abc /;
}

In this section, http: // test/abc/a.html specifies/home/html/abc/a.html. This configuration can also be changed to the root label:

Location/abc /{
Root/home/html /;
}

In this way, nginx will find the abc directory under the/home/html/directory, and the results will be the same. However, if I change the alias configuration

Location/abc /{
Alias/home/html/def /;
}

Then nginx will retrieve data directly from/home/html/def/, for example, accessing http: // test/abc/a.html directs to/home/html/def/a.html

You cannot directly use the root configuration for this configuration. If you need to configure it, you only need to create a soft link (shortcut) of def-> abc under/home/html.

In general, configuring root in location/and configuring alias in location/other is a good habit.

Alias fast-cgi

When nginx processes php scripts, it must be passed to fastcgi for processing. Below is the configuration that fast-cgi cannot work.

Server {
Listen 80;
Server_name pwta;
Root html;

Location/test /{
Alias html/test /;
Autoindex on;
    }
Location ~ \. Php $ {
Fastcgi_pass 127.0.0.1: 9000;
Fastcgi_index index. php;
Fastcgi_param SCRIPT_FILENAME $ document_root $ fastcgi_script_name;
Include fastcgi_params;
    }
}

Through the above configuration, we found that accessing/test/a. php and php scripts does not work. Why? The answer is as follows:

Adding alias will wait tively overwrite the $ document_root to whatever is the alias. note that it will not affect $ fastcgi_script_name or $ request_filename. using the new $ document_root together with regex matching the file name, resolves to the script file

For the above configuration, we want to change it to the following:

Server {
Listen 80;
Server_name pwta;
Index index.html index. php;
Root html;

Location/test /{
Alias html/test /;
}

Location ~ ^/Test/(. + \. php) $ {### This location block was the solution
Alias html/test /;
Fastcgi_pass 127.0.0.1: 9000;
Fastcgi_index index. php;
Fastcgi_param SCRIPT_FILENAME $ document_root $1;
Include fastcgi_params;
}

Location ~ \. Php $ {
Fastcgi_pass 127.0.0.1: 9000;
Fastcgi_index index. php;
Fastcgi_param SCRIPT_FILENAME $ document_root $ fastcgi_script_name;
Include fastcgi_params;
}

Restart nginx. Php file resolution takes effect.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.