nginx / php-fpm rewriting for laravel

September 21, 2014 9.9k views

Hi all,

I am new to configuring nginx and have been following many tutorials but to no avail in trying to get a subdomain routed correctly.

quick sidenotes, my domain is hosted elsewhere and for now I have beta.domain pointing to DO - until I can test site works then I will migrate. Also, my LEMP installation is the default just with my custom site.

my sites-available/example.com file reads as follows (note it's not actually called example.com i'm just substituting that for question purposes).

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

        root /usr/share/nginx/example.com/public;
        index index.php;

        server_name example.com;

        try_files $uri $uri/ @rewrite;

        location @rewrite {
                rewrite ^/(.*)$ /index.php/$1;
        }

        location ~ [^/]\.php(/|$) {
                try_files $uri $uri/ @rewrite;
                fastcgi_split_path_info    ^(.+\.php)(/.+)$;
                fastcgi_pass               unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
        }

}

I effectively have this block in my config, one for eaxmple.com and another for beta.example.com.

after a morning trying my laravel is at least working when I do beta.example.com/index.php/page but I would like to turn this into beta.example.com/page.

Thanks in advance for any replies.

1 comment
  • in my error log I get this when I try to use the rewritten url

    *9 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: x.x.x.x, server: beta.example.com, request: "GET /test HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "beta.example.com"

1 Answer

FastCGI's SCRIPT_FILENAME parameter isn't set properly -- replace

        location ~ [^/]\.php(/|$) {
                try_files $uri $uri/ @rewrite;
                fastcgi_split_path_info    ^(.+\.php)(/.+)$;
                fastcgi_pass               unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
        }

with

location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
}

and restart nginx:

sudo service nginx restart
  • I have just tried this, still not working, i'm wondering if this could be down to fpm configuration problem rather than nginx?

  • Accepted as answer, I think I was missing a semi-colon or something, also my symobolic link wasn't set up correctly, thanks for the help :)

Have another answer? Share your knowledge.