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;
}
Related
I want to have /blog directory on my application to have an instance of WordPress installed there. Currently /blog does takes me there, but I do want to know and have control, instead of directory how to read from routes.php.
There can be many use cases for it as blog directory is under maintenance or its down, in that case a simple change so it starts reading /blog from routes.php.
I am not much expert of '.htaccess' file but can we do from it?
if your CI application use the default routes configuration, then /blog will make CI try to load blog controller and index action, if it can't find them. you'll get 404 page not found error.
try to config the /blog in your web server configuration. add /blog block then apply wordpress server config there. make sure that request will find /blog location block before the / location block.
in nginx:
location /blog {
# This is cool because no php is touched for static content.
# include the "?$args" part so non-default permalinks doesn't break when using query string
try_files $uri $uri/ /index.php?$args;
}
# then your ci / location
location / {
# Check if a file or directory index file exists, else route it to index.php.
try_files $uri $uri/ /index.php;
}
I have to support one old project which uses Yii1. I get very strange behavior. Pretty URL is configured.
When URL doen't have a trailing slash (e.g. /about or /blog/post/5) everything goes OK (the route is correct and the correct page is displayed). But the same URLs with trailing slash (/about/ or /blog/post/5/) make a wrong route. Disregarding of url I always get the route 'main/index' and index page is displayed (not redirected to, but displayed at all URLs with trailing slash).
Any ideas?
I had the same issue and the problem was in nginx config. Yii's pretty URLs require proper config to work. This was strange but the same config worked fine on one server and produced the described behavior on another server.
The problematic config:
location ~ ^(.+\.(js|css|jpeg|jpg|gif|png|ico|swf|mp3|html|eot|woff|ttf|otf|svg|zip|pdf|xml))$
{
rewrite ^(.*)/$ $1 permanent;
try_files $uri /index.php?$args;
}
The working config:
location /
{
index index.php;
if (!-e $request_filename)
{
rewrite ^/(.*) /index.php?r=$1 last;
}
}
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.
For all pages in my codeigniter app except my default controller, main.php, when I refresh the browser the url isn't affected as one would expect.
However when I refresh the browser at "http://localhost/main", the main part is stripped off the url. So the browser bar shows just "http://localhost".
Totally lost on where to start with this but was just wondering if anyone has come across this before...?
Here's what I think could be the relevant part of my nginx.conf (if Nginx is the problem).
if ($request_uri ~* ^(/main(/index)?|/index(.php)?)/?$)
{
rewrite ^(.*)$ / permanent;
}
Note that changing the bracketed code to this:
rewrite ^(.*)$ /main permanent;
results in the error message The webpage at http://localhost/main has resulted in too many redirects.
Ok I answered my own question. Simply commenting out the nginx.conf code that I showed corrects the problem.
So just do this (says Bo Jackson):
#if ($request_uri ~* ^(/main(/index)?|/index(.php)?)/?$)
#{
# rewrite ^(.*)$ / permanent;
#}
If your trying to route your request to the index.php bootstrap so you can remove the index.php from your url in CodeIgniter you probably should try using this:
location / {
try_files $uri $uri/ /index.php?$query_string;
}
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