Laravel and WordPress integration routing using nginx

I am developing web app in Laravel 5.2. I have existing WordPress site. So, I want to integrate Laravel with WordPress. WordPress app has static pages. I have two separate directories for Laravel and WordPress in my root directory.

  1. laraApp
  2. wpApp

I want to make wpApp as default app. So when user clicks login button, user will be redirected to laraApp. I want wpApp at www.example.com and laraApp in www.example.com/laraApp. I have nginx web server running. So what should be my nginx config file?

Read More

Current nginx config file is :

server {
    listen 80;
    root /var/www/root/wpApp;
    index index.php index.html index.htm;
    server_name www.example.com;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
    # rewrite rules for laravel routes
        location /laraApp {
        rewrite ^/laraApp/(.*)$ /laraApp/public/index.php?$1 last;
    }
    location ~ .php$ {
        try_files $uri /index.php =404;
        fastcgi_split_path_info ^(.+.php)(/.+)$;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

Here my Laravel app is accessible using url www.example.com/laraApp/public/
I want to access it using www.example.com/laraApp.

Thanks.

Related posts

1 comment

  1. The configuration would be simpler if the base URI for each of the applications did not overlap. But given the constraints of your question, you must use two distinct document roots for the PHP section of each of the applications in your configuration.

    As you have placed one of your applications in /, the other application is kept separate by the use of nested location blocks. Notice the use of the ^~ modifier to prevent the first PHP block from processing Laravel requests.

    index index.php index.html index.htm;
    
    root /var/www/root/wpApp;
    
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
    
    location ~ .php$ {
        try_files $uri /index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }
    
    location ^~ /laraApp {
        rewrite ^/laraApp(.*)$ /laraApp/public$1 last;
    }
    
    location ^~ /laraApp/public {
        root /var/www/root;
    
        try_files $uri $uri/ /laraApp/public/index.php?$query_string;
    
        location ~ .php$ {
            try_files $uri /laraApp/public/index.php;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        }
    }
    

    I am away from my test system at the moment so the above has not been syntax checked or tested.

Comments are closed.