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 / 问题 / 411111
Accepted
Mediocre Gopher
Mediocre Gopher
Asked: 2012-07-26 07:27:14 +0800 CST2012-07-26 07:27:14 +0800 CST 2012-07-26 07:27:14 +0800 CST

Nginx 404 页面行为

  • 772

我有以下 nginx 服务器配置:

server {
    listen 80;
    server_name example.com
    root   /server/root;

    index index.php;

    error_page 404 = /index.php;

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

        proxy_pass         http://127.0.0.1:8080;
        proxy_redirect     off;
        proxy_set_header   Host $host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_set_header   X-Request-URI    $request_uri;
    }

}

我想要的行为是,当 nginx 遇到对不存在的文件的请求时,它会通过 404 页面显示 index.php 页面。问题是似乎 apache(这是被代理回的)在收到请求时仍在尝试解决原始请求。如果我去http://example.com/blahblah,我会返回错误:

The requested URL /blahblah was not found on this server.

这是一个 apache 错误。我怎样才能使 index.php 显示为 404 页面,就好像它是一个静态文件一样?

apache-2.2
  • 3 3 个回答
  • 2079 Views

3 个回答

  • Voted
  1. Best Answer
    kolbyjack
    2012-07-26T08:23:42+08:002012-07-26T08:23:42+08:00

    您使用的是哪个版本的 nginx?此问题已在 1.1.12 中解决:http: //nginx.org/en/CHANGES

    编辑:如果您无法更新,您可以将当前的 error_page 和 try_files 替换为:

    location / {
      try_files $uri $uri /index.php;
    }
    
    location ~ \.php$ {
      # Leave the =404 at the end so we don't 500 when /index.php doesn't exist
      try_files $uri /index.php =404;
    
      proxy_pass         http://127.0.0.1:8080;
      proxy_redirect     off;
      proxy_set_header   Host $host;
      proxy_set_header   X-Real-IP        $remote_addr;
      proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
      proxy_set_header   X-Request-URI    $request_uri;
    }
    
    • 3
  2. Joshua
    2012-07-26T07:35:39+08:002012-07-26T07:35:39+08:00

    nginx 将不得不拦截 Apache 的响应以识别 404 版本,并返回它自己的。

    如果 nginx 没有办法做到这一点,那么也许您可以将 Apache 配置为不返回任何东西——从而触发 nginx 自己的 404 状态?

    • 0
  3. Michael Hampton
    2012-07-26T08:17:08+08:002012-07-26T08:17:08+08:00

    如果你坚持使用 Apache,你将不得不设置 Apache 重写规则来向你的应用程序发送 404 错误,而不是在 nginx 的配置中。

    • 0

相关问题

  • Apache Django Mod_Wsgi - 自动重新加载应用程序

  • Apache:对多个虚拟主机使用相同的目录指令

  • Apache 上的子域不工作 - 找不到服务器

  • PHP 作为 CGI 还是 Apache 模块?

  • 避免将某些丢失的文件记录到 Apache2 错误日志中

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