1
votes
server {

    server_name mediaserver.localdomain;
    listen 80;
    index index.php index.htm index.html;
    root /var/www/html/Organizr;
    location = / {
        root /var/www/html/Organizr;
    }
    location /homelab {
        alias /opt/homelab/;
    }
    location ~ /\.git {

        deny all;
    }
    location ~ \.php$ {

    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }

}

The include snippets/fastcgi-php.com; =

# regex to split $uri to $fastcgi_script_name and $fastcgi_path
fastcgi_split_path_info ^(.+\.php)(/.+)$;

# Check that the PHP script exists before passing it
try_files $fastcgi_script_name =404;

# Bypass the fact that try_files resets $fastcgi_path_info
# see: http://trac.nginx.org/nginx/ticket/321
set $path_info $fastcgi_path_info;
fastcgi_param PATH_INFO $path_info;

fastcgi_index index.php;
include fastcgi.conf;

That is my config and for the life of me I cannot get it to work.

My expectation is to have the http :// mediaserver.localdomain/ to go to the "/var/www/html/Organizr/index.php"

And when I go to http :// mediaserver.localdomain/homelab/ it pulls the "/opt/homelab/index.php"

But only the http :// mediaserver.localdomain/ works not the /homelab/

I have exhausted my google admin techniques and the nginx documentation pages on alias and root definitions.

Thanks in advance.

FYI (i put the spaces in the links deliberately to get rid of auto links)

1

1 Answers

1
votes

You have two roots from which you need to execute PHP files, which means you need two separate locations with your fastcgi_pass directive.

server {
    server_name mediaserver.localdomain;
    listen 80;
    index index.php index.htm index.html;

    root /var/www/html/Organizr;

    location / {
        try_files $uri $uri/ =404;
    }
    location ~ /\.git {
        deny all;
    }
    location ~ \.php$ {
        try_files $uri =404;
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }

    location ^~ /homelab {
        root /opt;
        try_files $uri $uri/ =404;

        location ~ \.php$ {
            try_files $uri =404;
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        }
    }
}

The location / block inherits the root from the outer block, it does not need to be repeated.

The first location ~ \.php$ block processes any .php URI which does not begin with /homelab.

The location ^~ /homelab block is a prefix location that takes precedence over other regular expression locations at the same level. See this document for details.

The nested location ~ \.php$ block is responsible for processing .php URIs below /homelab which are located in /opt/homelab.

I have added a few try_files directives which also addresses the issue of passing uncontrolled requests to PHP.