Welcome! Log In Create A New Profile

Advanced

Basic question about single entry point - why my solution actually *works*?

Posted by konrados 
Basic question about single entry point - why my solution actually *works*?
May 19, 2018 11:45PM
Hi. I'm a newbie - started playing with nginx in the last days.

I'm trying to make a front controller (single entry point), i.e. . all the requests to non existing files/dirs should be forwarded to index.php, passing "stuff". My host is "http://point1.local" and I want http://point1.local/foo to be forwarded to http://point1.local/index.php (because 'foo' doesn't exist).

I have this right now (and it seems to work): https://paste.ngx.cc/03

That is (shortened):

server {
listen 80 default_server;
server_name point1.local;
index index.php;
root /var/www/html/point/point1;

location / {
try_files $uri $uri/ /index.php$is_args$args;
}

location ~ \.php$ {
try_files $uri =404;

# Bypass the fact that try_files resets $fastcgi_path_info
# see: http://trac.nginx.org/nginx/ticket/321
set $path_info $fastcgi_path_info;
fastcgi_param PATH_INFO $path_info;

include fastcgi.conf;

fastcgi_pass unix:/run/php/php7.2-fpm.sock;
}
}

But my question is - I've have read in at least two places, that only ONE 'location' catches, i.e. is "considered". For example here:
http://blog.martinfjordvald.com/2010/07/nginx-primer/ says "The most important things to note (...) and only one location block will ever be run."

Then... how this works? Because it clearly not only does the 'try_files' thing (first location block) , but it *is* also handled then by PHP engine (second location block).

Is it because the first one directs to index.php file and then, in contrast to the information I found, the second location block kicks in?
Well, I'll answer myself.

It seems that the sentence "and only one location block will ever be run" is a bit not precise, because nginx obviously can run more locations if we do any kind of 'rewrite', which try_files does (kind of). The same author in the same article even mentions this (this is related to a different example in the article):

(...) The one try files line means that it will first try accessing the full URI, which means that a static file request will end here. Secondly it will try the full URI plus a slash, thus looking for a directory. Finally, if none of these are found it will send the request to /index.php **and perform a new location match**, which will of course hit our PHP location and fastcgi_pass the request
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 135
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready