简体   繁体   English

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

[英]Nginx configuration for a wordpress blog in a subfolder of Symfony root

I am running a Symfony 4 application in the main root and need to have a Wordpress blog to be available at /blog 我正在主根目录中运行Symfony 4应用程序,需要在/ blog上有一个Wordpress博客。

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. 使用我当前的配置,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;
}

How can I have nginx to handle Wordpress site under the /blog location? 如何让Nginx在/ blog位置下处理Wordpress网站?

Put wordpress files in /public/blog directory, where /public is the public directory of symfony and configure nginx like that: 将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;
    }
  }

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. 就像我在评论中所说,请确保您在fastcgi_pass指令中使用了正确的php套接字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM