Micro api + nginx

Hi guys!!!

Once again I'm sorry for my english.

I need to build an application to provide a rest api to communicate with AngularJs. I've been following the post http://forum.phalconphp.com/discussion/1753/micro-api-versioning, but in my case, I need build it in nginx server

I've been trying some configurations to do it, but most often I get an error saying 'The cycling rewrite or a simple 404 Error'.

Someone help me?

Tks!



34.7k

Hi Could you upload configuration file nginx ?

Hi Thien, follow my server configuration. Remember that I tried some differents configurations, and I don't know if this is near of be a good configuration.

I'm new with Phalcon and nginx.

tks for you reply.

server {
    listen 80;
    server_name dev.crs.callink.com.br;
    access_log E:/Desenv/Servers/nginx-1.9.0/logs/dev.crs.callink.com.br-access.log;
    error_log E:/Desenv/Servers/nginx-1.9.0/logs/dev.crs.callink.com.br-error.log;
    root E:/Desenv/Projetos/php/svn/crs-admin-branch/public;

    client_max_body_size 100m;

    location @rewrite {
        rewrite ^/(.*)$ /index.php?_url=/$1;
    }

    location / {
        sendfile on;
        client_max_body_size 200m;
        try_files $uri $uri/ /index.php$is_args$args;
        index  index.html index.htm index.php;
    }

    location /api/v1  {
        alias E:/Desenv/Projetos/php/svn/crs-admin-branch/api/v1/public;

        try_files $uri $uri/ @rewrite;

        location ~ \.php {
            fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
            fastcgi_index /index.php;

            include /etc/nginx/fastcgi_params;

            fastcgi_split_path_info       ^(.+\.php)(/.+)$;
            fastcgi_param PATH_INFO       $fastcgi_path_info;
            fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        }

        location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
            root $root_path;
        }

        location ~ /\.ht {
            deny all;
        }

    }


    location /api/v2  {
        alias E:/Desenv/Projetos/php/svn/crs-admin-branch/api/v2/public;

    }

    location ~ \.php$ {
        try_files $uri /index.php =404;
        fastcgi_index index.php;
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_param  SCRIPT_FILENAME E:/Desenv/Projetos/php/svn/crs-admin-branch/public$fastcgi_script_name;
        fastcgi_param debug true;
        include        fastcgi_params;
    }
}


34.7k

You can refer to configuration file nginx here

server {

    listen   80;
    server_name api.zphalcon.com;

    index index.php index.html index.htm;
    set $root_path '/usr/share/nginx/html/master/apps/api';
    root $root_path;
    #auth_basic "Admin Login";
        #auth_basic_user_file /etc/nginx/pma_pass;
    try_files $uri $uri/ @rewrite;

    location @rewrite {
        #auth_basic "Admin Login";
        #auth_basic_user_file /etc/nginx/pma_pass;
        rewrite ^/(.*)$ /index.php?_url=/$1;
    }
    location ~ \.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include fastcgi_params;
    }

    location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
        root $root_path;
    }

    location ~ /\.ht {
        deny all;
    }
}

It working me http://api.zphalcon.com//v1/test

Hi Thien!

Sorry for my inexperience.

I've this folder tree:

- application
    - public # my angularJs application
        -index.html
    - api
        - v1 # my Phalcon application in a first version
        - v2

How to adjusts my nginx configuration to serve the two requests? I had try a similar configuration with your configuration, but only can listen the phalcon request, even so I can't rewrite the parametrs of request.

Follow my current config:

server {

    listen   80;
    server_name dev.rest.callink.com.br;

    index index.php index.html index.htm;
    set $root_path 'E:/Desenv/Projetos/php/local/locadora-web';
    root $root_path;

    try_files $uri $uri/ @rewrite;

    location @rewrite {
        rewrite ^/(.*)$ /index.php?_url=/$1;
    }

    location ~ \.php$ {
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_split_path_info       ^(.+\.php)(/.+)$;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
        root $root_path;
    }

    location ~ /\.ht {
        deny all;
    }
}

Tks for your reply!



473

Hi

I see folder the above and file config nginx does not correspond

Sorry Thien!

My config doesn't look good!

I just wanted to show you the infrastructure that I want build, and how my final structure should look like this.

How to configure my server for that both applications can work?

- application
    - public # my angularJs application
        -index.html
    - api
        - v1 # my Phalcon application in a first version
        - v2

Once again sorry for my English.



473
edited May '15

Hey

In v1 you need file index.php, that mean index.php loading all app of you such as dependenci , bootstrap file... . When you need config like exmaple

set $root_path '/usr/share/nginx/html/application//api/v1';

When you release v2 then create configuration vhost for it

set $root_path '/usr/share/nginx/html/application//api/v2';

