AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 658388
Accepted
Neel
Neel
Asked: 2015-01-10 05:47:49 +0800 CST2015-01-10 05:47:49 +0800 CST 2015-01-10 05:47:49 +0800 CST

Nginx 将旧的 PHP url 视为文件

  • 772

很难弄清楚这一点。我已将我的网站从另一个平台更改为 Joomla,现在 Nginx 无法处理旧网址。

我的旧网址是这样的:

example.com/home.php
example.com/contact-us.php

我的新 Joomla SEF 网址是这样的:

example.com/home
example.com/contact-us

根据 Joomla 指南,我有以下 Nginx 配置:

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

 # Process PHP
 location ~ \.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;

            fastcgi_pass   unix:/var/run/php5-fpm.sock;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
 }

我希望 Nginx 将这些旧网址传递给 Joomla 来处理它。现在,正在发生的事情是,Nginx 将这些旧网址视为 php 文件,然后向我显示此No input file specified.错误。然后我将 php 块中的 try_files 更改为,try_files $uri /index.php?$args;因此我的 Nginx 配置如下所示:

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

 # Process PHP
 location ~ \.php$ {
            try_files       $uri /index.php?$args;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;

            fastcgi_pass   unix:/var/run/php5-fpm.sock;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
 }

这是有效的吗?在某些情况下,这会导致无限循环问题吗?这是正确的方法吗?我没有找到任何类似的解决方案。有人可以指导我吗?

nginx
  • 1 1 个回答
  • 505 Views

1 个回答

  • Voted
  1. Best Answer
    AD7six
    2015-01-10T06:14:00+08:002015-01-10T06:14:00+08:00

    location /从未使用过

    您遇到的问题与位置优先级有关(已添加重点)。

    无论列出的顺序如何,nginx 首先搜索由文字字符串给出的最具体的前缀位置。[...] 然后 nginx 按照配置文件中列出的顺序检查正则表达式给出的位置。第一个匹配的表达式停止搜索,nginx 将使用这个位置。如果没有正则表达式匹配请求,则 nginx 使用之前找到的最具体的前缀位置。

    因此,这个位置块:

    location ~ \.php$ {
        try_files $uri =404; # <-
    

    匹配此请求:

    example.com/home.php
    

    并且没有其他位置块是相关的。

    正如您已经意识到的那样,这意味着 nginx 将尝试查找和服务,home.php从而导致 404。

    对主 index.php 文件使用 @location

    通常唯一相关的 php 文件是index.php,您可以像这样使用它:

    try_files $uri $uri/ @joomla;
    
    location @joomla {
        include fastcgi_params;
        fastcgi_pass    unix:/var/run/php5-fpm.sock;
        fastcgi_param   SCRIPT_FILENAME     $document_root/index.php;
        fastcgi_param   SCRIPT_NAME         $document_root/index.php;
        fastcgi_param   DOCUMENT_URI        /index.php;
        fastcgi_index   index.php;
    }
    

    对 *.php 请求使用另一个位置块

    除了前端控制器之外,joomla 还允许/期望直接访问其他 php 文件,例如/administrator/index.php. 要允许访问它们而不尝试处理丢失的 php 文件:

    location ~ \.php$ {
        try_files $uri @joomla;
    
        include fastcgi_params;
        fastcgi_pass    unix:/var/run/php5-fpm.sock;
        fastcgi_index   index.php;
        fastcgi_param   SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    }
    

    这将允许直接访问其他 php 文件(通常,这不是一件好事......)回退到 use /index.php,通过该@joomla位置,用于任何不存在的 php 文件请求。

    请注意,上述设置也在文档中。

    • 3

相关问题

  • Gzip 与反向代理缓存

  • nginx 作为代理的行为

  • Nginx 学习资源 [关闭]

  • 提供 70,000 个静态文件 (jpg) 的最佳方式?

  • 在 Apache、LightTPD 和 Nginx Web 服务器上提供 PHP 5.x 应用程序的现状?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve