Report this

What is the reason for this report?

Nginx Config for serving Laravel from within a subdirectory of Wordpress

Posted on November 25, 2019

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?



This textbox defaults to using Markdown to format your answer.

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

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

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

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.