Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Multi Module App Nginx issue

Hi guys,

I got an issue putting online a Multi Module Phalcon App on my Debian Wheezy VPS using Nginx 1.2.1, php-fpm and Mariadb. When I try to access my website, i got an "This webpage has a redirect loop" error.

This is my nginx conf file properly symlink in site-enabled:

server {
        listen  80;
        server_name l-huitre-pedagogique.com www.l-huitre-pedagogique.com;
        root /var/www/l-huitre-pedagogique.com/public;
        set $root_path '/var/www/l-huitre-pedagogique.com/public';
        try_files $uri $uri/ @rewrite;

        location @rewrite {
            rewrite ^/(.*)$ /index.php?_url=/$1;
        }
        index index.php;
        location ~ \.php$ {
                       try_files $uri =404;
                       root /var/www/l-huitre-pedagogique.com/public;
                       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;

        }
        location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
            root $root_path;
        }
        error_log /var/log/nginx/test.lan-error.log;
}

this is my public/index.php:

<?php

use Phalcon\Mvc\Router,
Phalcon\Mvc\Application,
Phalcon\DI\FactoryDefault;

$environment = getenv("APPLICATION_ENV");

define("SITE_NAME", "L'Huître Pédagogique");
define("SEND_MAIL", true);
if ($environment == "development")
{
    define("DOMAIN_NAME", "http://hp.local");
    define("MAIL_ADMIN", "[email protected]");
    define("MAIL_FROM_MAIL", "[email protected]");
    define("MAIL_FROM_NAME", "Thanatos");
}
else
{
    define("DOMAIN_NAME", "http://www.l-huitre-pedagogique.com");
    define("MAIL_ADMIN", "[email protected]");
    define("MAIL_FROM_MAIL", "[email protected]");
    define("MAIL_FROM_NAME", "L'Huître Pédagogique");
}

$di = new FactoryDefault();

//Specify routes for modules
$di->set('router', function () {

    $router = new \Phalcon\Mvc\Router\Annotations(\FALSE);
    $router->removeExtraSlashes(\TRUE);
    $router->setUriSource(
    \Phalcon\Mvc\Router\Annotations::URI_SOURCE_SERVER_REQUEST_URI);

    $router->setDefaultModule("www");
    $router->addModuleResource("www", "Multiple\Apps\WWW\Controllers\Index", "/");
    $router->add("/", array(
    'module' => 'www',
    'controller' => 'index',
    'action' => 'index',
    ));

    $router->notFound(array(
        "controller" => "index",
        "action" => "index"
    ));

    $router->add("/admin/", array(
        'module' => 'admin',
        'controller' => 'index',
        'action' => 'index',
        ));
    $router->add("/admin/:controller", array(
        'module' => 'admin',
        'controller' => 1,
        ));
    $router->add("/admin/:controller/:action", array(
        'module' => 'admin',
        'controller' => 1,
        'action' => 2,
        ));
    $router->add("/admin/:controller/:action/:params", array(
        'module' => 'admin',
        'controller' => 1,
        'action' => 2,
        'params' => 3,
        ));
    $router->add("/admin/page/{page:[0-9]+}", array(
        'module' => 'admin',
        "controller" => "page",
        "action" => "index",
        ));
    $router->add("/admin/mailbox/{page:[0-9]+}", array(
        'module' => 'admin',
        "controller" => "mailbox",
        "action" => "index",
        ));
    $router->add("/admin/image/{page:[0-9]+}", array(
        'module' => 'admin',
        "controller" => "image",
        "action" => "index",
        ));
    $router->add("/admin/album/{page:[0-9]+}", array(
        'module' => 'admin',
        "controller" => "album",
        "action" => "index",
        ));

    $router->add("/page", array(
        'module' => 'www',
        "controller" => "index",
        "action" => "list",
        ));

    $router->add("/page/{page}", array(
        'module' => 'www',
        "controller" => "index",
        "action" => "page",
        ));
    $router->add("/album", array(
        'module' => 'www',
        "controller" => "index",
        "action" => "album",
        ));

    $router->add("/album/{album}", array(
        'module' => 'www',
        "controller" => "index",
        "action" => "albumpage",
        ));

    $router->add("/category/{category}", array(
        'module' => 'www',
        "controller" => "index",
        "action" => "category",
        ));
    $router->add("/category/{category}/{page}", array(
        'module' => 'www',
        "controller" => "index",
        "action" => "categorypage",
        ));

    $router->add("/about", array(
        'module' => 'www',
        "controller" => "index",
        "action" => "about",
        ));

    $router->add("/sitemap", array(
        'module' => 'www',
        "controller" => "index",
        "action" => "sitemap",
        ));

    $router->add("/bulletin", array(
        'module' => 'www',
        "controller" => "index",
        "action" => "bulletin",
        ));

    $router->add("/calendar/{year:[0-9]+}/{month:[0-9]+}", array(
        'module' => 'www',
        "controller" => "calendar",
        "action" => "index",
        ));

    $router->add("/calendar", array(
        'module' => 'www',
        "controller" => "calendar",
        "action" => "index",
        "year" => date('Y'),
        "month" => date('m')
        ));
    $router->add("/calendar/", array(
        'module' => 'www',
        "controller" => "calendar",
        "action" => "index",
        "year" => date('Y'),
        "month" => date('m')
        ));

    $router->add("/calendar/{year:[0-9]+}/{month:[0-9]+}/{day:[0-9]+}", array(
        'module' => 'www',
        "controller" => "calendar",
        "action" => "event",
        ));

    return $router;
});

