nginx 502 errors with yii when i use path URL format

Hello,Everyone,

I want to use nginx as server in my vps, but it appears 404 error, then I reffer http://www.yiiframework.com/doc/cookbook/15/, then no 404 errors, but 502 errors appears. When I don’t use the path URL format everything is ok, So I don’t think it’s caused by the nginx configuration. Anybody have ideas about it?

Thank you!

James Heller

Best Regards

Hey James,

I’m currently running nginx on a VPS serving up a Yii app.

What rewrite rules do you have in your nginx.conf? Here is my location block, for reference:




        location / {

            root /path/to/webroot;

            index index.php;


            if (-f $request_filename) {

            break;

            }


            if (!-e $request_filename) {

                rewrite ^/(.+)$ /index.php?url=$1 last;

                break;

            }

        }



I use this with the path URL format and showScriptName set to false, like so:




		'urlManager'=>array(

			'urlFormat'=>'path',

			'showScriptName'=>false

		),



I also have an additional location block specifically for serving php files (they get passed to a fastcgi process listening on port 9000; php-fpm). If you need to see that as well, let me know :)

Thank you for your quick reply :)




server

 {

  listen       80;

  server_name www.xxx.com xxx.com;

  index index.html index.htm index.php;

  root  /home/www/blog;


  location ~ \.php {

    fastcgi_pass  127.0.0.1:9000;

    fastcgi_index index.php;

    include fastcgi_params;

    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

    fastcgi_param PATH_INFO $fastcgi_script_name;

    access_log off;

  }


  location /blog {

    if (!-e $request_filename){

        rewrite (.*) /blog/index.php/$1 break;

    }

  }




  location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$

   {

    expires      30d;

   }


  location ~ .*\.(js|css)?$

   {

    expires      12h;

   }


  access_log   off;

 }




This is my configuration, anything wrong with it?

Yep, even though you have a \.php location block to pass scripts to the fastcgi process, you still need a root location block (or wherever Yii’s top-most index.php is) to give Yii a chance to do it’s routing, and that means




rewrite ^/(.+)$ /index.php?url=$1 last;



This passes the requested URI (your controller and action names) to ‘index.php’ in your webroot in what is apparently the only rewrite rule I could get to work (and I tried all variations).

Since nginx applies logic to URI’s in the order of the location blocks as listed in your nginx.conf, you may need to position the root location block after your /blog location block, unless of course that is the yii web root, in which case you should change the rewrite rule to the one above.

I believe nginx is smart enough to know that if you have a location block that is something other than ‘/’ (yours is /blog’) it assumes the URI is then anything after the location. So in your case, anything after ‘/blog’ is the URI, and if that is the case, then the same rewrite rule above will work in your /blog location.

If that doesn’t work, try this rewrite rule for the /blog location block:




rewrite ^/(.+)$ /blog/index.php?url=$1 last;



…similar to yours except the regex is different and the ‘url=’ appendage is required for me. I could not get nginx to work (502s like you get) when I tried to use the simple $1 variable.

I did as what you said, now my yii application works well in the ngnix, Thanks for your help very much! :)