【发布时间】:2021-04-27 12:18:43
【问题描述】:
我第一次使用本地 nginx 服务器来设置我正在构建的网站,但在设置 nginx 配置以按照我想要的方式处理 url 请求时遇到了麻烦。当用户浏览网站时,我的网站提供多个 php 页面。在最初使用本地 php 服务器开发站点时,我使用了带有 window.location.href 更改的 GET 请求来进行站点导航。例如:
http://localhost:8000/shop.php?filter=all&sort=id_asc&page=3
但是,由于它将成为小型企业的电子商务网站,我希望以更干净、更专业的方式处理 URL。
我的网站结构如下所示:
网站:
->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;
}
}
我该如何解决这个问题?如果在构建网站及其 URL 方面有更好的标准,请告诉我。这是我的第一个网站,也是第一次使用 nginx - 所以我对最佳实践有点天真。
【问题讨论】:
标签: php nginx url configuration fpm