1

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

我已经设法让我的 nginx 配置达到我可以实际访问 https 页面的程度,并且它们会加载。除了 javascript 和 css 文件都无法加载:查看 chrome web 工具中的网络选项卡,我可以看到它正在尝试通过 https url 加载它们。例如,非工作文件 url 之一是

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

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

如果我在“网络”选项卡中单击它,它会将我带到上面的 url,该 url 重定向到 http 版本。因此,重定向似乎在某种意义上有效,但在它们由 https 页面加载时无效。就像我说的那样,我认为我在下面的配置中的第二个 try_files 指令中已经涵盖了这一点,但也许没有。

谁能看到我做错了什么?谢谢,马克斯

这是我的 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;
  }
}

编辑:我突然想到这在超级用户或服务器故障上可能会更好,或者两者兼而有之。我不确定跨站点发布规则是什么。

4

0 回答 0