Ngin,url:/ search =>运行php /search.php和url:/ my => php my.php

请帮我configurationNginx:

1)我有一些PHP脚本,我想运行他们的URL没有“.php”例如

  • url / search – >运行脚本“/search.php”
  • url / bookmark – >运行脚本“/bookmarks.php”
  • url / – >运行脚本“/index.php”

2)404 / asdfasdfasdf – 任何不匹配任何指定的位置,不会导致静态文件(图像,CSS)应该运行/not-found.php保存/ asdfasdfasdf在URL

当前服务器节根目录/ var / www / site / public; index index.php;

location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } location ~ \.(ico|css|js|jpe?g|png)$ { expires 7d; add_header Pragma public; add_header Cache-Control "public, must-revalidate, proxy-revalidate"; } 

我使用重写来完成

 location /abcd { rewrite ^/abcd\/([0-9a-zA-Z_\-\s\+]+)\/([0-9a-zA-Z_\-\s\+\(\)\.]+)$ /efgh.php?action=whatever; } location /xyz { rewrite ^/yxz\/([0-9a-zA-Z_\-\s\+]+)$ /rst.php?action=whatever; } 

有可能是一个更通用的方式,但如果你有一个有限的url,这可能工作。 这可能会使您走上正确的轨道。 我可能会看正则expression式,捕获组和expression式。

有一些实现无扩展PHP的通用模式。 一个这样的模式是这样的:

 index index.php; location / { try_files $uri $uri/ @php; } location @php { rewrite ^ $uri.php last; } location ~ \.php$ { try_files $uri =404; ... }