Using Nginx with Nextcloud/Website/Paperwork paral

2019-08-01 13:12发布

问题:

I am trying to run Nextcloud,a Homepage and Paperwork under different locations, but can't figure out how to configure my nginx-config correctly.

My working tree looks like this:

/var/www/
|-> website
|-> nextcloud
|-> paperwork

My Homepage is reachable through web.domain.com and my Nextcloud ist reachable with cloud.domain.com. Now i want to get Paperwork to be reachable under web.domain.com/notes. The index.php of Paperwork lies in the subfolder "paperwork/frontend/public".

This is my attemp to solve this (without the whole ssl and the cloud part):

server{
    listen 443 ssl http2;
    server_name web.domain.com;
    error_log /var/log/nginx/debug.log debug;

    root /var/www/website;
    location / {
    index index.php index.html;
    }

    location /notes {
            alias /var/www/paperwork/frontend/public;
            index index.php index.html index.htm;
            try_files $uri $uri/index.php;
    }

    location ~ /(nextcloud|backups) {
            deny all;
            return 403;
    }
    location ^~ /nextcloud/ {
            deny all;
            return 402;
    }
    location ^~ /nextcloud/ {
            deny all;
            return 402;
    }

    location ~ \.php$ {
            try_files $uri =404;
            alias /var/www/paperwork/frontend/public;
            index index.php index.html index.htm;

            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME 
            $document_root$fastcgi_script_name;
            include fastcgi_params;

    }
}

I tried out a lot different solutions but i eather get an 404 because he is using the wrong directory and can't find /var/www/notes/index.php (or similar errors) or nginx is returning me just the index.php as a file-download.

Thx in advance!

回答1:

Use nested location blocks for a cleaner solution. Note the ^~ modifier to avoid any ambiguity. See this document for more.

Try:

location ^~ /notes {
    alias /var/www/paperwork/frontend/public;
    index index.php index.html index.htm;

    if (!-e $request_filename) { rewrite ^ /notes/index.php last; }

    location ~ \.php$ {
        if (!-f $request_filename) { return 404; }

        fastcgi_pass unix:/var/run/php5-fpm.sock;

        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $request_filename;
    }
}

There is a long standing bug regarding the use of alias with try_files. See this caution on the use of if.

Include fastcgi_params before using the fastcgi_param directive, as it may silently overwrite your parameters.