Route all sub-locations to index.php with NGINX - php

I'd like to parse the URL in my php script, therefor I need everything from a specific location to route to index.php.
However, I can only see the specific php file when navigating directly to the file.
Example, the requests;
mycomain.ext/api
mycomain.ext/api/user
mycomain.ext/api/user/19
mycomain.ext/api/user/19/detail
should all route to api/index.php
Current config:
location /api {
root /usr/share/nginx/php/;
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?$query_string;
}

Yes, because it points to /index.php and not to /api/index.php as intended. Just change your try_files to try_files /api/index.php?$args =404;. And have a dedicated /api/index.php location where you forward the request to your php-fpm.
Native there is no $query_string variable, there is only the $args variable to pass all arguments. The variable $request_uri should not be used, only if it is encoded correctly. Your php script should use explode('?', $_SERVER['REQUEST_URI'], 2)[0] to handle the correct requests.

Related

nginx try_files location Configuration?

I am in the process of writing a directory listings script in Slim 3 Framework. I am having a problem that Slim 3 is not getting the request for a file download when it exists due to the 'try_files' directive in the configuration. Once I change the order of the 'try_files' it then fails to process any .php file for that matter.
Here is the current block of the configuration:
location / {
try_files $uri /index.php$is_args$args;
}
this was the tried configuration (and failed - it would serve up a .bin file with the contents of my index.php instead of processing it):
location / {
try_files /index.php$is_args$args $uri;
}
Ultimately, I want my Slim 3 script to capture the request and do something with it instead of simply serving it up by the web server.
If I rewrite all files to the index.php page as a parameter, I get my desired output. Here is the nginx configuration code:
location / {
rewrite ^(.*)$ /index.php?$1 last;
}

How do I get NGINX to properly rewrite and execute on a custom PHP application?

We have a custom PHP application that we wrote and runs on Apache with .htaccess files to handle the url rewrites. We are trying to convert it to work under NGINX with FPM under Plesk Onyx.
The application generates links like:
https://somedomain.com/mypage (same as index/mypage)
https://somedomain.com/index/sitemap
https://somedomain.com/blog/some-article-name
These URL's map to index.php files that take the request_uri and use it to render the page responses.
The structure of the application is nested as follows:
docroot (/)
./index.php //handler for the request in /
./blog/index.php //handler for any request to /blog
Each index.php expects to receive a ?path={request_uri} so that it can map the request to the controllers and actions.
I have tried multiple ways to get NGINX to do this using tryfiles and rewrite, but no luck. Using rewrite I can get / to work, but it wont render /mypage or /index/sitemap.
If I try to hit /index/sitemap it downloads the index.php instead of executing it, and if I try the blog the same thing happens. In fact the only path that works is /, all others just download the index.php file.
Here is my configuration as it is now, where am I going wrong?
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
expires 30d;
add_header Pragma public;
add_header Cache-Control “public”;
try_files $uri #fallback;
}
location / {
#index index.php index.html index.html;
rewrite ^/([^?]*) /index.php?path=$1 break;
rewrite ^blog/([^?]*) /blog/index.php?path=$1 break;
#try_files $uri #fallback;
}
Your configuration has multiple issues. I will ignore the first location block as it seems to have nothing to do with your question.
The first rewrite will always match, so the second rewrite will never be consulted. The second rewrite will never match anyway, as nginx URIs always begin with a /. The [^?] is meaningless, because rewrite uses a normalised URI which does not include the ? or query string. Using rewrite...break means that the rewritten URI is processed within the same location, which is an error as this location is not equipped to process PHP files. See this document for more.
A solution using try_files might look like this:
location / {
try_files $uri $uri/ /index.php?path=$uri&$args;
}
location /blog {
try_files $uri $uri/ /blog/index.php?path=$uri&$args;
}
location ~ \.php$ { ... }
See this document for more.

How to configure nginx rewrite rules for fuelphp framework?

