![](/img/trans.png)
[英]How can I configure Nginx to route a specific request pattern to a specific php-fpm upstream hosting a Symfony application?
[英]How to route request url subfolder paths to specific php pages using nginx and php-fpm
我第一次使用本地 nginx 服務器來設置我正在構建的網站,但我在設置 nginx 配置以處理 Z572D4E421E5E6B9BC11D815E8A02711 方式時遇到了麻煩。 當用戶瀏覽網站時,我的網站提供多個 php 頁面。 最初使用本地 php 服務器開發站點時,我使用 GET 請求和 window.location.href 更改以進行站點導航。 例如:
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 的 state :
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 腳本來負責整個路徑,則需要這樣的配置:
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
}
雖然我寧願推薦一個更傳統、更干凈的項目結構。
在您的項目根目錄中創建兩個目錄: shop
和product
。 將shop.php
和product-page.php
移動到指定文件夾並將兩者重命名為index.php
。 此結構的 nginx 配置將如下所示:
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.