panique / mini

Just an extremely simple naked PHP application, useful for small projects and quick prototypes. Some might call it a micro framework :)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[documentation] Can anyone help with nginx rewrites?

opened this issue · comments

Great looking and educational framework but I'm really struggling here and would appreciate any passing nginxexpers having a look.

I've played with some other basic MVC frameworks and never had the problem where a normal nginx try_file wouldn't work.

So, for example:

location /manage/login {
     try_files $uri $uri/ /manage/login/index.php;
}

But, of course, I can see from the .htaccess and application.php that this uses ?url= GET arguments.

So, 5 hours of reading http://wiki.nginx.org/HttpCoreModule#.24arg_PARAMETER and http://wiki.nginx.org/HttpCoreModule#.24args and http://nginx.org/en/docs/http/converting_rewrite_rules.html I've tried various of the following.... (the install folder is in manage/login

location /manage/login {
try_files $uri $uri/ /manage/login/index.php?url=$uri;
}

also

try_files $uri $uri/ /manage/login/index.php?url=$uri;
try_files $uri $uri/ /manage/login/index.php?url=$args;
try_files $uri $uri/ /manage/login/index.php?url=$uri$args;
try_files $uri $uri/ /manage/login/index.php?$args;

and other combinations of the above. No matter what I put, it always says:

You are in the controller home, using the method index()
You are in the View: application/views/home/index.php

I found another lost soul at http://forum.nginx.org/read.php?2,247614,247647#msg-247647 but again, he wasn't having much luck.

Hey, just for your info: This script uses the most used htaccess in the world, the typical "beautiful URLs" rewrite rules. No weird stuff, no stunts. I even think Wordpress etc. use exactly the same.

Btw maybe Nginx is not what you should use if this brings you in trouble.

Hi Panique; thanks for your reply.

Indeed it does seem to use the standard URL routing, which is why I can't figure out why it isn't working.

As for not using Nginx because of the trouble, I'd rather try to find a solution than turn back to Apache, which I detest. I've been using Nginx exclusively since 2010 and it is running phpbb3, Wordpress & phpmyadmin, also CodeIgniter first, then FuelCMS, and now PyroCMS.

When I return from work I'll post the full rewrite logs into the nginx and Howtoforge forums to see what they can come up with. Would you mind keeping this open for just one more day, then I can refer back to it, maybe get some more traffic and interest in the project too, and I promise to post any solution here to help others!

Thanks.

He again,

can you post more info / error logs ? Btw I noticed that you are using a custom folder structure. The php-mvc is build for usage in webroot or in a simple subfolder, so your use case might be special. The php-mvc is btw made for beginners, I think you're quite professional, maybe you don't need he php-mvc. :)

I think this ticket is overcomplicating things. The php-mvc is very basic and uses the most standard rewrite rules, and there's no need to install this in nginx in a multi-folder deep nesting structure usually. Running this from two-levels deep subfolders if definitly totally out of the scope of this script, so let's close the ticket.

Sorry, I have been away for a week, but I have just had a look and by enabling the debug lines, I can quickly see the solution, which is to change rtrim to trim in line 74 of application/libs/application.php.

This then makes it compatible with the very popular and fast growing nginx server, without breaking compatibility with Apache. I've contributed the changes and submitted a pull request.

The alternative way instead of changing rtrim to trim is the do the following:

in the main nginx.conf, eg: /etc/nginx/nginx.conf
add the following to the http stansa:

    map $request_uri $request_trimmed {
        default "";
        ~^/(?P<trim>.+)$ $trim;
    }

then in the server section of the site config file, change

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

to

location / {
    try_files $uri $uri/ /index.php?url=$request_trimmed&$args;
}

You can see which is easier :)

Hope this helps others. Thanks.

Okay this is a often requested feature, let's create a guideline / native integration for this project! Thanks for the comments so far, good stuff in here!

Hello. I am also having a problem getting this to work on nginx (and switching to Apache is not a realistic option.) I have cloned the repository to /var/www/php-mvc/
I have made the following changes. 1)modified this line of config.php:
define('URL', 'http://XXX.XXX.XXX.XXX/php-mvc/');
where the URL is the ip address of my test DO server.
2) made the change digitaltoast recommended to /etc/nginx/nginx.conf
3) made change digitaltoast recommended to /etc/nginx/sites-available/default
4) restarted the server

when I go to http://XXX.XXX.XXX.XXX/php-mvc/ the homepage shows up perfectly. But, when I click on the links I get a message saying "file not found."

I'm not sure how to troubleshoot this.

Hi @stevehmig - did you try the first, preferred and easier method?

