Nginx - Rails 上的 Wordpress 博客加载 mime 类型 text/html 的样式和脚本

Nginx - Wordpress blog on Rails loads styles and scripts with mime type text/html

我刚刚在 Rails 应用程序的 /blog 目录下安装了一个 Wordpress 博客,运行 在 Unicorn 和 Nginx 上,我的样式表和脚本没有在浏览器中正确加载时我转到我的域。com/blog 页面。 Chrome 控制台给我以下错误:

一直在努力解决这个问题,并在 SO 上尝试了很多解决方案,但仍然无法通过...似乎需要对我的 Nginx 配置进行一些更改,特别是 blog/php 位置。这是我的配置:

upstream unicorn {
  server unix:/tmp/unicorn.domain.sock fail_timeout=0;
}

server {
  server_name www.domain.com;
  return 301 $scheme://domain.com$request_uri;
}

server {
  listen 80 default deferred;
  server_name domain.com;
  root /home/dcs/htdocs/domain/current/public;

  access_log /home/dcs/htdocs/domain/log/access.log;
  error_log  /home/dcs/htdocs/domain/log/error.log;


  location /blog {
    try_files $uri $uri/ /blog/index.php?$args;
  }

  location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php-fpm.sock;

    fastcgi_index index.php;
    fastcgi_param  SCRIPT_FILENAME home/dcs/htdocs/domain/$fastcgi_script_name;
    include /etc/nginx/fastcgi_params;
  }


  location ^~ /assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

  try_files $uri/index.html $uri @unicorn;
  location @unicorn {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://unicorn;
  }

  error_page 500 502 503 504 /500.html;
  keepalive_timeout 10;
}

确保您在 nginx 配置中定义了 types 指令。

Syntax:     types { ... }
Default:    

types {
    text/html  html;
    image/gif  gif;
    image/jpeg jpg;
}

Context:    http, server, location

将文件扩展名映射到 MIME 类型的响应。扩展不区分大小写。可以将多个扩展名映射到一种类型,例如:

types {
    text/css                     css;
    application/javascript       js;
    application/json             json;
}

来源:http://nginx.org/en/docs/http/ngx_http_core_module.html#types

经过大量搜索,我终于找到了 this solution

似乎问题是我需要在 "location /blog" 中向应用程序添加根并将 "location ~ .php$" 嵌套在 /blog 中。这是我的 Nginx 配置,现在可以在使用 Unicorn 的 Rails 应用程序中用于 Wordpress 博客,以防其他人需要它:

upstream unicorn {
  server unix:/tmp/unicorn.domain.sock fail_timeout=0;
}

server {
  server_name www.domain.com;
  return 301 $scheme://domain.com$request_uri;
}

server {
  listen 80 default deferred;
  server_name domain.com;
  root /home/dcs/htdocs/domain/current/public;

  access_log /home/dcs/htdocs/domain/log/access.log;
  error_log  /home/dcs/htdocs/domain/log/error.log;

  location /blog {
    root /home/dcs/htdocs/domain;
    index index.php;

    location ~ \.php$ {
      fastcgi_split_path_info ^(.+\.php)(/.+)$;
      fastcgi_pass unix:/var/run/php-fpm.sock;

      fastcgi_index index.php;
      fastcgi_param  SCRIPT_FILENAME home/dcs/htdocs/domain/$fastcgi_script_name;
      include /etc/nginx/fastcgi_params;
    }
  }

  location ^~ /assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

  try_files $uri/index.html $uri @unicorn;
  location @unicorn {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://unicorn;
  }

  error_page 500 502 503 504 /500.html;
  keepalive_timeout 10;
}