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;
}
Related
I have a custom PHP application which follows the basic MVC pattern of development. The applications directory structure is as follows:
admin/
default/
index.php
In the admin is another index.php (which is how it handles requests to /admin).
In apache, this worked by putting a .htaccess file in each directory (admin and doc root) setting up a rewite and the application works. In NGINX, it doesnt seem so simple.
I can get the basic "default" application to work, by using this in my nginx.conf:
if (!-e $request_filename)
{
rewrite ^(.+)$ /index.php?path=$1 last;
}
(we run on vhosts so I cannot reference location / or I get a duplicate rule error).
This gets the frontend application to work, but when I try to access the /admin portion of the application the login screen loads, but when I try to submit and it tries to hit the endpoint 'admin/index/index' it fails, as my rewrite rule doesnt work. Here is what I have for the rewrite in NGINX:
location /admin {
try_files $uri $uri/ /admin/index.php?path=$uri&$args;
}
I think the issue is that the $uri being passed in is /admin/index/index instead of it being what it should be and is under Apache: /index/index.
Can anyone help me correct these NGINX rules so that my application works properly?
Thanks in advance.
As you said, you pass in the extra $uri. Try this:
location /admin {
try_files $uri $uri/ /admin/index.php?$args;
}
I've created a simple php file to display info fetched from my MYSQL database. Right after that i use a rewrite rule in Nginx to make the link seo friendly and to mask the .php file that fetches it.
Ex: http://localhost/myfile.php?seo=how-to-install-linux
After rewrite rule the i can access it like:
http://localhost/how-to-install-linux
My rewrite rules are:
location / {
rewrite ^/([a-zA-Z0-9_$\-]+)$ /myfile.php?seo=$1 last;
rewrite ^/(.*)/$ /$1 permanent; <- just to block trailing slash
}
My problem is that i also want to block any direct access to my php file and i want only the seo friendly url to work.
location = /myfile.php {
deny all;
}
This rule blocks complete access to my php file, including through seo friendy url.
Is there a way to make it work for the seo friendly version using NGINX?
My other settings are:
location ~ \.php$ {
try_files $uri =404;
include fcgi.conf;
fastcgi_pass unix:/var/run/ajenti-v-php7.0-fcgi-drnou-php7.0-fcgi-0.sock;
}
I only use Nginx, no Apache installed.
You can use the internal directive to prevent a location from being accessed directly. See this document for details.
For example:
location = /myfile.php {
internal;
include fcgi.conf;
fastcgi_pass unix:/var/run/ajenti-v-php7.0-fcgi-drnou-php7.0-fcgi-0.sock;
}
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.
I'm in the midst of migrating over to Nginx, from Apache.
I'm currently using a custom content management solution that utilizes the SERVER['request_uri'] to handle routing.
What I am trying to do is redirect all non-existing files & directory requests to /index.php, and not update the clients uri. However, when a file does exist, I want to return that instead.
An example url would be:
localhost/content/page/1 <- Should populate $_SERVER['request_uri'] to be /content/page/1
Or
localhost/public/script/exists.js <- Should be returned as an actual file.
You need to add a location / block or update your current location / block in your nginx vhost file.
This will redirect all request to the index.php if the file or directory is not found:
location / {
try_files $uri $uri/ /index.php;
}
This goes inside your server directive, for more information visit http://wiki.nginx.org/HttpCoreModule
After you modify your vhost file you need to restart nginx
Note: The try_files directive for server blocks was added in 0.7.44
Basically, the htaccess I use with apache says something like "if the requested file or directory does not exist, route the request through index.php"
How exactly can I do this with nginx?
That way, if a stylesheet is requested, it's served. But if the url isn't to a file on disk, then it should run the framework.
The way I do this, is to ignore certain extensions...
The configuration bellow runs everything except gif/jpg etc... through modify.php
location ~* \.(gif|jpg|jpeg|png|js|css|pdf)$ {
root /home/site/public_html;
expires 365d;
}
location / {
root /home/site/public_html;
index index.php index.html index.htm;
rewrite ^/(.*) /modify.php?file=$1;
expires 5m;
}
You can also test for the existance of a file with -f (though I prefer to avoid the extra stat call). The example bellow passes requests for missing files through to a proxy:
if (!-f $request_filename) {
break;
proxy_pass http://127.0.0.1;
}