user2967920 user2967920 - 1 year ago 167
PHP Question

How do I use route and have same directory name as route in nginx?

I have files in the following structure

index.php
help.php
help\helpone.php
help\helptwo.php


If I visit something.com, I'm taken to the correct index page with the .php removed from the end. When I goto something.com/help, nginx throws a 403. This is happening because there is a help folder inside the same directory. The requirement is to visit something.com/help to see the help page

help page has links to the other pages, so on click it becomes something.com/help/helpone

I have the following server block that does the operation.

server {
root /usr/share/nginx/landing;
server_name sample.com;
index index.php index.html;

location / {
try_files $uri/ $uri.php$is_args$query_string;
}

location ~ \.php {
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_index index.php;
}
}


How can I achive sample.com/help without getting 403

Answer Source

So the solution to this issue was to move the help.php which was in the root directory to help\index.php This way Nginx will directly look inside folder help for index.php instead of conflicting between the help.php which was outside and the folder help doing a Forbidden Access.

Now going to sample.com/help goes directly into sample.com/help/index.php whithout showing you the index.php of-course.

Attaching nginx block for reference although nothing much changed.

index index.php index.html;

location / {
    try_files  $uri/ $uri.php;
}

location ~ \.php {
        fastcgi_pass 127.0.0.1:9000;
        include fastcgi_params;
        fastcgi_index index.php;
    }

Folder structure

 index.php
 help\index.php
 help\help1.php
 help\help2.php

Output sample.com goes to sample.com/index.php & doesn't show the /index.php so UX is also satisfied!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download