We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Nginx config doesn't work

Hi everyone,

I've started learning Phalcon about half year ago, and I used Apache with no problem

Now I want to switch to Nginx, because I'm also working on a project with Magento, and I deploy Magento on Nginx without problem.

Today, I tried to build the simple rest api https://docs.phalcon.io/en/latest/reference/tutorial-rest.html

I've editted host file 127.0.0.1 api.test.com

The project folder is in /var/www/api.test.com/public_html/ (I mean index.php and folder models in is public_html)

This is my config in .../etc/nginx/site-availables/api.test.com

server {
  listen   80;

  server_name api.test.com;
  root /var/www/api.test.com/public_html;

  ## rewrite example.com to www.example.com
  if ($http_host != "api.test.com") {
      rewrite ^ $scheme://api.test.com$request_uri permanent;
  }

  location / {
      index index.html index.php; ## Allow a static html file to be shown fir$
      try_files $uri $uri/ @handler; ## If missing pass the URI to Magento's $
      expires 30d; ## Assume all files are cachable
  }

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

  location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
      root /var/www/api.test.com/public_html;
  }
  location ~ /\.ht {
      deny all;
      access_log off;
      log_not_found off;
  }
}

And I also link this to /etc/nginx/site-enables/api.test.com

When I go to api.test.com on the browser bar, I can only see the Nginx default index.html (I mean Welcome to Nginx...)

Anyone know why it doesn't point to the api.test.com/public_folder/index.php?

Thank you very much

server {
    server_name *.sitename.fr;
    listen 80;
    port_in_redirect off;
    server_tokens off;
    autoindex off;
    charset      utf-8;

    set $root_path /var/www/sitefolder/public/;
    root $root_path;

    index index.php index.html index.htm;

    try_files $uri $uri/ @rewrite;

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

    location ~ \.php$ {

        try_files $uri =404;

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

    }

}
server {
   server_name *.sitename.fr;
   listen 80;
   port_in_redirect off;
   server_tokens off;
   autoindex off;
   charset      utf-8;

   set $root_path /var/www/sitefolder/public/;
   root $root_path;

   index index.php index.html index.htm;

   try_files $uri $uri/ @rewrite;

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

   location ~ \.php$ {

       try_files $uri =404;

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

   }

}

Unfortunately, now I get 404 Not Found Do you have any suggestion? Thank you



58.4k
Accepted
answer

Hey man

What is OS used ?

You try this:

server {

    listen   80;
    server_name hovercrowd.localdomain;

    index index.php index.html index.htm;
    set $root_path '/home/duythien/hovercrowd/public';
    root $root_path;

    try_files $uri $uri/ @rewrite;

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

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

        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;

        include /etc/nginx/fastcgi_params;
    #return 200 $document_root$fastcgi_script_name;
    }

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

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

Then in folder public change all content in file index.php tobe

phpinfo();

Hey man

What is OS used ?

You try this:

server {

   listen   80;
   server_name hovercrowd.localdomain;

   index index.php index.html index.htm;
   set $root_path '/home/duythien/hovercrowd/public';
   root $root_path;

   try_files $uri $uri/ @rewrite;

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

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

       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;

       include /etc/nginx/fastcgi_params;
  #return 200 $document_root$fastcgi_script_name;
   }

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

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

Then in folder public change all content in file index.php tobe

phpinfo();

Thank you very much, after an hour test and see log with your config, I found the solution. I have to remove this line "fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;" and then I can see the phpinfo page.

I think the reason why I have to remove that line because I set cgi.fix_pathinfo = 0 for security purpose Anyway, thank you for helping me!



58.4k

congratulation :) . I see you name like Vietnam if you have tip or want to support forum Phalcon Vietnam check here https://forum.zphalcon.com

I have the same problem with you, set cgi.fix_path_info=0; thank you very much!

Hey man

What is OS used ?

You try this:

server {

   listen   80;
   server_name hovercrowd.localdomain;

   index index.php index.html index.htm;
   set $root_path '/home/duythien/hovercrowd/public';
   root $root_path;

   try_files $uri $uri/ @rewrite;

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

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

       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;

       include /etc/nginx/fastcgi_params;
 #return 200 $document_root$fastcgi_script_name;
   }

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

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

Then in folder public change all content in file index.php tobe

phpinfo();

Thank you very much, after an hour test and see log with your config, I found the solution. I have to remove this line "fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;" and then I can see the phpinfo page.

I think the reason why I have to remove that line because I set cgi.fix_pathinfo = 0 for security purpose Anyway, thank you for helping me!