nginx+php的配置

Posted 郁冬

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nginx+php的配置相关的知识,希望对你有一定的参考价值。

nginx+php的配置比较简单,核心就一句话----

把请求的信息转发给9000端口的PHP进程,

让PHP进程处理 指定目录下的PHP文件.

如下例子:

location ~ \\.php$ {
            //根目录
            root html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

1:碰到php文件,

2: 把根目录定位到 html,

3: 把请求上下文转交给9000端口PHP进程,

4: 并告诉PHP进程,当前的脚本是 $document_root$fastcgi_scriptname

 

配置nginx支持pathinfo,这样便能支持TP框架的/news/1这种请求

location ~ \\.php(.*)$ {
            //根目录
            root html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO $1;
            include        fastcgi_params;
        }

 

 

配置nginx重写(运用正则表达式)

try_files方法实现重写

是指,

先试试 有没有 /goods/1/ 这个目录

再试试 有没有 /goods/1 这个文件

最后试试 /index.php?goods/1 这个URL

 

以上是关于nginx+php的配置的主要内容,如果未能解决你的问题,请参考以下文章

Nginx——Nginx启动报错Job for nginx.service failed because the control process exited with error code(代码片段

Nginx配置文件详细介绍

nginx配置php

如何正确配置Nginx + PHP

如何正确配置 Nginx + PHP

如何正确配置Nginx + PHP