Home > Software engineering >  Nginx Laravel - Moving blog from subdomain to /blog
Nginx Laravel - Moving blog from subdomain to /blog

Time:05-03

My server application uses Laravel, and I have until recently had a wordpress blog hosted at blog.server.com

I wish to change this to server.com/blog

I have updated the main nginx config by adding the following:

location ^~ /blog {
    root /home/ploi/blog.server.com/public;
    index index.php index.html index.htm;
    try_files $uri $uri/ /blog/index.php?$query_string;

    location ~ \.php$ {
        try_files $uri /index.php =404;
        fastcgi_split_path_info ^(. \.php)(/. )$;
        fastcgi_pass unix:/run/php/php7.4-fpm.sock;
        fastcgi_buffers 16 16k;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
 }

This kind of works. It loads the new /blog url and posts, but:

  • No static assets get loaded (js, png, css, etc)
  • server.com/blog/wp-admin/ loops infinitely

How can I solve this?

CodePudding user response:

You could try this, worked for me:

    location /blog {
        alias /home/ploi/blog.server.com/public;
        
        try_files $uri $uri/ @blogrewrite;

        location ~ \.php$ {
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $request_filename;
            fastcgi_pass unix:/run/php/php7.4-fpm.sock;
        }   
    }

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

I used a "named location" (@blogrewrite), but frankly I don't know why this was necessary, but it avoided the recursion.

  • Related