Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/242.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Nginx-Wordpress-blog-on-Rails加载带有mime类型text/html的样式和脚本_Php_Ruby On Rails_Wordpress_Nginx_Mime Types - Fatal编程技术网

Php Nginx-Wordpress-blog-on-Rails加载带有mime类型text/html的样式和脚本

Php Nginx-Wordpress-blog-on-Rails加载带有mime类型text/html的样式和脚本,php,ruby-on-rails,wordpress,nginx,mime-types,Php,Ruby On Rails,Wordpress,Nginx,Mime Types,我刚刚在Rails应用程序的/blog目录下安装了一个Wordpress博客,运行在Unicorn和Nginx上,当我转到domain.com/blog页面时,我的样式表和脚本没有正确加载到浏览器中。Chrome控制台给了我以下错误: 解释为样式表但使用MIME类型text/html传输的资源 解释为脚本但使用MIME类型text/html传输的资源 我一直在努力解决这个问题,在这里尝试了很多解决方案,但仍然无法通过。。。似乎需要对我的Nginx配置进行一些更改,特别是对于blog/php位

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

  • 解释为样式表但使用MIME类型text/html传输的资源
  • 解释为脚本但使用MIME类型text/html传输的资源
我一直在努力解决这个问题,在这里尝试了很多解决方案,但仍然无法通过。。。似乎需要对我的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;
}

资料来源:

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

问题似乎是我需要在“location/blog”中为应用程序添加一个根目录,并在/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;
}

为了扩展,如果您的javascript和css文件在/blog路径之外加载良好,我建议您在nginx配置文件(包括主nginx.conf和任何virtualhost文件)中搜索“mime.types”(无引号)。您应该在顶级配置中包含此文件(即:nginx.conf)。听起来好像mime类型只是在/blog之外有条件地加载。我从未见过导致您正在详述的问题的配置。您的带有try_files指令的“location/blog”非常完美,应该可以正常提供静态文件。感谢您的回复-我目前在nginx.conf中有“include mime.types”,并且mime.types文件有您提到的带有css/javascript/等的types指令,Tan。我甚至尝试在/blog和/php位置内的特定vhost中执行“include/etc/nginx/mime.types”,但仍然没有成功…Nate-是的,我的样式和脚本都在“location/blog”之外正确加载,但由于某些原因,Wordpress博客中的任何页面都会不断遇到此mime类型错误。