【问题标题】:Nginx configuration for a wordpress blog in a subfolder of Symfony rootSymfony 根目录下 wordpress 博客的 Nginx 配置
【发布时间】:2019-01-07 01:12:48
【问题描述】:

我正在主根目录中运行 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 网站?

【问题讨论】:

  • 我认为您需要确定我认为的第 10 行。 root $host_path/public;
  • @Ahmedbhs 刚刚尝试过,但得到了相同的结果

标签: php wordpress symfony nginx nginx-config


【解决方案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;
}

就像我在 cmets 中所说,确保您在 fastcgi_pass 指令中使用正确的 php 套接字。

【讨论】:

  • 现在为 website.com/blog 提供了 502 错误网关
  • @Martin 让你使用 php5 套接字。我粘贴了一个php7,因为我使用的是php7。我的答案已经过检查 - 我在我的生产设置中使用它并且它正在工作。或者更好地检查您使用的是哪一个,因为在您的 blog 位置您有 fastcgi_pass unix:/var/run/php5-fpm.sock; 而在您的 / 位置您正在使用 fastcgi_pass localhost:9000; - 这是两个不同的东西
  • @Martin 我已经用完整的配置示例更新了我的答案。
猜你喜欢
  • 2014-09-11
  • 1970-01-01
  • 1970-01-01
  • 2016-11-03
  • 1970-01-01
  • 2019-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多