0

It's unfortunate, but I am forced to make a wordpress and laravel application not only coexist but be aware of each other's state. I have this functioning via a wordpress plugin, laravel package and Apache configuration, but now I need to translate the configuration to work with nginx as the webserver.

My repository is set up like so:

/src - laravel application, underneath which /public contains the index.php entrypoint into the application

/wordpress - wordpress application

I have the following Apache VirtualHost config that works does exactly what I need:

<VirtualHost *:80>
    ServerName app.local
    DocumentRoot /var/www/vhosts/app.local/wordpress
    ErrorLog /var/www/vhosts/logs/app_local_error_log

     <Directory "/var/www/vhosts/app.local/wordpress">
         Options Indexes FollowSymlinks MultiViews
         AllowOverride All
         Require all granted
         DirectoryIndex index.php
    </Directory>

    <Directory /var/www/vhosts/app.local/src/public>
            DirectoryIndex index.php
            Options FollowSymLinks MultiViews
            AllowOverride All
    </Directory>

     Alias /xyz /var/www/vhosts/app.local/src/public

     <Location "/xyz">
         AllowOverride All
     </Location>
</VirtualHost>

Everything under /xyz specifically is handled by laravel. Everything else, root included, is handled by wordpress.

Here is my nginx config(I am testing this out locally with a laravel/homestead box):

server {
listen 80;
server_name .app.local;
root "/home/vagrant/code/app/wordpress";

index index.html index.htm index.php;

charset utf-8;

location /xyz/ {
    alias /home/vagrant/code/app/src/public/;
    try_files $uri $uri/ /index.php?$query_string;
}

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

location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt  { access_log off; log_not_found off; }

access_log off;
error_log  /var/log/nginx/app.local-error.log error;

sendfile off;

client_max_body_size 100m;

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

location ~ /\.ht {
    deny all;
}
}

I feel I am very close, however it seems that wordpress still ends up handling the request regardless of whether I use alias or root, append trailing slashes to the paths, or rearrange the location directives. I just don't know enough about nginx to debug this, or what my alternatives are configuration-wise. Any help would be greatly appreciated!

I have also tried setting a specific location directive and customizing the parameters I pass into fastcgi, as seen here, and that seems to have no effect:

https://gist.github.com/mnshankar/9642844

jvnk
  • 123
  • 4
  • You are passing PHP requests back to WordPress in your `location /xyz/`. – Michael Hampton Feb 18 '18 at 20:53
  • I can see that is the case now - I tried removing the prefix slash from the `try_files` directive, which errors out in the logs as trying to resolve `/home/vagrant/code/app/wordpressindex.php`. However I am unsure how to configure the directive to be relative to my laravel app's directory instead of wordpress. Perhaps I am misusing `alias` here. Thanks for the second pair of eyes, I am very new to nginx! – jvnk Feb 19 '18 at 18:05

0 Answers0