【问题标题】:PHP assets not being served by nginxnginx 不提供 PHP 资产
【发布时间】:2017-03-06 23:24:26
【问题描述】:

我已经安装了一个 WP 博客,并在 nginx 服务器块配置中为其添加了一个位置块。当访问example.com/blog 的博客或example.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/ 的所有 URL 的请求?我尝试添加以下位置块,但没有成功:

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

【问题讨论】:

    标签: php wordpress nginx


    【解决方案1】:

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

    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 了解更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-11
      • 2011-09-18
      • 2014-04-21
      • 1970-01-01
      • 2014-01-13
      • 2014-03-11
      • 1970-01-01
      相关资源
      最近更新 更多