The most perfect solution to Nginx deployment thinkphp Project

Source: Internet
Author: User

The configuration of the Web-based general solution is as follows:

server {

...

Location/{

Index index.htm index.html index.php;

#访问路径的文件不存在则重写URL转交给ThinkPHP处理

if (!-e $request _filename) {

Rewrite ^/(. *) $/index.php/$1 last;

Break

}

}

Location ~ \.php/?. *$ {

Root/var/www/html/website;

Fastcgi_pass 127.0.0.1:9000;

Fastcgi_index index.php;

#加载Nginx默认 the server environment variables configuration

Include fastcgi.conf;

#设置PATH_INFO并改写SCRIPT_FILENAME, SCRIPT_NAME Server environment variables

Set $fastcgi _script_name2 $fastcgi _script_name;

if ($fastcgi _script_name ~ "^ (. +\.php) (/.+) $") {

Set $fastcgi _script_name2 $;

Set $path _info;

}

Fastcgi_param path_info $path _info;

Fastcgi_param script_filename $document _root$fastcgi_script_name2;

Fastcgi_param script_name $fastcgi _script_name2;

}

}

In fact, it should be simpler to use the FastCGI module with a fastcgi_split_path_info directive specifically designed to solve this kind of problem, which separates the URL according to the given regular expression, thus extracting the script name and path info information. Using this directive avoids using the IF statement, which is simpler to configure.
There is also a simpler way to determine if a file exists, using the try_files directive.

server {

...

Location/{

Index index.htm index.html index.php;

#如果文件不存在则尝试TP解析

Try_files $uri/index.php$uri;

}

Location ~. +\.php ($|/) {

Root/var/www/html/website;

Fastcgi_pass 127.0.0.1:9000;

Fastcgi_index index.php;

#设置PATH_INFO, note that Fastcgi_split_path_info has automatically rewritten the fastcgi_script_name variable,

#后面不需要再改写SCRIPT_FILENAME, script_name the environment variable, so you must set the fastcgi.conf before loading the

Fastcgi_split_path_info ^ (. +\.php) (/.*) $;

Fastcgi_param path_info $fastcgi _path_info;

#加载Nginx默认 the server environment variables configuration

Include fastcgi.conf;

}

}



The most perfect solution to Nginx deployment thinkphp Project

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.