Ive faced this problem when trying to execute an action from a controller in fuelphp framework, I get an 404 message from nginx. Im able to see, for e.g. localhost/index.php or just localhost, but when I try to access to an action-controller like localhost/index.php/login/huehue I get the 404 error. Can anyone help me? this app is currently working in apache, I was facing this trouble here too but everything got fine when I executed
a2enmod rewrite
then I tried to search for equivalent config for nginx and I found this like:
location /{ try_files $uri $uri/ /index.php?$args
/index.php?q=$request_uri }
or this:
location /{
rewrite ^ /index.php?/$request_uri;}
but they didnt work for me. Ive spent several hours trying to find out the reason. This is my actual vhost file config for my site:
server {
listen 80;
listen [::]:80;
root /var/www/nginx/goutmeet;
index index.php index.html index.htm index.nginx-debian.html;
server_name goutmeet.local www.goutmeet.local;
location / {
try_files $uri $uri/ /index.php?$args /index.php?q=$request_uri #handler;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
location #handler {
rewrite ^ /index.php?/$request_uri;
}
}
Id love to know how to solve this since using nginx sometimes is a better option than apache, and having this issue with fuelphp framework and not being able to use this two great tools together is awful.
Thanks in advance.
The try_files directive can have one default action. You have three! See this document for more.
Choose one:
try_files $uri $uri/ /index.php?$args;
try_files $uri $uri/ /index.php?q=$request_uri;
try_files $uri $uri/ #handler;
I do not know which is the appropriate default action for your application. They all send the request to the PHP controller, but with different sets of parameters.
The first case passes the query string; the second case passes a single parameter containing the request URI; and the third case invokes the rewrite in the named location.
I found a workaround for this problem, just add this line to config file:
error_page 404 /index.php;
I know is not the best solution but its been the only thing that has worked for me. In my opinion its acceptable since all routes should be managed by the framework and not by the web server.
Hope this helps someone.

how to make nginx rewirte url from one php to another php

I have a old web application which use get.php to interact with client app. now we upgraded our web application to use laravel framework, which use restful api as url interface,
and our server use nginx, so I want to redirect old url to new url like this:
/get.php?update => /update or (index.php/update)
I have tried with these config.
location = /get.php?update {
try_files $uri $uri/ /index.php/update;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
}
but it seems when I access get.php, I still get the 404 error.
So how can I redirect this using nginx's rewrite?
Nginx location block doesn't match query string, so your try will fail.
Try this:
location = /get.php {
if ($args = update) {
rewrite ^ /index.php?$query_string last;
}
}
But IF is kinda evil.

How to get the right try_files $uri directive working with _GET parameters in nginx

I just migrated a site from apache to nginx, and am very pleased so far. However, the server doesn't seem to recognize a $_GET parameter.
I've read that the answer is to change the try_files directive to:
location / {
try_files $uri $uri/ /index.php?$query_string;
}
But this isn't working for me. I suspect it's because the query string I'm using is a few directories in, with the url something like:
http://www.mysite.com/thedirectory/thefile?sortby=director
I just can't tweak the try_files directive to work in that instance, and the documentation seems sparse or obsolete.
Any ideas? If I don't get this resolved, I'm going to have to go back to Apache.
I don't know where you got that answer from,but in the context of your problem I don't see the relevance. Let's first get something clarified:
Try_files looks to match the URI with a physical location on disk and allows you to control the fallback action.
The default fallback is to throw a 404.
$query_string is not relevant to the matching process. It is used for try_files constructs where something has to be added to the query string.
There are three possible causes and remedies for your problem:
The uri matches a real file, but without the file extension, which causes the php processing location to not get triggered. In this case your statement should be:
try_files $uri $uri/ $uri.php;
This is a virtual location and the router is in index.php, like it it's with many applications, like WordPress and Magento.
In this case the try_files should be:
try_files $uri $uri/ #appname;
Without more context providing a location block for #appname is not possible.
You are not including relevant fastcgi_param directives. In this case your try_files is noise. Fix the actual problem first, by including the provided example fastcgi_param, as mentioned in the comments.

Categories