//Set the database service
$di->set('db', function() {
    return new \Phalcon\Db\Adapter\Pdo\Mysql(array(
        "host" => "localhost",
        "username" => "epi",
        "password" => "secret",
        "dbname" => "hp",
        "options" => array(
            PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'
            )
        ));
});

//Start the session the first time when some component request the session service
$di->setShared('session', function() {
    $session = new Phalcon\Session\Adapter\Files();
    $session->start();
    return $session;
});

//Set up the flash service
$di->set('flashSession', function() {
    return new \Phalcon\Flash\Session(array(
        'error' => 'alert alert-danger',
        'success' => 'alert alert-success',
        'warning' => 'alert alert-warning',
        'notice' => 'alert alert-info',
        ));
});

//Set up a base URI so that all generated URIs include the "tutorial" folder
$di->set('url', function() {
    $url = new \Phalcon\Mvc\Url();
    $url->setBaseUri('/');
    return $url;
});

Phalcon\Tag::setDocType(Phalcon\Tag::HTML5);

try {

    //Create an application
    $application = new Application($di);

    // Register the installed modules
    $application->registerModules(
        array(
            'www' => array(
                'className' => 'Multiple\WWW\Module',
                'path' => '../apps/www/Module.php',
                ),
            'admin' => array(
                'className' => 'Multiple\Admin\Module',
                'path' => '../apps/admin/Module.php',
                )
            )
        );

    //Handle the request
    echo $application->handle()->getContent();
} catch (\Exception $e) {
    if ($environment == "development")
        echo $e->getMessage();
    else
        header("Location: /");
}

and my apps/www/Module.php (frontend)

<?php

namespace Multiple\WWW;

use Phalcon\Loader,
    Phalcon\Mvc\Dispatcher,
    Phalcon\Mvc\View,
    Phalcon\Mvc\ModuleDefinitionInterface;

class Module implements ModuleDefinitionInterface {

    /**
     * Register a specific autoloader for the module
     */
    public function registerAutoloaders() {

        $loader = new Loader();

        $loader->registerNamespaces(
                array(
                    'Multiple\WWW\Controllers' => '../apps/www/controllers/',
                    'Multiple\WWW\Models' => '../apps/www/models/',
                    'Multiple\WWW\Forms' => '../apps/www/forms/',
                    'Multiple\WWW\Helpers' => '../apps/www/helpers/',
                )
        );

        $loader->register();
    }

    /**
     * Register specific services for the module
     */
    public function registerServices($di) {

        //Registering a dispatcher
        $di->set('dispatcher', function() {
            $dispatcher = new Dispatcher();
            $dispatcher->setDefaultNamespace("Multiple\WWW\Controllers");
            return $dispatcher;
        });

        //Registering the view component
        $di->set('view', function() {
            $view = new View();
            $view->setViewsDir('../apps/www/views/');
            return $view;
        });
    }

}

my apps/admin/Module.php is similar

thanks in advance for considering my request and thanks for give us this excellent framework!



7.1k
Accepted
answer
edited Sep '14
  1. If you use Annotations router, why you setup router's paths (router->add)? It does not matter
  2. If you use URISOURCESERVERREQUESTURI, why you use rewrite rule in nginx config? You can try this config :

nginx.conf :

user  nginx;
worker_processes  1;
worker_rlimit_nofile 16384;
timer_resolution 100ms;

#error_log  /var/log/nginx/error.log debug;
pid        /var/run/nginx.pid;


events {
    worker_connections 16384; 
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    tcp_nopush     on;
    tcp_nodelay on;
      types_hash_max_size 2048;
    server_tokens off;
    charset utf-8;

    keepalive_timeout  echo $e->getMessage()5;

      gzip on;
      gzip_comp_level 1;
      gzip_buffers 4 8k;
    gzip_min_length 1400;
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript image/jpeg image/x-icon;

    include /etc/nginx/sites-enabled/*;
}

nginx/site-enabled/your_server :

server {
    listen   80; 
  server_name your_server;
  autoindex off;
  set $root_path  '/your_root_path';
  root $root_path;
  index index.php;

  location ~ assets/(css|js|img|img\/icons|video|photo|fonts|artimage)/ {

    access_log off;
    expires 360d;
  }

location ~ favicon.ico$ {
    access_log off;
    expires 360d;
  }
  location /  {
    try_files $uri  $uri/  /index.php;
  }

 location ~* \.php$ {
   default_type  application/octet-stream;
   try_files $uri =404;
   fastcgi_pass 127.0.0.1:9000;
   include /etc/nginx/fastcgi_params;
   fastcgi_split_path_info  ^(.+\.php)(/.+)$;
   fastcgi_index index.php;
   fastcgi_param  SCRIPT_FILENAME /full_path_for_your_public_folder/index.php;
   fastcgi_param  SCRIPT_NAME     /index.php;
   fastcgi_param  QUERY_STRING    $uri$args;
   fastcgi_param  REQUEST_METHOD  $request_method;
   fastcgi_param  CONTENT_TYPE    $content_type;
   fastcgi_param  CONTENT_LENGTH  $content_length;
   fastcgi_param  REQUEST_BODY    $request_body;
 }
} ## server config end

Try to use \Phalcon\Exception in your handle. And try it without redirect to /

catch (\Phalcon\Exception $e) {
    //if ($environment == "development")
    //    echo $e->getMessage();
    //else
    //    header("Location: /");
    echo $e->getMessage()

P.S. I recommended to use nginx repository for stable nginx

http://nginx.org/en/linux%5packages.html#distributions



1.1k

Yeah its works ! It was a problem with my Phalcon\Exception the redirection to / Thanks a lot Oleg <3