Symfony根目录下的wordpress博客的Nginx配置

时间:2019-01-06 11:16:27

标签: php wordpress symfony nginx nginx-config

我正在主根目录中运行Symfony 4应用程序,需要在/ blog上有一个Wordpress博客

这是我希望提供这些应用程序的方式:

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网站?

1 个答案:

答案 0 :(得分:1)

将wordpress文件放在/public/blog目录中,其中/public是symfony的公共目录,并按以下方式配置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套接字。