Nginx: Pretty URLs

Hi,

can anyone pass me the configuration for clean urls with Yii2? I’ve searched the web, but I couldn’t find any working tutorial.

That’s my config by now:


server {

    listen 80 default_server;

    listen [::]:80 default_server ipv6only=on;


    root /usr/share/nginx/html;

    index index.php index.html index.htm;


    # Make site accessible from http://localhost/

    server_name localhost MY-IP;


    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


        try_files $uri $uri/ /index.php?$args;

    }


    location @rewrite{

              rewrite ^/(.*)$ /index.php?$args;

         }


    error_page 404 /404.html;

        error_page 500 502 503 504 /50x.html;

        location = /50x.html {

            root /usr/share/nginx/html;

        }


        location ~ \.php$ {

            try_files $uri =404;

            fastcgi_split_path_info ^(.+\.php)(/.+)$;

            fastcgi_pass unix:/var/run/php5-fpm.sock;

            fastcgi_index index.php;

            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

            include fastcgi_params;

        }


}

The root url works. But I cannot access any controller or method.

That’s my Yii2 Url Manager component config:


        'urlManager' => [

            'class' => 'yii\web\UrlManager',

            // Disable index.php

            'showScriptName' => false,

            // Disable r= routes

            'enablePrettyUrl' => true,

            'rules' => array(

                '/' => 'my-module/password/index',

                '/user' => 'my-module/user/index',

                '<controller:\w+>/<id:\d+>' => '<controller>/view',

                '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',

                '<controller:\w+>/<action:\w+>' => '<controller>/<action>',

            ),

        ],

Has anyone an idea what I should edit here?

Did you look at this - http://stackoverflow.com/questions/27944703/how-to-get-clean-urls-in-yii2-like-post-100 ?

Hi,

thanks for your answer.

I’ve already have the urlManager component configured. It works with apache. But if I have slashes in the url, Nginx treats them like folders. How I can change that? Did I need any specific Nginx config? I’ve already tried to configure my Nginx (please see my very first post for this).