Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
5.6k views
in Technique[技术] by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...