Nginx rewrite rule to PHP that also allows .php in the URI -


A little unusual question, hopefully with a simple answer! (I am new to NGN)

I have an old PHP system running on Apache and I would like to bring it on NGNX, but my point is that some call it a single Handler should be rewritten on file ( /handler.php ) and want to execute some actual files from it It seems that almost all the routes end in .php, whether they refer to the actual PHP file or not.

For example, /foo.php might be a real file that executes its code, but / bar.php does not exist And therefore wants to call /handler.php . There are also examples of routes of / bar (without the .php extension) that want to call /handler.php .

There are lots of types in the system (so far, I want to manually code manually). What is the solution in Nginx?

There is currently something like this in the server block:

  location / {try_files $ url / $ /handler.php$is_args$ arg; } Include /etc/nginx/sites.d/*.conf;  

and sites.d / php.conf currently shows something like this:

  location ~ \ .php $ {fastcgi_pass unix: / var / run / Php5 -fpm.sock; Fastcgi_index index.php; Fastcgi_param SCRIPT_FILENAME $ document_root $ fastcgi_script_name; Include /etc/nginx/fastcgi_params; }  

But it treats all the roots with the .php extension as the actual files and only the standard "does not specify an input file." T is present (no rewriting does not). There is no problem if there is no .php extension, then they call without call / handler.php.

So in summary, with almost the default setup:

  /foo.php - works (actual file) / bar.php - failed (no file ) / Bar - works (no file)  

If I had only "no-file" type that I could update to Php.conf "location ~ Something like "handler.php $" , but in this case it means that all the actual .php files just trigger a download (ie /foo.php failed).

Any help is appreciated! thank you in advanced.

You can test if your location block matches .php Actually exists and redirect to handler.fp if it is not there:

  location ~ \ .php $ {if (! -f $ request_filename) { Rewrite ^. * \. Php $ /handler.php final; } Fastcgi_pass unix: /var/run/php5-fpm.sock; Fastcgi_index index.php; Fastcgi_param SCRIPT_FILENAME $ document_root $ fastcgi_script_name; Include /etc/nginx/fastcgi_params; Optional location rule (as suggested by OP) using  

updated example

try_files :

  location ~ \ .php $ {try_files $ uri / handler.php $ is_args $ args; Fastcgi_pass Unix: /var/run/php5-fpm.sock; Fastcgi_index index.php; Fastcgi_param SCRIPT_FILENAME $ document_root $ fastcgi_script_name; Include /etc/nginx/fastcgi_params; }  

rewrite with the first version you can replace with regex matching but try_file I think The file is the recommended method of testing for existence. Thanks for the OP to suggest a better option.


Comments

Popular posts from this blog

sqlite3 - UPDATE a table from the SELECT of another one -

c# - Showing a SelectedItem's Property -

javascript - Render HTML after each iteration in loop -