By the way to understand Vhost Nginx see here https://www.digitalocean.com/community/tutorials/how-to-set-up-nginx-virtual-hosts-server-blocks-on-ubuntu-12-04-lts--3

Hi Thien!

I'm new with nginx, and I never got understand the apache config very well.

This was my last try:

server {

    # SERVER
    listen   80;
    server_name dev.rest.application.build;

    # PATHS
    set $root_path 'E:/Desenv/Projetos/php/local/application/public';


    # ALIASES
    set $v1_path 'E:/Desenv/Projetos/php/local/application/api/v1/public';
    set $v2_path 'E:/Desenv/Projetos/php/local/application/api/v2/public';

    # COMMONS
    #gzip  on;
    root $root_path;
    index index.php index.html index.htm;

    #LOCATIONS 

    location /api/v1 {
        alias $v1_path;
        if (!-e $request_filename) {
            rewrite ^(.+)$ /api/v1/index.php?_url=$1 last;
            break;
        }
    }

    location /api/v2 {
        alias $v2_path;
        if (!-e $request_filename) {
            rewrite ^(.+)$ /api/v2/index.php?_url=$1 last;
            break;
        }
    }

    location ~ \.php {
        try_files $uri /index.php =404;
        fastcgi_index index.php;
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_param  SCRIPT_FILENAME $root_path$fastcgi_script_name;
        fastcgi_param debug true;
        include        fastcgi_params;
    }

    location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
        root $root_path;
    }

    #LOGGING
    rewrite_log on;
    access_log E:/Desenv/Servers/nginx-1.9.0/logs/dev.rest.application.build-access.log;
    error_log E:/Desenv/Servers/nginx-1.9.0/logs/dev.rest.application.build-error.log debug;
}

I think this config is close to what should be. Reading your suggestion, I found some config that I was not understand before, but yet not can run it as wanted to do.

I wanted to thank you for your support in this issue.



34.7k

Hi Robson

I am very glad to hear you say so. Any way if you want learming Phalcon tip or trick checkout http://phalcontip.com



3.1k

Hi,

Can you please suggest me which is the proper working configuration file ? I am getting 404 error as response for http://api.localhost.com/test/user, whereas my Root URL http://api.localhost.com is responding with Json data.

Please suggest me on the same.

Thanks, Raja K

edited May '16

I achieve to make multiples version of micro phalcon APIs on nginx :

Here my projects on file system :

- /var/www/myapp
        - /1.0
            phalcon project
        - /1.1
            phalcon project
        - /404.html

And my nginx configuration file :

server {

    listen 80;

    server_name api.my-domain.com;

    error_log /var/log/myapp/api/error.log;

    index index.php index.html index.htm;

    try_files $uri $uri/ @rewrite;

    root /var/www/myapp/;

    ## 404 PAGE FOR FACTCGI ERRORS
    error_page 404 /404.html;

    ## OTHER RULES (GZIP...)

    location @rewrite {
        rewrite ^(/1.0|/1.1|)/(.*)$ $1/public/index.php?_url=/$2;
    }

    ## EXAMPLE OF REWRITE :
    ##
    ## URL INPUT 
    ## http://api.my-domain.com/1.1/test
    ##
    ## URL OUTPUT
    ## http://api.my-domain.com/1.1/public/index.php?_url=/test

    location ~/1.0.*\.php$ {

        root /var/www/myapp/;

        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index /index.php;
        fastcgi_intercept_errors on;
        include /etc/nginx/fastcgi_params;
        fastcgi_split_path_info       ^(.+\.php)(/.+)$;
        fastcgi_param PATH_INFO       $fastcgi_path_info;
        fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    location ~/1.1.*\.php$ {

        root /var/www/myapp/;

        fastcgi_intercept_errors on;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index /index.php;
        include /etc/nginx/fastcgi_params;
        fastcgi_split_path_info       ^(.+\.php)(/.+)$;
        fastcgi_param PATH_INFO       $fastcgi_path_info;
        fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    ##CATCH URLs WITHOUT VERSIONS -- SET VERSION 1.1 BY DEFAULT
    location ~ \.php$ {

        root /var/www/myapp/1.1/;

        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index /index.php;
        fastcgi_intercept_errors on;
        include /etc/nginx/fastcgi_params;
        fastcgi_split_path_info       ^(.+\.php)(/.+)$;
        fastcgi_param PATH_INFO       $fastcgi_path_info;
        fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    ## WARN : MAKE SUR TO HAVE SAME FILES IN 1.0 & 1.1 PUBLIC FOLDERS (NOT A PROBLEM FOR APIs)

    location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
        root /var/www/myapp/1.1/public;
    }

    location ~ /\.ht {
        deny all;
    }
}

Now you juste have to call http://api.my-domain.com/{version}/{yourlocation} and nginx will redirect to the {version}/public/index.php.

Hope it may help someone.