Which is:

change rtrim to trim in line 74 of application/libs/application.php.

in nginx.conf in your location block, put:

try_files $uri $uri/ /index.php?url=$uri;

and that's it. Just revert those other changes - they are confusing things because at first I could not persuade the author to change rtrim to trim so I had to find a more complicated alternative.

If you're still stuck, I'll send you my complete nginx config file :)

I changed application.php line 74 to trim, I changed my nginx.conf back to the original. I have this in my /etc/nginx/sites-available/default file:
location / {
index index.html index.htm index.php;
try_files $uri $uri/ /index.php?url=$uri;
}
I believe I'm modifying the correct file (/etc/nginx/sites-available/default). If not let me know.
If it's no trouble, I wouldn't mind looking at your nginx.config file. Thanks!

Sure, @stevehmig - here's my config:

server {
    listen       80;
    server_name phpmvc.xxxx.co.uk;
    }

root   /var/www/php-mvc;

location / {
    try_files $uri $uri/ /index.php?url=$uri;
    }

location ~ \.(php)$ {
    fastcgi_pass   unix:/var/run/php5-fpm.sock;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
     }
}

Also, in \php-mvc\application\libs\application.php uncomment the debugging lines at the end so that they look like this:

//for debugging. uncomment this if you have problems with the URL
echo 'Controller: ' . $this->url_controller . '<br>';
echo 'Action: ' . $this->url_action . '<br>';
echo 'Parameters: ' . print_r($this->url_params, true) . '<br>';

Doing this REALLY helped me a lot!

Also, don't forget to be restarting/reloading your nginx config as the root user - in fact, you can test it first with

sudo nginx -t

Thanks, I got it working!
I changed root /var/www; to:
root /var/www/php-mvc;
and in config.php changed:
define('URL', 'http://XXX.XXX.XXX.XXX/php-mvc/');
to:
define('URL', 'http://XXX.XXX.XXX.XXX/');

I'm not sure if there is a way to get it working my original way, as I have other projects in /var/www/ that I'd want to work on simultaneously. Perhaps this is bad practice, though?

Thanks again @digitaltoast

You SHOULD be able to just change root back to /var/www and the URL define back to how you had it, but you see where it says

location / {
    try_files $uri $uri/ /index.php?url=$uri;
    }

Try changing that to

location /php-mvc/
(...)

(assuming that you have your php-mvc in that folder, of course! :)

Awesome stuff!

I just made php-mvc work on Mac OS X 10.10 Beta 4 with NGINX 1.6.0, PHP 5.5.14, PEAR 1.9.5 and MySQL 5.6.16.

I followed @digitaltoast instructions. Only difference is that I have my sites separated from the main nginx.conf file inside the sites-available folder.

Here is my /sites-available/phpmvc.com configuration file:

root       /var/www/phpmvc; 
server_name  phpmvc.com;
index   index.html index.htm index.php;
rewrite_log on;
error_log    /usr/local/etc/nginx/logs/phpmvc.error.log;
access_log  /usr/local/etc/nginx/logs/phpmvc.access.log;

location / {
    try_files $uri $uri/ /index.php?url=$uri;
}

location ~ \.(php)$ {
    try_files      $uri = 404;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include        fastcgi_params;
}

Added "127.0.0.1 phpmvc.com" to my /etc/hosts file.

Cool stuff guys! I'll implement this right into the project in September or October (when there's time!). Good to see that the project is useful for you all :)

FYI: #59 has been just merged into develop branch.

Heyyyy people,

I've just released MINI (=php-mvc) in a new version with a different structure (index.php is now in /public, plus diffrenent .htaccess). Would be nice if you could test this and post a working nginx config for this.

Big thanks!

Great - sounds very exciting! Yes, I'll take a good look and test ASAP, but may not be today.

Hi @panique - project looks good. It a bit different than I've seen before by having the index file in a subfolder, and may confuse some people used to "index at the top" setups.

However, it's not a problem and after a bit of fiddling, this seem to work for very well for me on a "real" server to be sure (not local dev, obviously my hosts file has been changed for this to work)

server {
    server_name test.mini.com _;
    listen      [::]:80;
    listen      80;

    root /var/www/test.mini.com/mini;

    location / {
                index /public/index.php;
                try_files /public/$uri /public/$uri/ /public/index.php?url=$uri;
                }

    location ~ \.(php)$ {
                fastcgi_index  index.php;
                include fastcgi_params;
                fastcgi_pass unix:/var/run/fpm.sock;
                }   
}

Short and seet - Anyone else come up with anything better?

commented

Good news: There's now a nginx config right in the README :)