2

i'm trying to use Nginx to rewrite the url to more user friendly. Removing index.php?r= is success, but the problem is, after i tried to visit other page, it says 404 Not Found. I already add in the config/web the urlmanager for pretty url but its not working . Can someone help me with this?

i'll try to post the code .

this is the nginx.conf

server {
        listen       88;
        server_name  localhost;


        location / {
            root   html;
            index index.php index.html index.htm;
            rewrite ^(.*[^/])$ $1/ permanent;
            try_files $uri $uri/ /index.php?r=$args;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        location ~ .php$ {
            include        fastcgi_params;
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include fastcgi.conf;
        }
    }

and this is the url manager .

'urlManager' => [
            'class' => 'yii\web\UrlManager',
            // Disable index.php
            'showScriptName' => false,
            // Disable r= routes
            'enablePrettyUrl' => true,
            'rules' => array(
                    '<controller:\w+>/<id:\d+>' => '<controller>/view',
                    '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
                    '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
            ),
        ],

I tried this at my localhost first .

Thank You.

2
  • I Think you have to give the root path as root /var/www/html/yii-app/web; instead of html; Commented Feb 22, 2016 at 12:10
  • @Selvakumar when i change the root html as you suggest from html to /html/yii-app/web, this is the error i get in error.log.. 2016/02/23 10:13:53 [error] 3116#1344: *2 "C:/html/MAPUser/web/MAPUser/web/index.php" is not found (3: The system cannot find the path specified) i place my nginx in C: Commented Feb 23, 2016 at 3:18

2 Answers 2

1

Nginx config for yii2 Basic:

server {
    server_name localhost;
    root /path/to/localhost/yii2basic/web;
    index index.php;

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

    location ~ \.php$ {
        try_files $uri =404;
        include fastcgi.conf;
    }
}

My fastcgi code. - normally is in nginx config folder.

Yii config file:

'urlManager' => [
    'enablePrettyUrl' => true,
    'showScriptName' => false,
    'rules' => [
        '/' => 'site/index',
        '<controller:\w+/?>' => '<controller>/index',
        '<controller:\w+>/<id:\d+>' => '<controller>/view',
        '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
    ],

],

Sign up to request clarification or add additional context in comments.

1 Comment

What if we're using php-fpm? Do we still use the fastcgi.conf file?
0
server {
  listen 84 default_server;
  listen [::]:84 default_server;

  root /var/www/your_project_dir_name/html;

  # Add index.php to the list if you are using PHP
  index index.html index.htm index.php;

  server_name your_domain_name.com www.your_domain_name.com;

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

  # pass PHP scripts to FastCGI server
  #
  location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php/php5.6-fpm.sock;
  }

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.