0

I'm using Laravel in a serverblock and I'd like to create an alias, for example /webmail. This results in an nginx "Primary script unknown" error. I think I need to change my fastcgi_param. Can anyone help me?

Below is the important part about my Nginx serverblock.

server {

    listen       80 default_server;
    server_name  _;

    set $root_path '/var/www/html/public';
    root $root_path;

    index index.php index.html index.htm;

    try_files $uri $uri/ @rewrite;

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

    location ~ \.php {

        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index /index.php;

        include /etc/nginx/fastcgi_params;

        fastcgi_split_path_info       ^(.+\.php)(/.+)$;
        fastcgi_param PATH_INFO       $fastcgi_path_info;
        fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    location /webmail {
        root /var/www/;
    }
}

So my webmail application is located in /var/www/webmail.

Webmail works when I change the SCRIPT_FILENAME to:

fastcgi_param SCRIPT_FILENAME /var/www$fastcgi_script_name;

However, the root domain doesn't work anymore after this change. I can't place the line above in the location section.

Law29
  • 3,557
  • 1
  • 16
  • 28
Patrick
  • 71
  • 8

1 Answers1

0

Try this configuration:

server {

    listen       80 default_server;
    server_name  _;

    set $root_path '/var/www/html/public';
    root $root_path;

    index index.php index.html index.htm;

    try_files $uri $uri/ @rewrite;

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

    location ~ \.php {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index /index.php;

        include /etc/nginx/fastcgi_params;

        fastcgi_split_path_info       ^(.+\.php)(/.+)$;
        fastcgi_param PATH_INFO       $fastcgi_path_info;
        fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
        fastcgi_param SCRIPT_FILENAME $root_path$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT   $root_path;
    }

    location /webmail {
        set $root_path '/var/www';
        root $root_path;
    }
}

So, we pass a different document root / script filename prefix to PHP depending on the location. On default locations we have $root_path set to /var/www/html/public and on /webmail we set it to /var/www.

You may need to add a separate rewrite / try_files section in the /webmail location, so that index.php or any other default webmail entry script is tried. You need to look at your webmail's documentation for this.

Tero Kilkanen
  • 36,796
  • 3
  • 41
  • 63
  • I'm currently testing it with Laravel in my subdirectory, but this still returns the same message. Any suggestions?` – Patrick Aug 15 '16 at 08:59