【问题标题】:nginx clean url with seo friendly file names带有 seo 友好文件名的 nginx clean url
【发布时间】:2012-04-08 17:35:43
【问题描述】:

我希望实现以下在 Apache 下完美运行的功能。这样做是为了更好地对 URL 进行 SEO。

示例网址:

http://www.astrogyan.com/enter/indian_astrology_horoscope_chart_prediction.html
http://www.astrogyan.com/know_your_gemstone/gID-7/sani_planet_saturn_gemstone_blue_sapphire_neelam.html

我真正期待的是一个位置正则表达式来捕获根文件夹中的所有无扩展名 php 脚本,仅供 php-fptm 处理。

在上述所有 URL 中,“enter”、“know_your_gemstone”都是 PHP 脚本,后面是 PHP 为 SEO 生成的虚拟文件名。实际上“indian_astrology_horoscope_chart_prediction.html”文件名不存在。在 Apache 中,我使用以下内容拦截“enter / know_your_gemstone”等,并且从不关心文件名的其余部分:

DefaultType application/x-httpd-php

在上述 URL 的最后,“gID-7”用于将变量传递给脚本以显示适当的内容。虽然此 URL 显示的是动态内容,但该 URL 经过精心设计,看起来像一个静态 URL,可以很容易地被搜索引擎索引。这个变量解析已经在 PHP 中完成,与 Nginx 无关。我相信这部分已经被称为漂亮的网址/干净的网址。

我想知道在 NGINX 下如何最好地实现它?我需要正则表达式来处理根文件夹中的所有脚本(扩展名较少的文件),并忽略这些脚本名称之后的内容。如果这样的文件不存在,则考虑检查 URL 的其余部分,希望它是一个有效的目录,后跟一个文件名。此目录部分是可选的,对我目前的需求不是必需的。

我有一个运行 ubuntu 的 VPS,我在其中安装了带有 php-fpm 的 nginx,对于像 index.htm / index.php 这样的普通 URL,设置工作正常。我不是正则表达式写作的专业人士,因此我被困在这个时刻。我在网上搜索了很多nginx博客/论坛,但没有找到合适的解决方案。

我使用的是最新开发版本的 Nginx v1.1.17 和 php v5.3.6.13。我还编译了其他模块,例如更多标头、缓存清除、内存缓存等。

对此的任何帮助将不胜感激。提前谢谢...

【问题讨论】:

  • 考虑修改并简化您的问题。

标签: php nginx


【解决方案1】:

这应该适合你:

server {
    listen 80;
    server_name example.com;
    root   /full/server/path/to/your/cms;
    index  index.php;

    location / {
        try_files $uri $uri/ /phphandler
    }

    location /phphandler {
        internal;
        # nested location to filter out static items not found
        location ~ .php$ {
            rewrite ^/([^/]*)(.*) /$1 break;
            fastcgi_pass   127.0.0.1:8080;
            ...
        }
    }
}

代理替代方案:

server {
    listen 80;
    server_name example.com;
    root   /full/server/path/to/your/cms;
    index  index.php;

    location / {
        try_files $uri $uri/ /phphandler
    }

    location /phphandler {
        internal;
        # nested location to filter out static items not found
        location ~ .php$ {
            rewrite ^/([^/]*)(.*) /$1 break;
            proxy_pass   127.0.0.1:8080;
            ...
        }
    }
}

【讨论】:

  • 修复了在命名位置嵌套的问题
  • 谢谢你,但真正的 404 文件怎么样?他们也会被/phphandler 处理吗?
  • 是的,该位置块将处理 404 个结果,如果需要,您可以为那里添加自己的东西。
【解决方案2】:

您指的是 mod_rewrite,CMS 通常使用它将看似静态的文件 URL 重写为 index.php 动态 URL。这可以在 nginx 中完成,也可以通过重写:

server {
    # port, server name, log config omitted

    location / {
        root   /path/to/your/cms;
        index  index.php index.html;  # replace index.php with your real handler php

        if (!-f $request_filename) {  # request is not a file
            rewrite  ^(.*)$  /index.php?q=$1  last;
            break;
        }
        if (!-d $request_filename) {  # request is not a dir
            rewrite  ^(.*)$  /index.php?q=$1  last;
            break;
        }
    }

    # sample fastcgi php config, to allow running of your index.php
    location ~ .php$ {
      fastcgi_pass   127.0.0.1:9090;
      fastcgi_index  index.php;
      fastcgi_param  SCRIPT_FILENAME  /path/to/your/cms$fastcgi_script_name;
      fastcgi_param  QUERY_STRING     $query_string;
      fastcgi_param  REQUEST_METHOD   $request_method;
      fastcgi_param  CONTENT_TYPE     $content_type;
      fastcgi_param  CONTENT_LENGTH   $content_length;
    }

    # static files
    location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico)$ {
        access_log        off;
        expires           30d;
    }

    error_page  404              /index.php;
 }

【讨论】:

    猜你喜欢
    • 2014-02-26
    • 1970-01-01
    • 1970-01-01
    • 2016-01-18
    • 2019-11-08
    • 2014-07-09
    • 1970-01-01
    • 2011-08-25
    相关资源
    最近更新 更多