简体   繁体   中英

Serving PHP from a subdirectory using nginx + php-fpm

The main static website (mirror) is located in: /home/mirrors/mirror

I'm trying setup PHP website only in the subdirectory /home/mirrors/mirror/phpweb/ . So far it loads PHP, but all the images, css and url links point to /home/mirrors/mirror/filename* instead of /home/mirrors/mirror/phpweb/filename*

I've tried with:

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

But that results in error logs showing:

FastCGI sent in stderr: "Unable to open primary script: /home/mirrors/mirror/phpweb/phpweb/index.php

The closest to working config is the incorrect way of setting the path with:

fastcgi_param SCRIPT_FILENAME /home/mirrors/mirror$fastcgi_script_name;

What am I overlooking? Here's the entire config. Basically it works except that images/css/js are not pointed to the phpweb/ directory. My config seems to be missing some lines that will allow statics inside of root /home/mirrors/mirror/phpweb; to point to that folder.

server {
    listen       80;
    listen      443 ssl http2;
    server_name  mirrors.domain.com;
   if ($scheme = http) {
        return 301 https://$http_host$request_uri;
    }

    location / {
        root /home/mirrors/mirror;
        index index.html index.htm;
        include /home/mirrors/Nginx-Fancyindex-Theme/fancyindex.conf;
        }

    location /phpweb {
        root /home/mirrors/mirror/phpweb;
        index /phpweb/index.php;
        }


    location ~ /phpweb/.*\.php$ {
        root /home/mirrors/mirror/phpweb;
#        try_files $uri =404;
#       try_files $uri $uri/ =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /home/mirrors/mirror$fastcgi_script_name;
        include fastcgi_params;
    }

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

Here's where I found a config example.

UPDATE: Here's updated config:

server {
    listen       80;
    listen      443 ssl http2;
    server_name  mirrors.domain.com;
    root /home/mirrors/mirror;
   if ($scheme = http) {
        return 301 https://$http_host$request_uri;
    }

    location / {
        index index.html index.htm;
        include /home/mirrors/Nginx-Fancyindex-Theme/fancyindex.conf;
        }

    location /phpweb {
        index /phpweb/index.php;
        }


    location ~ /phpweb/.*\.php$ {
        try_files $uri =404;
#       try_files $uri $uri/ =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#       fastcgi_param SCRIPT_FILENAME /home/mirrors/mirror/phpweb$fastcgi_script_name;
        include fastcgi_params;
    }

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

Which results in:

Warning: include(/home/mirrors/mirror/include/site.inc): failed to open stream: No such file or directory in /home/mirrors/mirror/phpweb/include/prepend.inc on line 78

Warning: include(): Failed opening '/home/mirrors/mirror/include/site.inc' for inclusion (include_path='.:/usr/share/php:/usr/share/pear') in /home/mirrors/mirror/phpweb/include/prepend.inc on line 78

Warning: include(/home/mirrors/mirror/include/langchooser.inc): failed to open stream: No such file or directory in /home/mirrors/mirror/phpweb/include/prepend.inc on line 81

Warning: include(): Failed opening '/home/mirrors/mirror/include/langchooser.inc' for inclusion (include_path='.:/usr/share/php:/usr/share/pear') in /home/mirrors/mirror/phpweb/include/prepend.inc on line 81

Warning: include(/home/mirrors/mirror/include/ip-to-country.inc): failed to open stream: No such file or directory in /home/mirrors/mirror/phpweb/include/prepend.inc on line 84

Warning: include(): Failed opening '/home/mirrors/mirror/include/ip-to-country.inc' for inclusion (include_path='.:/usr/share/php:/usr/share/pear') in /home/mirrors/mirror/phpweb/include/prepend.inc on line 84

Warning: include(/home/mirrors/mirror/include/layout.inc): failed to open stream: No such file or directory in /home/mirrors/mirror/phpweb/include/prepend.inc on line 87

Warning: include(): Failed opening '/home/mirrors/mirror/include/layout.inc' for inclusion (include_path='.:/usr/share/php:/usr/share/pear') in /home/mirrors/mirror/phpweb/include/prepend.inc on line 87

Warning: include(/home/mirrors/mirror/include/last_updated.inc): failed to open stream: No such file or directory in /home/mirrors/mirror/phpweb/include/prepend.inc on line 92

Warning: include(): Failed opening '/home/mirrors/mirror/include/last_updated.inc' for inclusion (include_path='.:/usr/share/php:/usr/share/pear') in /home/mirrors/mirror/phpweb/include/prepend.inc on line 92

Warning: include_once(/home/mirrors/mirror/include/releases.inc): failed to open stream: No such file or directory in /home/mirrors/mirror/phpweb/include/branches.inc on line 2

Warning: include_once(): Failed opening '/home/mirrors/mirror/include/releases.inc' for inclusion (include_path='.:/usr/share/php:/usr/share/pear') in /home/mirrors/mirror/phpweb/include/branches.inc on line 2

Warning: include_once(/home/mirrors/mirror/include/version.inc): failed to open stream: No such file or directory in /home/mirrors/mirror/phpweb/include/branches.inc on line 3

Warning: include_once(): Failed opening '/home/mirrors/mirror/include/version.inc' for inclusion (include_path='.:/usr/share/php:/usr/share/pear') in /home/mirrors/mirror/phpweb/include/branches.inc on line 3

Fatal error: Call to undefined function mirror_setcookie() in /home/mirrors/mirror/phpweb/index.php on line 44

Previously (original config pasted in this post) there we no php errors and the page loads. But images/statics were broken as described above.

The root directive specifies the part of the physical path which is prefixed to the URI to find the physical file. See this document for details.

So in the configuration you posted, it seems to me that the value for root should be the same in all three locations:

root /home/mirrors/mirror;

In fact, it is common to specify the root directive once in the server block and allow it to be inherited into each location. For example:

root /home/mirrors/mirror;
location / { ... }
location /phpweb { ... }
location ~ \.php$ { ... }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM