Brotzka
Brotzka

Reputation: 2785

Running Wordpress on a subfolder within a Laravel app on NGINX

I'm having trouble configuring wordpress on a subfolder of my Laravel app on Laravel Forge (server is powered by nginx).

The Wordpress-app is located under /home/forge/my-domain.com/public/blog (my-domain.com contains my laravel app).

I've tried several answers from other questions here on SO, but I keep getting errors (mostly "too many redirects" or "502 bad gateway").

This is my current solution (taken from here - resulting in a 502 bad gateway, also for the admin-area of my wordpress-app):

# BLOG START
location @rewriteblog {
    rewrite ^(.*)$ /blog/index.php?q=$uri&$args;
}

location @rewriteblogadmin {
    rewrite ^(.*)$ /blog/wp-admin/index.php?q=$uri&$args;
}

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

location = /blog/robots.txt {
    allow all;
    log_not_found off;
    access_log off;
}

location /blog {
    try_files $uri @rewriteblog;
}

location /blog/wp-admin {
    try_files $uri @rewriteblogadmin;
}

location ~ ^/(blog|blog\/wp-admin)/(.*)\.php(/|$) {
    try_files $uri =404;
    fastcgi_index index.php;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    include fastcgi_params;

    fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
    fastcgi_param DOCUMENT_ROOT $realpath_root;

    fastcgi_intercept_errors on;
    fastcgi_buffers 16 16k;
    fastcgi_buffer_size 32k;
}
#BLOG-END

Another attempt was this (502 Bad Gateway):

location /blog {
    root      /home/forge/my-domain.com/public/blog;
    index     index.php;
    try_files $uri $uri/ /blog/index.php?q=$uri;
}

Another attempt was this (Admin-Area and startpage accessible, posts result in too many redirects):

location /blog {
    root      /home/forge/my-domain.com/public/;
    index     index.php;
    try_files $uri $uri/ /blog/index.php?q=$uri;
}

Another attempt was this (Admin-Area and startpage too many redirect, posts accessible):

location /blog {
    root      /home/forge/faaren.com/public/blog;
    index     index.php;
    try_files $uri $uri/ /index.php?q=$uri;
}

Any ideas how to configure my nginx to get this done?

Upvotes: 1

Views: 1733

Answers (1)

Danila Vershinin
Danila Vershinin

Reputation: 9855

Keep things simple, starting from a working state, then extend.

You didn't post your Laravel part of NGINX config, but let us assume that the essentials of it are:

root /home/forge/my-domain.com/public;
location / {
    try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
    fastcgi_pass ...
}

Now let's say we add a /blog/ with different root as you specified:

location /blog/ {
    root /home/forge/my-domain.com/public/blog; 
    try_files $uri $uri/ /index.php?$args;
}

Why this doesn't work is because for /blog/ URL, NGINX literally checks the existence (try_files) of /home/forge/my-domain.com/public/blog (specified root) + /blog/index.php (request URI).

If you set the root to the parent folder (as in, same to Laravel's webroot; in other words, not specify it at all), then NGINX tries: /home/forge/my-domain.com/public (root) + /blog/index.php (request URI), which is correct.

For a /blog/foo kind of URL (SEO friendly), you want NGINX to use root + /blog/index.php.

Thus, the complete config:

root /home/forge/my-domain.com/public;
location / {
    try_files $uri $uri/ /index.php?$query_string;
}
location /blog/ {
    try_files $uri $uri/ /blog/index.php?$args;
}
location ~ \.php$ {
    fastcgi_pass ...
}

Upvotes: 4

Related Questions