【问题标题】:Nginx & Handling Files Without ExtensionNginx & 处理无扩展名的文件
【发布时间】:2020-04-17 00:51:42
【问题描述】:

我正在尝试让 Nginx 处理没有扩展名的 php 文件(即处理 http://localhost/sample 的方式与处理 http://localhost/sample.php 的方式相同)。

这是我的网站配置:

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    server_name localhost;

    root /var/www;
    index index.html index.php;

    location ~ \.(hh|php)$ {
        fastcgi_keep_conn on;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location / {
        try_files $uri $uri/ $uri.html @extensionless =404;
    }

    location @extensionless {
        rewrite ^(.*)$ $1.php last;
    }
}

据我所知,它应该可以解决问题。但是 - 它没有。尝试 http://localhost/sample 只会让我进入 404 页面(而 http://localhost/sample.php 工作正常)。

打开调试时,我在日志中看到以下内容:

2015/07/19 15:37:00 [debug] 4783#0: *1 http script var: "/sample"
2015/07/19 15:37:00 [debug] 4783#0: *1 trying to use file: "/sample" "/var/www/sample"
2015/07/19 15:37:00 [debug] 4783#0: *1 http script var: "/sample"
2015/07/19 15:37:00 [debug] 4783#0: *1 trying to use dir: "/sample" "/var/www/sample"
2015/07/19 15:37:00 [debug] 4783#0: *1 http script var: "/sample"
2015/07/19 15:37:00 [debug] 4783#0: *1 http script copy: ".html"
2015/07/19 15:37:00 [debug] 4783#0: *1 trying to use file: "/sample.html" "/var/www/sample.html"
2015/07/19 15:37:00 [debug] 4783#0: *1 trying to use file: "@extensionless" "/var/www@extensionless"
2015/07/19 15:37:00 [debug] 4783#0: *1 trying to use file: "=404" "/var/www=404"

这很奇怪。基本上看起来@extensionless 被视为纯文件名(而不是导致 URL 重写的位置)。

我错过了什么? :) 谢谢!

【问题讨论】:

    标签: nginx


    【解决方案1】:

    只是更新(以防有人觉得这很有用)我最终让它工作了。

    这就是成功的配置:

    server {
        listen 80;
    
        root /var/www;
        index index.html index.htm index.php;
    
        server_name localhost;
    
        location / {
            if (!-e $request_filename){
                rewrite ^(.*)$ /$1.php;
            }
            try_files $uri $uri/ =404;
        }
    
        location ~ \.php$ {
            try_files $uri =404;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
    }
    

    【讨论】:

      【解决方案2】:
          try_files $uri $uri/ $uri.html @extensionless =404;
      

      是的,@extensionless 被视为普通文件,这是因为您在 @extensionless 之后添加了一个额外的 =404 try_files - @extensionless 部分只能用作最后一个参数内部重定向到另一个上下文。

      如果您不仅要支持在没有 .php 的情况下处理请求,而且还要从任何请求中去除 .php,您可能需要执行以下操作:

      location / {
          if (-e $request_filename.php){
              rewrite ^/(.*)$ /$1.php;
          }
      }
      location ~ \.php$ {
          if ($request_uri ~ ^/([^?]*)\.php(\?.*)?$) {
              return 302 /$1$2;
          }
          fastcgi_...
      }
      

      【讨论】:

      • nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls 最好不要使用 IF 和 REWRITE。在这种情况下,最好执行 301 重定向而不是 302(浏览器会记住它)。在下面寻找替代答案。
      • @EugeneZakharenko,您在两个帐户上都不正确。真正邪恶的是 301 重定向,因为正如您所指出的,它们被浏览器缓存;您忽略的是它们被永久缓存,导致各种奇怪的行为。
      • 请阅读 301 重定向及其用途:en.wikipedia.org/wiki/HTTP_301。它将扫清它所服务的浏览器的方式。根本没有奇怪的行为。 301 的“永远”缓存是绝对正常的,因为它是永久重定向。在语义 url 情况下需要完全永久重定向,以使搜索引擎确保可以在唯一地址访问这些 url。我的第二点不正确是什么? :)
      • 更新:阅读 Google 关于使用 301 重定向更改 URL 地址的良好做法的建议:support.google.com/webmasters/answer/93633?hl=en
      • 完美。我尝试了许多其他解决方案,但索引页不起作用。
      【解决方案3】:

      最好用最快最简单的方式避免使用慢速重写和邪恶if:

      location ~ ^(.*)\.php$ # If PHP extension then 301 redirect to semantic URL
      {
          return 301  $scheme://$server_name$1$is_args$args;
      }
      location ~ ^/(.*)
      {
          try_files $uri.php @static; # If static, serve it in @static
          include fastcgi_params; # if semantic, serve it here
          fastcgi_param SCRIPT_FILENAME $document_root/$1.php;
      }
      location @static
      {
          try_files $uri =404;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-03-14
        • 1970-01-01
        • 1970-01-01
        • 2015-05-28
        • 1970-01-01
        • 2021-04-11
        • 1970-01-01
        • 2018-08-02
        相关资源
        最近更新 更多