3

I usually use apache and want to give NGINX a try.

I have installed it on my ubuntu dev machine and have a few different frameworks and sites set up and in development (codeigniter, symfony, laravel, etc).

The problem I'm getting is that only paths that end with .php work. If I try index.php/welcome/index it just 404s instead of loading index.php.

I have tried with cgi.fix_pathinfo set to 1 and 0.

Here is my current (of many tried) site config.

server {
listen   80; ## listen for ipv4; this line is default and implied
#listen   [::]:80 default_server ipv6only=on; ## listen for ipv6

root /my/path;
index index.php index.html;

# Make site accessible from http://localhost/
server_name localhost;

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
#error_page 500 502 503 504 /50x.html;
#location = /50x.html {
#   root /usr/share/nginx/www;
#}

location ~ \.php$ {
    try_files $uri =404;

    # Fix for server variables that behave differently under nginx/php-fpm than typically expected
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    # Include the standard fastcgi_params file included with nginx
    include fastcgi_params;
    fastcgi_param  PATH_INFO        $fastcgi_path_info;
    fastcgi_index index.php;
    # Override the SCRIPT_FILENAME variable set by fastcgi_params
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    # Pass to upstream PHP-FPM; This must match whatever you name your upstream connection
    fastcgi_pass unix:/var/run/php5-fpm.sock;
}


location / {
    # First attempt to serve request as file, then
    # as directory, then fall back to displaying a 404.
    try_files $uri $uri/ =404;
    # Uncomment to enable naxsi on this location
    # include /etc/nginx/naxsi.rules
}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
    deny all;
}
}
Atrox1449
  • 101
  • 3
  • 8
  • 1
    I know that symfony has links to an example nginx setup, and i would think that most of the other frameworks you mentioned do as well... did you consult the documentaion? Also your main issue here is that you are looking specifically for .php at the **end** in your `location` – prodigitalson Mar 09 '13 at 22:49
  • 1
    to state the obvious: http://wiki.nginx.org/Codeigniter . You need "try_files $uri $uri/ /index.php;" – galchen Mar 09 '13 at 22:52
  • @prodigitalson Thanks, it was php$ which was causing the problem. Removed it now and it works ok. galchen That would only work for codeigniter and others that use index.php (symfony does not) and also forwards all to root which is no good for development. – Atrox1449 Mar 09 '13 at 22:58
  • @Atrox1449: You should be using vhosts or the nginx equiv `server`, not working with subfolders in the the root... thats just silly. And then it lets you set up different rules on a per `server` basis. – prodigitalson Mar 09 '13 at 23:06
  • 1
    i meant to make a redirect for each folder in it's own config. As @prodigitalson stated, rules per server, use vhost – galchen Mar 09 '13 at 23:55

3 Answers3

2

I'd prefer to use the following nginx config structure. It's cleaner:

location / {
  try_files $uri $uri/ @phpsite;
}

location @phpsite {
  include fastcgi_params;
  ... other fast_cgi directives
}

A bit more complex setup can be found in the popular silex project: http://silex.sensiolabs.org/doc/web_servers.html#nginx.

I see 2 problems in the original config file:

location ~ \.php$ {
    try_files $uri =404;
    ...
}
  1. In regex '$' means matching at the end of the string. So it failed as stated in the comments by prodigitalson.
  2. That try_files directive inside the above fast_cgi location block should not be there because that location block is supposed to be handled by php alone. It's cleaner to remove that line.
Chuan Ma
  • 9,754
  • 2
  • 45
  • 37
  • I removed the $ and it worked. The question is why doesn't it work when the $ is there? The file should end in '.php' after all no? – Marin Mar 12 '13 at 19:07
  • The match is against the entire url, e.g., index.php/welcome/index. Here the last string is index, not php – Chuan Ma Mar 12 '13 at 21:24
  • Makes sense thanks. So we're saying match any 'path' with '.php' in it basically, correct? – Marin Mar 14 '13 at 14:22
  • I actually browsed another site and found out that the culprit was the $. I'm upvoting yours so that it helps others. – Marin Mar 14 '13 at 20:04
0

I think what you're missing is a rule like

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

that will try to call that index.php url if the path does not exist.

Or maybe, if you know that it is pointless to try other things, just

location / {
    try_files /index.php?$args;
}

or

location ~ /index.php {
    try_files /index.php?$args;
}
Alex Siri
  • 2,856
  • 1
  • 19
  • 24
0

This works for me...

location ~ ^(.*?\.php)($|/.+) {
    try_files $1 =404;

    ... fastcgi conf...
}
Felipe Buccioni
  • 19,109
  • 2
  • 28
  • 28