2

I've been fighting with this the last couple days and I am hoping someone with a deeper nginx understanding can point me in the right direction.

I have need to serve a secondary PHP application from a subdirectory within an existing PHP applications "root" using PHP-FPM.

The structure is

- /var/www/www.example.com/
  - public/
    - index.php
  - subapplication/
    - public/
      - index.php

Both of these applications would be available from the same domain at www.example.com and www.example.com/subapplication/.

I can serve either of these applications directly with nginx but I am struggling to get my directives to work so that I can properly serve both the main and sub applications.

This is the configuration I have at this point that feels closest, but it's still not quite working.

server {
    listen 80 default_server;
    server_name www.example.com;

    set $base /var/www/www.example.com;

    index index.php;

    location /subapplication/ {
        alias $base/subapplication/public;
        try_files $uri /subapplication/index.php?$query_string;
        location ~ \.php$ {
            # 404
            try_files $fastcgi_script_name =404;

            # default fastcgi_params
            include fastcgi_params;

            # fastcgi settings
            fastcgi_pass        unix:/var/run/php/php7.2-fpm.sock;
            fastcgi_index       index.php;
            fastcgi_buffers     8 16k;
            fastcgi_buffer_size 32k;

            # fastcgi params
            fastcgi_param DOCUMENT_ROOT     $realpath_root;
            fastcgi_param SCRIPT_FILENAME   $realpath_root$fastcgi_script_name;
        }
    }

    location / {
        root $base/public;
        try_files $uri $uri/ /index.php?$query_string;
        location ~ \.php$ {
            # 404
            try_files $fastcgi_script_name =404;

            # default fastcgi_params
            include fastcgi_params;

            # fastcgi settings
            fastcgi_pass        unix:/var/run/php/php7.2-fpm.sock;
            fastcgi_index       index.php;
            fastcgi_buffers     8 16k;
            fastcgi_buffer_size 32k;

            # fastcgi params
            fastcgi_param DOCUMENT_ROOT     $realpath_root;
            fastcgi_param SCRIPT_FILENAME   $realpath_root$fastcgi_script_name;
        }
    }
}

Am I missing something obvious?

Currently I simply get a 404 for all requests to the /subapplication/ route. Adding $uri/ to the try_files under the location /subapplication/ gives me a 403 stating it can't index the subapplication/public folder.

I would deeply appreciate any insight someone might be able to offer.

2
  • See this answer Apr 1, 2020 at 7:06
  • @RichardSmith Thank you for pointing me in the right direction. I missed your comment at first. Your answer got me where I needed to go! I'll update once I wrap this up to point people that way in the future.
    – ephbaum
    Apr 1, 2020 at 18:56

2 Answers 2

4

As a sign post to anyone encountering this question later, there's a long standing issue with alias and try_files that was vexing me.

The solution can be found in Richard Smith's answer

My working configuration:

# www.example.com configuration #

server {
    listen 80 default_server;
    server_name www.example.com;

    set $base /var/www/www.example.com;
    root $base/public;

    index index.php;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    # @SEE https://serverfault.com/a/828579/212904 #
    location ^~ /subapplication {
        alias $base/subapplication/public;

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

        location ~ \.php$ {
            if (!-f $request_filename) {
                rewrite ^ /subapplication/index.php last;
            }

            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
            fastcgi_param SCRIPT_FILENAME $request_filename;
        }
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
    }
}
1
  • 1
    Just created my account to appreciate this answer, very helpful. Thanks Apr 20, 2022 at 10:49
0

Try the following

server {
        listen 80;

        root /var/www/www.example.com;

        index index.php index.html index.htm index.php index.nginx-debian.html;

        server_name www.example.com;

        location ~* ^.+\.(js|css|ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
                access_log off;
                add_header Access-Control-Allow-Origin *;
                expires 30d;
        }

        location / {
                try_files $uri $uri/ /index.php?q=$uri&$args;
        }

        location = /xmlrpc.php {
              return 404;
        }

        location /subapplication {
            try_files $uri $uri/subapplication /subapplication/index.php?q=$uri&$args;
        }

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

        location  /. {
                return 404;
        }
}
5
  • Thank you for your suggestion, I have tried this but I get a different error from of a 404: [error] 1712#1712: *1 open() "/usr/share/nginx/htmlindex.php" failed (2: No such file or directory), ...
    – ephbaum
    Apr 1, 2020 at 14:36
  • you can use the above config(edited)
    – Vinoth Rc
    Apr 1, 2020 at 17:24
  • Thank you for your updated attempt, but this still does not get me where I need to go. While the base application call to / still works, I am getting a 404 for calls to /subapplication/ without any error in the logs.
    – ephbaum
    Apr 1, 2020 at 18:47
  • shall I know your framework?
    – Vinoth Rc
    Apr 2, 2020 at 9:11
  • See my answer for how this was solved with the help of Richard Smith's answer. For the record, I am using both Laravel and pre-Laminas Apigility in this particular project.
    – ephbaum
    Apr 2, 2020 at 14:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .