Nginx Virtual Directory using the same root gives 404 error

Viewed 1057

I'm fairly new to Nginx, and I'm attempting to import legacy sites from Apache. I want to get rid of the virtual directories but unfortunately I cannot.

The virtual directories point to the same root at the primary site. There is logic in the code that detects the virtual directory and loads data based on that information, which is why it's there.

Here is the config I'm attempting to get to work:

server {
    listen 80;

    large_client_header_buffers 4 32k;

    server_name site.domain.com;

    access_log /var/log/sites/site.access.log;
    error_log /var/log/sites/site.error.log error;


    location / {
        root /var/www/php/site;
        index index.php;
        include /etc/nginx/conf.d/php.inc;
    }

    location /sitea {
        root /var/www/php/site;
        index index.php;
        include /etc/nginx/conf.d/php.inc;
    }

    location ~ /\.ht {
        deny all;
    }
}

Contents of php.inc:

location ~ \.php$ {
    include snippets/fastcgi-php.conf;

    fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
    fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;

    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}

I have tried every thing that google has to offer to get this to work, but no matter what I do I continue to get the following error:

2018/11/15 20:28:32 [debug] 5056#5056: *1 http script var: "/sitea/index.php"
2018/11/15 20:28:32 [debug] 5056#5056: *1 trying to use file: "/sitea/index.php" "/var/www/php/site/sitea/index.php"
2018/11/15 20:28:32 [debug] 5056#5056: *1 trying to use file: "=404" "/var/www/php/site=404"
2018/11/15 20:28:32 [debug] 5056#5056: *1 http finalize request: 404, "/sitea/index.php?requestType=home" a:1, c:1
2018/11/15 20:28:32 [debug] 5056#5056: *1 http special response: 404, "/sitea/index.php?requestType=home"
2018/11/15 20:28:32 [debug] 5056#5056: *1 http set discard body
2018/11/15 20:28:32 [debug] 5056#5056: *1 xslt filter header
2018/11/15 20:28:32 [debug] 5056#5056: *1 HTTP/1.1 404 Not Found

Any help towards the right direction will be appreciated.

Note: it also does the same thing using the alias vs root

With alias I get the following:

2018/11/15 20:37:38 [error] 5189#5189: *1 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: #.#.#.#, server: site.domain.com, request: "GET /sitea/index.php?requestType=home HTTP/1.1", upstream: "fastcgi://unix:/run/php/php7.0-fpm.sock:", host: "site.domain.com"

Possible Solution

I'm not sure if this is the proper way of doing it, but it worked.

If I created a symbolic link for the virtual directory in the main folder the site loaded. I'd rather do this in Nginx, but if I have to go this route I will. Thoughts?

1 Answers

The root directive constructs the path to the file by the simple concatenation of its value with the current URI. So your second location block is looking for the file at /var/www/php/site/sitea/index.php.

The alias directive within a prefix location will replace the prefix text with the alias value. See this document for more.

location /sitea {
    alias /var/www/php/site;
    ...
}

So the above location block will look for the URI /sitea/index.php at /var/www/php/site/index.php.

Both the root and alias directives set a variable called $request_filename to the path to the file.

In your PHP block, you use $document_root$fastcgi_script_name to inform PHP-FPM of the path to the file. This works with root but not with alias.

fastcgi_param SCRIPT_FILENAME $request_filename;

The above works with both root and alias for PHP blocks which do not process path info (such as yours).

Related