如何使用nginx和php-fpm将请求url子文件夹路径路由到特定的php页面

如何使用nginx和php-fpm将请求url子文件夹路径路由到特定的php页面,php,nginx,url,configuration,fpm,Php,Nginx,Url,Configuration,Fpm,我第一次使用本地的nginx服务器来建立一个我正在构建的网站,但我在设置nginx配置以按照我想要的方式处理url请求时遇到了问题。我的网站在用户浏览网站时提供多个php页面。最初使用本地php服务器开发站点时,我使用GET requests with window.location.href changes进行站点导航。例如: http://localhost:8000/shop.php?filter=all&sort=id_asc&page=3 然而,由于它将成为一个小企业

我第一次使用本地的nginx服务器来建立一个我正在构建的网站,但我在设置nginx配置以按照我想要的方式处理url请求时遇到了问题。我的网站在用户浏览网站时提供多个php页面。最初使用本地php服务器开发站点时,我使用GET requests with window.location.href changes进行站点导航。例如:

http://localhost:8000/shop.php?filter=all&sort=id_asc&page=3
然而,由于它将成为一个小企业的电子商务网站,我想以更干净、更专业的方式处理URL

我的网站结构如下所示:

root /var/www/html/reagansrockshop; # root directive is necessary to define where '/' is
location /shop/ { # this means "all URLs starting with '/shop/' "
  index /shop.php; # be careful with path to the file here
}
server {
listen 80 ;
listen [::]:80 ;

server_name localhost;

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

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

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

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

location ~ \.php$ {
    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}
网站:

->index.php  
->shop.php  
->about.php  
->product-page.php  
->/css/  
->/javascript/  
->/php/
我想通过以下方式配置nginx来路由url路径

www.mywebsite.com -> routes to index.php  
www.mywebsite.com/shop -> routes to shop.php  
www.mywebsite.com/shop/anything -> routes to shop.php  
www.mywebsite.com/about -> routes to about.php  
www.mywebsite.com/product -> routes to product-page.php   
www.mywebsite.com/product/anything -> routes to product-page.php 
在这里询问之前的几天里,我尝试了许多建议,但由于各种原因,404、500个内部错误和重定向循环,一切都失败了。我希望在我进入网站的其他方面时,能在这里获得一些信息,这样我就不会把头撞在墙上了。下面是我的nginx conf此时的状态:

server {
listen 80 ;
listen [::]:80 ;

server_name localhost;

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

location / {
    try_files $uri $uri/ =404;
}

location = /shop {
    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    fastcgi_index shop.php;
    try_files $uri /shop.php;
}

location /shop/ {
    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    try_files $uri /shop.php;
}

location ~ \.php$ {
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}
}


我怎样才能着手解决这个问题呢?如果有一个更好的标准,在构造一个网站和它的网址,请让我知道。这是我的第一个网站,也是第一次使用nginx,所以我对最佳实践有点幼稚。

如果你需要某个php脚本来负责整个路径,你需要这样的配置:

root /var/www/html/reagansrockshop; # root directive is necessary to define where '/' is
location /shop/ { # this means "all URLs starting with '/shop/' "
  index /shop.php; # be careful with path to the file here
}
server {
listen 80 ;
listen [::]:80 ;

server_name localhost;

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

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

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

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

location ~ \.php$ {
    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}
尽管我宁愿推荐一个更传统、更干净的项目结构

在项目根目录中创建两个目录:
shop
product
。将
shop.php
product page.php
移动到指定的文件夹中,并将两者重命名为
index.php
。此结构的nginx配置如下:

root /var/www/html/reagansrockshop; # root directive is necessary to define where '/' is
location /shop/ { # this means "all URLs starting with '/shop/' "
  index /shop.php; # be careful with path to the file here
}
server {
listen 80 ;
listen [::]:80 ;

server_name localhost;

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

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

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

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

location ~ \.php$ {
    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}

简单有效的解决方案。这给了我一个良好的开端。非常感谢。