Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

php 网站使用 nginx 遇到的一些问题

准备使用 php 做个网站,域名是 dev.hellotools.org,根目录下有四个文件,分别是,

  1. index.php,就是主页了
  2. about.php,关于页面
  3. 404.php,也就是 404 页面了
  4. /a/b/page.php,a 和 b 是目录,意思就是根目录下有个二级目录,里边有个 page.php

现在我想实现以下功能,

  1. 访问 dev.hellotools.org 时,浏览器上地址栏显示 dev.hellotools.org,且浏览器显示 index.php 的内容;
  2. 访问 dev.hellotools.org/about 时,浏览器上地址栏显示 dev.hellotools.org/about,且浏览器显示 about.php 的内容;
  3. 访问 dev.hellotools.org/nono 时,浏览器上地址栏显示 dev.hellotools.org/nono,且浏览器显示 404.php 的内容;
  4. 访问 dev.hellotools.org/a/b/page 时,浏览器上地址栏显示 dev.hellotools.org/a/b/page,且浏览器显示 /a/b/page.php 的内容

于是我的 nginx 配置如下,

server
    {
        listen 80;
        server_name  dev.hellotools.org;

        index index.html index.htm index.php default.html default.htm default.php;
        root  /home/wwwroot/dev.hellotools.org;

        charset utf-8;

        error_page 404 /404.php;

        ## enable php path info
        location ~ [^/].php(/|$)
        {
            fastcgi_pass  unix:/tmp/php-cgi.sock;
            fastcgi_index index.php;
            include fastcgi.conf;
            include pathinfo.conf; 
        }

        location / {
            try_files $uri $uri.php /index.php;
        }
    }

搞不清楚我的配置哪里有问题,在访问 about 页面的时候,竟然文件直接下载了,404 也出不来,不知道哪里出问题了。

每个页面的内容都写清楚了,就几个简单的汉字和字母。比如你打开的是 index.php,那么页面内容就有:index.php。其它都是类似的。

折腾一晚上了,实在找不出了,望有了解的大兄弟指点一下。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

try_files 的最后一个位置(fall back)是特殊的,它会发出一个内部 “子请求” 而非直接在文件系统里查找这个文件。除此之外前几个位置都是找文件将文件内容直接返回给用户。
可以这样写:

try_files $uri $uri.php;  或者 rewrite "^/(.*)$" /$1.php;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...