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

Say*_*han 10 php wordpress ruby-on-rails nginx mime-types

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

  • 资源解释为样式表,但使用MIME类型text/html进行传输
  • 资源解释为脚本,但使用MIME类型text/html进行传输

一直在试图解决这个问题并在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;
}
Run Code Online (Sandbox Code Playgroud)

Say*_*han 7

经过大量的搜索,我终于找到了这个解决方案.

似乎问题是我需要在"位置/博客"中向应用添加根,并在/ blog中嵌套"location~.php $".这是我的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;
}
Run Code Online (Sandbox Code Playgroud)


Tan*_*Tat 6

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

Syntax:     types { ... }
Default:    

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

Context:    http, server, location
Run Code Online (Sandbox Code Playgroud)

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

types {
    text/css                     css;
    application/javascript       js;
    application/json             json;
}
Run Code Online (Sandbox Code Playgroud)

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