nginx干净的URL与SEO友好的文件名

我希望实现以下在Apache下完美工作。 这是为了更好的search引擎优化的URL。

示例URLS:

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 

我真正期待的是一个位置正则expression式,可以捕获ROOT FOLDER中的所有无扩展php脚本,以便通过php-fptm进行处理。

在所有上面的URL中,“input”,“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”将variables传递给脚本以显示适当的内容。 虽然此url显示的是dynamic内容,但是url制作得相当像静态url,可以被search引擎轻松地编入索引。 这个variablesparsing已经在PHP完成了,与Nginx无关。 我相信这部分已经被称为漂亮的url/干净的url。

我需要知道如何在NGINX下实现这一点? 我需要正则expression式来处理ROOT FOLDER中的所有脚本(扩展名较less的文件),并忽略这些脚本名称之后的内容。 如果这样的文件不存在,那么考虑检查URL的其余部分,希望它成为一个有效的目录后跟一个文件名。 这个目录部分是可选的,对我目前的需要来说不是必需的。

我有一个运行ubuntu的VPS,我已经安装了php-fpm的nginx和普通的URL,像index.htm / index.php安装程序工作正常。 在正则expression式中,我不是一个职业,所以我在这个时刻就被卡住了。 我在许多nginx博客/论坛网上search,但找不到正确的解决scheme。

我正在使用PHP v5.3.6.13的Nginx v1.1.17的最新开发版本。 我还编译了额外的模块,比如更多的头文件,caching清除,memcache等。

任何帮助,将不胜感激。 提前致谢…

这应该为你工作:

 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; ... } } } 

您指的是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; }