Quantcast
Channel: Nginx Forum - How to...
Viewing all articles
Browse latest Browse all 4759

index directive absolute vs relative

$
0
0
Hi!

Looks like a very basic issue, but..

What's the difference between absolute and relative paths for the index directive? The documentation says nothing about it:

Index defines files that will be used as an index. The file name can contain variables. Files are checked in the specified order. The last element of the list can be a file with an absolute path.

I have a server block with dynamic-only (php-fpm) content except for some always-requested files like favicon.ico & robots.txt where I don't control the requested location/domain. Everything static is served by another server block for another domain (s.example.org).

Note: though php & nginx are on the same server, nginx doesn't have access to the php-fpm scripts directory.


This example returns 403 when http://example.org/ is requested (log says: directory index of "/srv/nginx/empty/" is forbidden):

server {
listen <ip>;
server_name example.org;

access_log xxx
error_log xxx

root /srv/nginx/empty;

location = / {
index index.php;
}

location / {
return 404;
}

location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:socket;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
}

location ~ ^/(favicon\.ico|robots\.txt)$ {
root /srv/nginx/static;
try_files $uri =404;
}
}


Now, if I change "index index.php;" line to an absolute path (index /index.php;) nginx sends a 302 redirect to /index.php and then it's correctly served by php-fpm.

Also, if I create index.php file (empty or with any content) in /srv/nginx/empty, the example above works (with the relative path), but the file (index.php) is served by php regex location block, i.e. /srv/nginx/empty/index.php only serves for nginx to check that the file exists.

So, what's the difference in handling relative and absolute paths in index directive? Looks like it's the same with the only exception that relative path is checked for actual file existence and an absolute doesn't. And how to make it work with relative paths (e.g. example.org/dir_x/ -> php-fmp /dir_x/index.php; again, nginx doesn't have access to php scripts to check php script exists)?

Also, the documentation says:

It should be noted that using an index file causes an internal redirect, and the request can be processed in a different location.

Why the documentation says it's an internal redirect when the server performs an external redirect? Is it possible to perform an internal redirect with index or any other way around?

From the documentation, my understanding was that when the request is for an URI ending at /, then the index file (relative) is tried in the corresponding location block (in this case the one that has the php regex) and is served directly, without performing an external (302) redirect.


I take this opportunity to ask if the example above is a correct (with absolute path /index.php) server configuration for dynamic-only content (php.ini cgi.fix_pathinfo = 0)? Does it have any errors, security dangers or performance inefficiencies? Could it be rewritten in a more efficient way?


Thanks in advance,
Anatoli

Viewing all articles
Browse latest Browse all 4759

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>