Search code examples
nginxfastcgi

No input file specified : Can't config NGINX Alias


I'm having trouble setting up two locations in my Nginx conf file.

I had no problem having two locations before adding one with an alias.

Location / with alias doesn't work. Location /dev without alias works. I would like to use two aliases because I have two folders : prod and dev.

Here is my current conf :

server {
    listen       80;
    listen   [::]:80;
    server_name  domain.com www.domain.com;

    root /home/domain/public_html/www/;
    index index.html index.htm index.php index2.php;

    location / {
        alias /home/domain/public_html/www/prod/;
        try_files $uri $uri/ /index.php?q=$uri&$args;
    }


    location /dev {
        try_files $uri $uri/ /index.php?q=$uri&$args;
    }

    location ~* \.php$ {
      fastcgi_pass     127.0.0.1:9000;
      include          fastcgi_params;
      fastcgi_param    SCRIPT_FILENAME    $request_filename;
      fastcgi_param    SCRIPT_NAME        $fastcgi_script_name;
    }

    charset utf-8;

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }
    location ~ /\.ht {
        deny all;
    }
}

What is happening is that accessing domain.com/dev/ works great but as soon as it's on the / location, I get a "no input file specified" error.

If I enter domain.com/license.txt, I can see Wordpress's license file. If I try domain.com/index.php, I get the error.

I'm already using $request_filename to avoid root vs alias problems, any idea ?


Solution

  • You do not need to use alias in this scheme, but if you wish to run PHP with two separate roots, you will need to use a nested location block.

    For example:

    root /home/domain/public_html/www/prod;
    
    location / {
        try_files $uri $uri/ /index.php?q=$uri&$args;
    }
    location ~* \.php$ {
      fastcgi_pass     127.0.0.1:9000;
      include          fastcgi_params;
      fastcgi_param    SCRIPT_FILENAME    $request_filename;
      fastcgi_param    SCRIPT_NAME        $fastcgi_script_name;
    }
    
    location ^~ /dev {
        root /home/domain/public_html/www;
    
        try_files $uri $uri/ /dev/index.php?q=$uri&$args;
    
        location ~* \.php$ {
            fastcgi_pass     127.0.0.1:9000;
            include          fastcgi_params;
            fastcgi_param    SCRIPT_FILENAME    $request_filename;
            fastcgi_param    SCRIPT_NAME        $fastcgi_script_name;
        }
    }
    

    The first two location blocks are your /prod/ configuration, with the correct root to resolve URIs like /index.php.

    The last location and nested location blocks are your /dev/ configuration. The root is set to the correct value to resolve URIs like /dev/index.php.

    See this document for more.