Justin Bramel
Justin Bramel

Reputation: 81

How to route request url subfolder paths to specific php pages using nginx and php-fpm

I'm using a local nginx server for the first time to set up a website i'm building and i'm having trouble setting up the nginx config to handle url requests the way I want. My website serves multiple php pages as the user navigates through the website. When developing the site initially using a local php server, I used GET requests with window.location.href changes for site navigation. For example:

http://localhost:8000/shop.php?filter=all&sort=id_asc&page=3

However, since its going to be an ecommerce website for a small business, I wanted to handle the URLs in a cleaner and more professional manner.

My site structure looks something like this:

Website:

->index.php  
->shop.php  
->about.php  
->product-page.php  
->/css/  
->/javascript/  
->/php/

I want to configure nginx to route url paths in the following way

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 

I've tried numerous suggestions over a couple of days before asking here but everything failed due to one reason or another, 404s, 500 internal errors, and redirect loops. I'm hoping to gain some inside here while I move onto other aspects of the site, so as to stop beating my head against the wall. Here is the state of my nginx conf at this moment:

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

}

How could I go about solving this? And if there is a better standard in structuring a website and its URLS please let me know. This is my first website and first time using nginx - so i'm a little naive on best practices.

Upvotes: 2

Views: 3230

Answers (1)

anemyte
anemyte

Reputation: 20196

If you need a certain php script to be responsible for a whole path, you need a config like this:

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
}

Although I would rather recommend a more traditional and cleaner project structure.

In your project root create two directories: shop and product. Move shop.php and product-page.php into designated folder and rename both to index.php. Your nginx config for this structure will be like this:

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

Upvotes: 4

Related Questions