0

I am running a Symfony 4 application in the main root and need to have a Wordpress blog to be available at /blog

This is how I would like the apps to be served:

website.com -> Symfony App (working)
website.com/blog -> Wordpress (returning 404)

With my current configuration, the Symfony app is working but the blog is returning a 404.

server {
    listen              *:443 ssl http2;
    server_name         www.website.com;

    client_max_body_size 1m;
    charset utf-8;

    set $host_path "/var/www/website.com";
    set $symfony_bootstrap "index.php";

    root $host_path/public;
    index  index.html index.htm index.php;

    location /blog {
        root /var/www/blog;
        index index.php;
        try_files $uri $uri/ /blog/index.php?q=$uri&$args;

        location ~ \.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
        }
    }

    location / {
        root   $host_path/public;
        try_files $uri $uri/ /$symfony_bootstrap$is_args$args;
        autoindex on;
        index index.php;
    }

    location ~ ^/index\.php(/|$) {
        set $path_info $fastcgi_path_info;
        root  $host_path/public;
        fastcgi_index index.php;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        try_files $uri $uri/ /index.php$is_args$args;
        fastcgi_pass   localhost:9000;
        include fastcgi_params;

        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
        fastcgi_param APP_ENV prod;
    }

    location ~ /.well-known {
        allow all;
    }

    # prevent nginx from serving dotfiles (.htaccess, .svn, .git, etc.)
    location ~ /\. {
        deny all;
        access_log off;
        log_not_found off;
    }

    # add caching to built resources
    location /build {
       alias $host_path/public/build/;
       access_log off;
       expires max;
    }

    server_tokens off;
    sendfile off;
}

How can I have nginx to handle Wordpress site under the /blog location?

2
  • I think you need to dete the line 10 i think. root $host_path/public; Commented Jan 6, 2019 at 13:46
  • @Ahmedbhs just tried now, but getting the same result Commented Jan 6, 2019 at 13:57

1 Answer 1

2

Put wordpress files in /public/blog directory, where /public is the public directory of symfony and configure nginx like that:

location /blog {
    root      /var/www/website.com/public;
    index     index.php;
    try_files $uri $uri/ /blog/index.php?$args;

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

EDIT: Here is full example config:

server {
  listen 80;
  server_name website.com
  root /var/www/website.com/public;

  location /blog {
    root      /var/www/website.com/public;
    index     index.php;
    try_files $uri $uri/ /blog/index.php?$args;

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

  location / {
    try_files $uri /index.php$is_args$args;
  }

  location ~ ^/index\.php(/|$) {
    fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
    fastcgi_split_path_info ^(.+\.php)(/.*)$;

    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
    fastcgi_param DOCUMENT_ROOT $realpath_root;  
    internal;
  }

  location ~ \.php$ {
    return 404;
  }

  error_log /var/log/nginx/website.com_error.log;
  access_log /var/log/nginx/website.com_access.log;
}

Like i said in the comments, make sure you are using correct php socket in your fastcgi_pass directives.

Sign up to request clarification or add additional context in comments.

3 Comments

That's giving now a 502 bad gateway for website.com/blog
@Martin Make sue you use php5 socket. I pasted php7 one, because i'm using php7. My answer is checked - i'm using it in my production setup and it is working. Or even better check which one you are using, because in your blog location you have fastcgi_pass unix:/var/run/php5-fpm.sock; and in your / location you are using fastcgi_pass localhost:9000; - that's 2 different things
@Martin I have updated my answer with full config example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.