Rails 2和Nginx:https页面无法加载css或js(但会加载图形)

时间:2013-11-11 11:57:03

标签: css ruby-on-rails ssl https nginx

我正在向我的rails网站添加一些https页面。为了在本地测试它,我在一个mongrel_rails实例(在3000)和nginx下运行我的网站。

我已经设法让我的nginx配置到我可以实际访问https页面的位置,然后加载。除此之外,javascript和css文件都无法加载:查看chrome web tools中的Network选项卡,我可以看到它正在尝试通过https url加载它们。例如,其中一个非工作文件网址是

https://cmw-local.co.uk/stylesheets/cmw-logged-out.css?1383759216

我在我的nginx配置中设置(或至少认为我这样做)以重定向到静态文件的http版本。这似乎适用于图形,但不适用于css和js文件。

如果我在“网络”标签中单击此按钮,则会转到上面的网址,该网址会重定向到http版本。因此,重定向似乎在某种意义上起作用,但不是在它们被https页面加载时。就像我说的,我认为我在下面的配置中的第二个try_files指令中有这个,但可能没有。

谁能看到我做错了什么?谢谢,Max

这是我的nginx配置 - 对不起它有点冗长!我认为错误可能出现在第一个(ssl)服务器块中:

注意:这里的网址(elearning.dev,cmw-dev.co.uk等)都只是本地主机名,即它们只是127.0.0.1的别名。

server {
  listen 443 ssl;
  keepalive_timeout   70;

  ssl_certificate /home/max/work/charanga/elearn_container/elearn/config/nginx/certs/max-local-server.crt;
  ssl_certificate_key /home/max/work/charanga/elearn_container/elearn/config/nginx/certs/max-local-server.key;

  ssl_session_cache shared:SSL:10m;
  ssl_session_timeout 10m;
  ssl_protocols SSLv3 TLSv1;
  ssl_ciphers RC4:HIGH:!aNULL:!MD5;
  ssl_prefer_server_ciphers on;

  server_name elearning.dev cmw-dev.co.uk cmw-dev.com cmw-nginx.co.uk cmw-local.co.uk;

  root /home/max/work/charanga/elearn_container/elearn;

  # ensure that we serve css, js, other statics when requested
  # as SSL, but if the files don't exist (i.e. any non /basket controller)
  # then redirect to the non-https version
  location / {
    try_files $uri @non-ssl-redirect;
  }

  # securely serve everything under /basket (/basket/checkout etc)
  # we need general too, because of the email/username checking
  location ~ ^/(basket|general|cmw/account/check_username_availability) {
    # make sure cached copies are revalidated once they're stale
    add_header Cache-Control  "public, must-revalidate, proxy-revalidate";

    # this serves Rails static files that exist without running
    # other rewrite tests
    try_files $uri @rails-ssl;
    expires 1h;
  }

  location @non-ssl-redirect {
    return 301 http://$host$request_uri;
  }

  location @rails-ssl {

    proxy_set_header  X-Real-IP  $remote_addr;
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;

    proxy_read_timeout 180; 

    proxy_next_upstream off;
    proxy_pass http://127.0.0.1:3000;
    expires 0d;
  }

}

#upstream elrs {
#  server 127.0.0.1:3000;
#}

server {
  listen       80;
  server_name elearning.dev cmw-dev.co.uk cmw-dev.com cmw-nginx.co.uk cmw-local.co.uk;

  root /home/max/work/charanga/elearn_container/elearn;

  access_log /home/max/work/charanga/elearn_container/elearn/log/access.log;
  error_log  /home/max/work/charanga/elearn_container/elearn/log/error.log debug;
  client_max_body_size  50M;
  index index.html index.htm;

  # gzip html, css & javascript, but don't gzip javascript for pre-SP2 MSIE6 (i.e. those *without* SV1 in their user-agent string)
  gzip  on;   
  gzip_http_version 1.1;
  gzip_vary on;
  gzip_comp_level 6;
  gzip_proxied any;
  gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript; #text/html 

  # make sure gzip does not lose large gzipped js or css files
  # see http://blog.leetsoft.com/2007/7/25/nginx-gzip-ssl
  gzip_buffers 16 8k;

  # Disable gzip for certain browsers.
  #gzip_disable "MSIE [1-6].(?!.*SV1)";
  gzip_disable "MSIE [1-6]";

  # blank gif like it's 1995
  location = /images/blank.gif {
    empty_gif;
  }

  # don't serve files beginning with dots
  location ~ /\. { access_log off; log_not_found off; deny all; }

  # we don't care if these are missing
  location = /robots.txt   { log_not_found off; }
  location = /favicon.ico   { log_not_found off; } 
  location ~ affiliate.xml { log_not_found off; }  
  location ~ copyright.xml { log_not_found off; }  

  # convert urls with multiple slashes to a single /
  if ($request ~ /+ ) {
    rewrite ^(/)+(.*) /$2 break;
  }

  # X-Accel-Redirect
  # Don't tie up mongrels with serving the lesson zips or exes, let Nginx do it instead
  location /zips {
   internal;
   root /var/www/apps/e_learning_resource/shared/assets;
  }

  location /tmp {
   internal;
   root /;
  }

  location /mnt{
   root /;
  }

  # resource library thumbnails should be served as usual
  location ~ ^/resource_library/.*/*thumbnail.jpg$ {
    if (!-f $request_filename) {
      rewrite ^(.*)$ /images/no-thumb.png
      break;
    }
    expires 1m;
  } 

    # don't make Rails generate the dynamic routes to the dcr and swf, we'll do it here
    location ~ "lesson viewer.dcr" {
      rewrite ^(.*)$ "/assets/players/lesson viewer.dcr" break;
    }

    # we need this rule so we don't serve the older lessonviewer when the rule below is matched
    location = /assets/players/virgin_lesson_viewer/_cha5513/lessonViewer.swf {
      rewrite ^(.*)$ /assets/players/virgin_lesson_viewer/_cha5513/lessonViewer.swf break;
    }

    location ~ v6lessonViewer.swf {
      rewrite ^(.*)$ /assets/players/v6lessonViewer.swf break;
    }

    location ~ lessonViewer.swf {
      rewrite ^(.*)$ /assets/players/lessonViewer.swf break;
    }

    location ~ lgn111.dat {
      empty_gif;
    } 

  # try to get autocomplete school names from memcache first, then
  # fallback to rails when we can't
  location /schools/autocomplete { 
    set $memcached_key $uri?q=$arg_q;
    memcached_pass     127.0.0.1:11211;
    default_type       text/html;

    error_page         404 =200 @rails; # 404 not really! Hand off to rails
  }  

  location / {

    # make sure cached copies are revalidated once they're stale
    add_header Cache-Control  "public, must-revalidate, proxy-revalidate";

    # this serves Rails static files that exist without running other rewrite tests
    try_files $uri @rails;

    expires 1h;
  }

  location @rails {

    proxy_set_header  X-Real-IP  $remote_addr;
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;

    proxy_read_timeout 180; 

    proxy_next_upstream off;
    proxy_pass http://127.0.0.1:3000;
    expires 0d;
  }
}
编辑:我刚刚想到这可能在超级用户或服务器故障上更好,或者两者兼而有之。我不确定跨网站发布规则是什么。

0 个答案:

没有答案