Nginx Config for serving Laravel from within a subdirectory of Wordpress

November 25, 2019 225 views
Nginx PHP WordPress

I’m having an nginx problem. I have a WordPress installation and I’m trying to serve a laravel app from a subdirectory.

home/foo/var/www/html is WP
home/foo/var/www/html/app is Laravel
home/foo/var/www/html/app/public is what I want to serve

My current config (written after searching this forum and StackOverflow):

server {
        listen 80;
        listen [::]:80;

        root /home/foo/var/www/html;
        index index.php index.html index.htm;

        server_name foo.com www.foo.com;

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

        location / {
                try_files $uri $uri/ /index.php$is_args$args;
        }

        location = /favicon.ico { log_not_found off; access_log off; }
        location = /robots.txt { log_not_found off; access_log off; allow all; }
        location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
            expires max;
            log_not_found off;
        }

        location ~ \.php$ {
            fastcgi_split_path_info ^(.+?\.php)(/.*)$;
            fastcgi_pass   unix:/run/php/php7.2-fpm.sock;
            fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO $fastcgi_path_info;
            fastcgi_index  index.php;
            include        fastcgi_params;
        }

        location ^~ /app {
                alias home/foo/var/www/html/app/public;
                try_files $uri $uri/ @app;

                location ~ /app/.+\.php$ {
                        fastcgi_split_path_info ^(/app/.+?\.php)(/.*)$;
                        fastcgi_pass   unix:/run/php/php7.2-fpm.sock;
                        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
                        fastcgi_param  PATH_INFO $fastcgi_path_info;
                        fastcgi_index  index.php;
                }
        }

        location @app {
                rewrite /app/(.*)$ /app/index.php?/$1 last;
        }

        location ~ /\.ht {
                deny all;
        }

}

can anyone tell me what i’m doing incorrectly?

1 Answer

Hi @heynavigator,

Your Nginx configuration file looks okay with the only exception that you might want to make

alias home/foo/var/www/html/app/public;

as

root home/foo/var/www/html/app/public;

If this still doesn’t work out, can you confirm if you have any errors or what does happen when you load the website?

Additionally, I’ll recommend you to check this DigitalOcean article which explains how to configure Laravel with Nginx - Article

Indeed it doesn’t cover your case, however what it has been explained in it, can be applied to your configuration as well.

Regards,
KDSys

by Mateusz Papiernik
Laravel is one of the most popular open-source web application frameworks written in PHP. It aims to help developers build both simple and complex applications by making frequently-used application tasks (like caching and authentication) easier. In this tutorial, we will deploy a simple Laravel application with a production environment in mind.
  • Thanks for the response. I don’t get any errors that I can see, but currently I’m receiving a 404 on WordPress when trying to access Laravel on /app. In other attempts along the way I also received “No Input File”, an nginx 404, and a white screen. Changing ‘alias’ to 'root’ results in the white screen. I’ll investigate the article you sent, thank you.

    I had the application running before I added WordPress and rearranged the folders. Previously I had Laravel as my main project with WordPress in a subdirectory (“/blog”), but that conflicts with business requirements.

  • I think the problem is with the rewrite because I see this error: “Unable to open primary script: /home/foo/var/www/html/app/public/app/public/index.php
    but changing it to

            location @app {
                    rewrite /app/(.*)$ /index.php?/$1 last;
            }
    

    just gets me the wordpress 404
    and changing it to

            location @app {
                    rewrite /app/(.*)$ /public/index.php?/$1 last;
            }
    

    results in a similar No input file specified error.

Have another answer? Share your knowledge.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!