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.

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 http://docs.phalconphp.com/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/publichtml/ (I mean index.php and folder models in is publichtml)

> 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 { > servername *.sitename.fr; > listen 80; > portinredirect off; > servertokens off; > autoindex off; > charset utf-8; > > set $rootpath /var/www/sitefolder/public/; > root $rootpath; > > index index.php index.html index.htm; > > tryfiles $uri $uri/ @rewrite; > > location @rewrite { > rewrite ^/(.*)$ /index.php?url=/$1; > } > > location ~ .php$ { > > tryfiles $uri =404; > > fastcgipass 127.0.0.1:9000; > fastcgisplitpathinfo ^(.+.php)(/.+)$; > fastcgiindex index.php; > fastcgiparam SCRIPTFILENAME $documentroot$fastcgiscriptname; > include fastcgiparams; > > > } > >} >```

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



37.1k
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: > >```php >server { > > listen 80; > servername hovercrowd.localdomain; > > index index.php index.html index.htm; > set $rootpath '/home/duythien/hovercrowd/public'; > root $rootpath; > > tryfiles $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 > >```php >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 "fastcgiparam PATHTRANSLATED $documentroot$fastcgipath_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!



37.1k

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

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

>>Hey man >> >>What is OS used ? >> >>You try this: >> >>```php >>server { >> >> listen 80; >> servername hovercrowd.localdomain; >> >> index index.php index.html index.htm; >> set $rootpath '/home/duythien/hovercrowd/public'; >> root $rootpath; >> >> tryfiles $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 $rootpath; >> } >> >> location ~ /.ht { >> deny all; >> } >>} >> >>``` >> >>Then in folder public change all content in file index.php tobe >> >>```php >>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 "fastcgiparam PATHTRANSLATED $documentroot$fastcgipathinfo;" 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!