Using NginX and Laravel: URL Rewrites

10,640

Solution 1

I am not very familiar with Nginx, but I do think that your regex for the rewrite is incorrect.

Try changing

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

To:

rewrite ^/(.*)$ /index.php/$1 last;

Or:

rewrite ^.*$ /index.php last;

Solution 2

Here is my nginx / Laravel configuration (Debian in Linode):

# Laravel 4 nginx configuration
server {

        server_name www.yourdomain.com;

        access_log /var/www/yourdomain.com/log/access.log;
        error_log /var/www/yourdomain.com/log/error.log;

        root /var/www/yourdomain.com/public;
        index index.php index.html index.htm;

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

        location ~ \.php$ {

                # With php5-fpm:
                include /opt/nginx/conf/fastcgi_params; #Here you need to point your nginx instalation directory
                fastcgi_pass unix:/var/run/php5-fpm.sock; #Use this config if you are using sock or use fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

        }

}
Share:
10,640
BraydenJW
Author by

BraydenJW

I am a Software Systems Engineering student at the University of Regina.

Updated on June 04, 2022

Comments

  • BraydenJW
    BraydenJW almost 2 years

    I'm trying to setup the Laravel framework on my VPS running CentOS 6.4 and NginX 1.8. I can get everything else to work perfectly, except I can't get the cleaner URLs to work, like using "website.com/home" instead of "website.com/index.php/home". Can anyone help? This is the contents of my virtual host configuration file currently.

    server {
    
        listen 80;
        server_name swati.havok.semicolony.com;
        access_log /usr/share/nginx/semicolony.com/_subdomains/swati/storage/logs/access.log;
        error_log /usr/share/nginx/semicolony.com/_subdomains/swati/storage/logs/errors.log;
    
        root /usr/share/nginx/semicolony.com/_subdomains/swati/public;
        index index.php;
    
        #browse folders if no index file
        autoindex on;
    
        # enforce NO www
        if ($host ~* ^www\.(.*))
        {
            set $host_without_www $1;
            rewrite ^/(.*)$ $scheme://$host_without_www/$1 permanent;
        }
    
        # serve static files directly
        location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
            access_log off;
            #expires max;
        }
    
        # removes trailing slashes (prevents SEO duplicate content issues)
        if (!-d $request_filename)
        {
            rewrite ^/(.+)/$ /$1 permanent;
        }
    
        # removes trailing "index" from all controllers
        if ($request_uri ~* index/?$)
        {
            rewrite ^/(.*)/index/?$ /$1 permanent;
        }
    
        # unless the request is for a valid file (image, js, css, etc.), send to bootstrap
        if (!-e $request_filename)
        {
            rewrite ^/(.*)$ /index.php?/$1 last;
            break;
        }
    
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    
        location ~ \.php$ {
        root                    /usr/share/nginx/semicolony.com/_subdomains/swati/public;
        fastcgi_pass            127.0.0.1:9000;
        fastcgi_index           index.php;
        fastcgi_param SCRIPT_FILENAME   $document_root$fastcgi_script_name;
        include             fastcgi_params;
        }
    
        location ~ /\.ht {
            deny  all;
        }
    }
    

    Edit: It may be of importance that I am using Laravel 4.