PHP资产不是由nginx提供的

时间:2017-03-06 14:37:40

标签: php wordpress nginx

我已经安装了一个WP博客,并在nginx服务器块配置上为它添加了一个位置块。在example.com/blogexample.com/blog/foo-bar-baz等任何博客帖子访问博客时,内容已投放,但所有资产都已丢失。

当我检查日志时,看起来所有对这些资产URL的请求都被永久重定向到根博客URL。以下是来自nginx访问日志的请求示例:

"GET /blog/wp-includes/js/jquery.min.js HTTP/1.1" 301 5 "https://example.com/blog/" "USER_AGENT_STRING"

以下是nginx配置:

server {
  listen 80;
  server_name www.example.com example.com;
  return 301 https://www.example.com$request_uri;
}

server {
  listen 443 ssl;
  server_name example.com www.example.com;
  root /home/example/current/public;
  index index.html index.htm;

  location ~ /blog {
    index index.php index.html index.htm;
    fastcgi_param  SCRIPT_FILENAME  /var/www/blog/index.php;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
  }

  location ~ \.php$ {
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index  index.php;
    fastcgi_buffers 256 128k;
    fastcgi_connect_timeout 300s;
    fastcgi_send_timeout 300s;
    fastcgi_read_timeout 300s;
    include fastcgi_params;
  }
}

如何编辑其中一个位置块,让nginx捕获包含/blog/wp-content//blog/wp-includes/的所有网址的请求?我尝试添加以下位置块,但它不起作用:

location ~ ^(/wp-content/|/wp-includes/)/.*\.(jpe?g|gif|css|png|js|ico|pdf|m4a|mov|mp3)$ {
  root /var/www/blog;
}

1 个答案:

答案 0 :(得分:2)

您的配置似乎无法提供静态内容。更传统的方法是使用嵌套位置来提供动态内容。例如:

location ^~ /blog {
    root /var/www;
    index index.php index.html index.htm;

    try_files $uri $uri/ /blog/index.php;

    location ~ \.php$ {
        try_files $uri =404;

        include fastcgi_params;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_param  SCRIPT_FILENAME  $request_filename;
    }
}

有关详情,请参阅this document