【问题标题】:Removing index files from the URL with Nginx使用 Nginx 从 URL 中删除索引文件
【发布时间】:2011-11-19 17:34:46
【问题描述】:

在工作中,我们正在从共享 LAMP 堆栈切换到运行 nginx 的 VPS。我对 Apache 更熟悉了,但学习使用 Nginx 非常令人兴奋(或者与学习配置新的网络服务器一样令人兴奋。

当前的问题是:在几个域中,我们需要从 URL 中删除 index.php 以进行规范化。这是我们目前需要为这些域处理的最后一件事。

我一直在研究从 URL 中删除 index.php 的大量不同技术(其中大部分专门用于 CI 或 ExpressionEngine),并且我尝试将其中一些技术用于个人用途,但我最终得到了一个我只能想象的无限循环错误与以下内容有关:

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

我一心想了解这一切是如何运作的,但现在我需要寻求帮助来解决这个问题,这样我们才能继续前进,我可以找出我做错了什么。

我将非常感谢任何回复,并将进一步感谢任何愿意深入了解该主题以帮助像我这样的新人以及可能处于类似情况的其他任何人阅读本文的人。

谢谢!

更新
为了让事情变得更简单,我只是将我的 nginx 配置放在这里用于这个虚拟主机。

server  {
    listen          80;
    server_name     examplesite.com;
    # redirect non-www to www. for canonical urls
    rewrite ^/(.*) http://www.examplesite.com/$1 permanent; 
}

server {
    listen          80;
    server_name     www.examplesite.com;

    error_log   /srv/http/nginx/examplesite.com/log/nginx-error.log;
    access_log  /srv/http/nginx/examplesite.com/log/nginx-access.log;

    root  /srv/http/nginx/examplesite.com/root;

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

    location ~ \.php$ {
        include        fastcgi.conf;
        fastcgi_param  PHP_ADMIN_VALUE "error_log=/srv/http/nginx/examplesite.com/log/php-error.log";
        fastcgi_pass   unix:/var/run/php-fpm/php-fpm.sock;
    }

}

【问题讨论】:

  • 如果您需要传递参数,请使用 try_files $uri $uri/ /index.php/$uri&$args; $args =404 之类的东西?
  • 并使用 last 参数(在 apache 中也称为 [L])。对于 CI,它将类似于 try_files $uri $uri/ /index.php?/$uri last;
  • 该网站是一个简单的静态网站。没有任何东西像 CI 那样通过 index.php。我只需要从 URL 中删除 index.php 即可进行规范化。
  • 它使用.php 所以不是那么静态。我认为 index.php 使用一些 GET 参数来处理 url(例如index.php?q=somepage)所以在这种情况下你可以try_files $uri $uri/ /index.php?q=$uri&$args last;。但无论如何,请提供有关您的架构的更多信息,或者只是阅读更多关于nginx configuration
  • index.php 几乎完全是静态的。这个网站没有运行框架或任何东西。它实际上是一页+一个单独的文件来处理表单操作。我的问题是关于出于规范原因强制 www.example.com/index.php 重写为 www.example.com。这不会破坏幕后发生的任何事情。

标签: nginx seo location rewrite


【解决方案1】:

试试这个...

server  {
    listen          80;
    server_name     examplesite.com;
    # redirect non-www to www. for canonical urls
    # proper way to do this
    rewrite ^ http://www.examplesite.com$request_uri? permanent; 
}

server {
    listen          80;
    server_name     www.examplesite.com;

    error_log   /srv/http/nginx/examplesite.com/log/nginx-error.log;
    access_log  /srv/http/nginx/examplesite.com/log/nginx-access.log;

    root  /srv/http/nginx/examplesite.com/root;

    # add index under server as you have done for root
    index index.php index.html;

    location / {
        # drop the "=404". Nginx will return 404 by itself if not found.
        # note the "last" should not be added to "try_files"
        # whether you need something after "$uri/" depends on your setup.
        # this should do for most unless running WP etc
        try_files $uri $uri/;
    }

    location ~ \.php$ {
        # Return '400 Bad Request' for malformed URLs
        # See: http://wiki.nginx.org/Pitfalls#Pass_Non-PHP_Requests_to_PHP.
        location ~ \..*/.*\.php$ {
            return 400;
        }

        # Rewrite "index.php" requests
        rewrite ^(.*)index.php(.*)$ $1$2    permanent;

        # continue for other php and rewritten "index.php" requests 
        include        fastcgi.conf;
        fastcgi_param  PHP_ADMIN_VALUE "error_log=/srv/http/nginx/examplesite.com/log/php-error.log";
        fastcgi_pass   unix:/var/run/php-fpm/php-fpm.sock;
    }

    ...


}

不是 100% 关注 index.php 重写行,因为它有可能使您进入重定向循环。如果是这种情况,那么答案就是只处理 index.php 请求,但更改应用程序中的所有链接以删除它们,以便它们随着时间的推移而消失。

【讨论】:

    【解决方案2】:

    首先,检查你的索引指令

    server {
      index index.php index.html;
      ...
    

    然后确保您配置的后端正确处理 php

    location ~ \.php$ {
        include         fastcgi_params;
        fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass    backend;
    }
    

    其中 backend 是之前配置的上游,例如:

    upstream backend {
        server 127.0.0.1:9000;
    }
    

    VirtualHost example

    【讨论】:

    • 我已经添加了我原来的 vhost 配置文件以供参考。此答案不包含与重写相关的任何内容。
    • 这个答案包含index。您写了 我的问题是关于强制 www.example.com/index.php 重写为 www.example.com 而不是从 /index.php 重定向到 /。所以不需要重写规则,阅读更多关于HttpIndexModule
    • 请记住,使用 "location ~ \.php$ {" 通常是不安全的。见:wiki.nginx.org/Pitfalls#Pass_Non-PHP_Requests_to_PHP
    • 来自您的链接:正确的解决方案是在 php.ini 中设置 cgi.fix_pathinfo=0。但是您的问题与安全性无关,对吗?那么index 指令对你有帮助吗?
    • 我没有问这个问题,也没有对此发表评论。我只是想向你们两位指出,显示的配置通常是不安全的。
    猜你喜欢
    • 2023-04-04
    • 2020-11-08
    • 2012-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-01
    • 2018-12-25
    相关资源
    最近更新 更多