Symfony root 子文件夹中 wordpress 博客的 Nginx 配置

Nginx configuration for a wordpress blog in a subfolder of Symfony root

我是 运行 主根中的 Symfony 4 应用程序,需要有一个 Wordpress 博客才能在 /blog

这是我希望应用程序的服务方式:

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

使用我当前的配置,Symfony 应用程序可以正常运行,但博客返回 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;
}

如何让 nginx 处理 /blog 位置下的 Wordpress 站点?

将wordpress文件放在/public/blog目录下,其中/public是symfony的public目录,然后这样配置nginx:

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;
    }
  }

编辑: 这是完整的示例配置:

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;
}

正如我在评论中所说,确保您在 fastcgi_pass 指令中使用正确的 php 套接字。