Nginx root prefix with other subfolder

apache-2.4nginxPHP

I spent a lot of hours with a seemingly simple config, but I don't know the correct config 🙁

I have 2 different application, what I have to serve 2 different url. Everything under this 2 application need to catch all an index.php.

Examples: (public URL => served path (application))

https://example.com/ => /var/public/index.php
https://example.com/xy => /var/public/index.php
https://example.com/xy/zw => /var/public/index.php

but!

https://example.com/api/v2 => /api/public/index.php
https://example.com/api/v2/xy => /api/public/index.php
https://example.com/api/v2/xy/test => /api/public/index.php

I also have Apache reverse proxy configured: (only for rewrite /api/v2 to /api/public, so on nginx only /api/public uri displayed – reverse proxy needed, I have only 1 public IP but many sites)

<VirtualHost *:443>
    ServerName xxx.com
    <Proxy *>
        Order deny,allow
        Allow from all
    </Proxy>
    SSLEngine On
    SSLCertificateFile  /etc/apache2/cert/xxx.com.pem
    SSLCertificateKeyFile /etc/apache2/cert/xxx.com.key
    SSLCertificateChainFile /etc/apache2/cert/yyyy.pem
    ProxyRequests       Off
    ProxyPreserveHost On
    RewriteEngine On
    RewriteRule ^/api/v2$ /api/v2/ [R,L]
    <Location /api/v2/>
            ProxyPass           http://1.2.3.4/api/public/     KeepAlive=On    TimeOut=3600 retry=0
            ProxyPassReverse    http://1.2.3.4/api/public/
    </Location>

    ProxyPass      /     http://1.2.3.4/    KeepAlive=On    TimeOut=3600 retry=0
    ProxyPassReverse  /  http://1.2.3.4/

    LogLevel debug
    ErrorLog /var/log/apache2/xxx-web-error.log
    CustomLog /var/log/apache2/xxx-web-access.log common
</VirtualHost>

My config don't work for the second section, only for one exact path: https://example.com/api/v2.

Without fastcgi php execution, I managed this config, but this don't work with dynamic php.

My nginx config:

server {
    listen       80;
    #server_name  localhost;

    # main root for GUI
    #root        /api/public;
    root        /var/public;
    index  index.php;

    location /api/public {
            alias /api/public;
            try_files $uri $uri/ /api/public/index.php?$args;
            #try_files $uri $uri/ /api/public/index.html;
            location ~ \.php$ {
                    include fastcgi_params;
                    try_files $uri = 404;
                    fastcgi_split_path_info ^(.+\.php)(/.+)$;
                    fastcgi_pass 127.0.0.1:9000;
                    fastcgi_index index.php;
                    fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
            }
    }

    location / {
            try_files $uri $uri/ /index.php?$args;
            #try_files $uri $uri/ /index.html;
            location ~ \.php$ {
                    include fastcgi_params;
                    try_files $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;
            }
    }

    access_log /var/log/nginx/scripts.log scripts;
    error_log  /var/log/nginx/error.log debug;
}

Any advice?

Thanks!!!

UPDATE
Content of index.php files:

<html>
<body>
<?php
print "<pre>";
print_r($_SERVER);
print "</pre>";
?>
</body>
</html>

Best Answer

Solved!

I changed:

  • location /api/public => location ~ ^/api/public
  • under location /api/public change from

    try_files $uri $uri/ /api/public/index.php?$args; 
    

    to

    try_files $uri $uri/ /index.php?$args;
    
  • and the most important! Change location / to this:

    location / {
            try_files $uri $uri/ /index.php?$args;
            #try_files $uri $uri/ /index.html;
            location ~ \.php$ {
                set $php_root $document_root;
                if ($request_uri ~ ^/api/public) {
                    set $php_root /api/public;
                }
                    include fastcgi_params;
                    try_files $uri = 404;
                    fastcgi_split_path_info ^(.+\.php)(/.+)$;
                    fastcgi_pass 127.0.0.1:9000;
                    fastcgi_index index.php;
                    fastcgi_param SCRIPT_FILENAME $php_root$fastcgi_script_name;
            }
    }
    

The plus block is:

set $php_root $document_root;
if ($request_uri ~ ^/api/public) {
    set $php_root /api/public;
}

And of course change $document_root to $php_root