不懂就问系列: PHP 网站使用 nginx 遇到的一些问题 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
请不要在回答技术问题时复制粘贴 AI 生成的内容
Ethson
V2EX    程序员

不懂就问系列: PHP 网站使用 nginx 遇到的一些问题

  •  
  •   Ethson 2020-10-19 13:07:13 +08:00 2242 次点击
    这是一个创建于 1823 天前的主题,其中的信息可能已经有所发展或是发生改变。

    准备使用 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。其它都是类似的。

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

    8 条回复    2020-10-19 14:18:50 +08:00
    dilu
        1
    dilu  
       2020-10-19 13:27:28 +08:00
    ```
    try_files $uri $uri.php /$url.php;
    ```

    试试?
    dilu
        2
    dilu  
       2020-10-19 13:31:09 +08:00
    try_files $uri $uri/ /$url.php;

    如果还是不行试试这个
    zjsxwc
        3
    zjsxwc  
       2020-10-19 13:55:26 +08:00
    仅仅只是去掉.php 后缀的话

    ```
    location / {
    try_files $uri $uri.html $uri/ @extensionless-php;
    index index.html index.htm index.php;
    }
    location @extensionless-php {
    rewrite ^(.*)$ $1.php last;
    }
    ```
    Ethson
        4
    Ethson  
    OP
       2020-10-19 14:06:02 +08:00
    @dilu 你的第二个答案是对的。

    @zjsxwc 你的答案也是对的。

    非常感谢两位。只是 @dilu 提供的正确答案有些不太明白,为什么要加上 **$uri/**,这不是在目录下查找的嘛,但我这里没有这个目录啊,去找也没用啊,既然是无用功,那为什么去做呢?不得解....
    Lax
        5
    Lax  
       2020-10-19 14:09:13 +08:00
    直接下载的问题,加一句 default_type text/html;
    Ethson
        6
    Ethson  
    OP
       2020-10-19 14:15:57 +08:00
    @Lax 谢谢,学到了。
    brader
        7
    brader  
       2020-10-19 14:18:24 +08:00
    @Ethson 你理解有误,你仔细琢磨 try_files 的用法,其实我想,$uri/没有起作用,因为它正如你想的一样,没找到,所以继续执行后面一句,起作用的是后面一句 /$url.php ,
    至于你一开始,填了$uri.php ,问题就在这里了,nginx 找到了改文件,所以 nginx 的 try_files 会将该文件发送给用户。
    Hanada
        8
    Hanada  
       2020-10-19 14:18:50 +08:00 via Android
    @Ethson 优先查找是否存在该路径,不存在才去找.php 文件。也就是如果同时有一个 test 文件夹和 test.php ,会优先匹配到 test 文件夹而不是 test.php
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     6251 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 43ms UTC 02:35 PVG 10:35 LAX 19:35 JFK 22:35
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86