haruya
haruya

Reputation: 63

yii2 using nginx for rewrite url

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.

Upvotes: 2

Views: 5220

Answers (2)

Mohammed Abir
Mohammed Abir

Reputation: 423

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;
  }
} 

Upvotes: 0

Rafal
Rafal

Reputation: 573

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>',
    ],

],

Upvotes: 1

Related Questions