我正在将RoR应用程序部署到Amazon Elastic Beanstalk。 静态文件由nginx提供。 应用程序根目录是: /var/app/current ,它包含当前的Rails应用程序。 public目录树:
=> /var/app/current/public drwxr-xr-x 6 webapp webapp 4096 Aug 31 12:53 . drwxr-xr-x 13 webapp webapp 4096 Aug 31 12:54 .. -rw-r--r-- 1 webapp webapp 1722 Aug 31 12:52 404.html -rw-r--r-- 1 webapp webapp 1705 Aug 31 12:52 422.html -rw-r--r-- 1 webapp webapp 1635 Aug 31 12:52 500.html -rw-r--r-- 1 webapp webapp 0 Aug 31 12:52 apple-touch-icon.png -rw-r--r-- 1 webapp webapp 0 Aug 31 12:52 apple-touch-icon-precomposed.png drwxr-xr-x 3 webapp webapp 4096 Aug 31 12:54 assets drwxr-xr-x 2 webapp webapp 4096 Aug 31 12:52 debug -rw-r--r-- 1 webapp webapp 0 Aug 31 12:52 favicon.ico drwxr-xr-x 4 webapp webapp 4096 Aug 31 12:52 fonts drwxr-xr-x 3 webapp webapp 4096 Aug 31 12:52 images -rw-r--r-- 1 webapp webapp 98 Aug 31 12:52 robots.txt
问题是资产没有完全服务。 这是我推送给EBS服务器的nginxconfiguration:
# 0.02 upstream my_app { server unix:///var/run/puma/my_app.sock; } log_format healthd '$msec"$uri"' '$status"$request_time"$upstream_response_time"' '$http_x_forwarded_for'; server { listen 80; server_name _ localhost; # need to listen to localhost for worker tier if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})T(\d{2})") { set $year $1; set $month $2; set $day $3; set $hour $4; } access_log /var/log/nginx/access.log main; access_log /var/log/nginx/healthd/application.log.$year-$month-$day-$hour healthd; location /assets { alias /var/app/current/public/assets; gzip_static on; gzip on; expires max; add_header Cache-Control public; } location /fonts { alias /var/app/current/public/fonts; gzip_static on; gzip on; expires max; add_header Cache-Control public; } location /images { alias /var/app/current/public/images; gzip_static on; gzip on; expires max; add_header Cache-Control public; } location / { proxy_pass http://my_app; # match the name of upstream directive which is defined above proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
块/assets和/fonts被正确地提供,返回代码200。
但是/images返回301,redirect到带有斜线的URL,找不到:
如果我请求curl PNG图像,图像正确地服务。 redirect/images/stubs/banner.png – > 301 => /images/stubs/banner.png/ – > 404只在浏览器中发生。
有任何想法吗? 先谢谢你。
请检查您的mime.types文件是否包含以下行:
types{
...
image / png png;
...
}
这可能是nginx试图把它作为HTML服务,因为它不知道什么样的MIMEtypes,当它提供出来。
另外,请确保这个文件包含在你的nginx.conf文件中,而且你还没有意外删除它! :)应该是这样的:
html {
...
包括/etc/nginx/mime.types;
...
}
PNG已经是压缩格式,不需要在这里使用gzip。
尝试通过删除位置/图像的gzip压缩,并在此位置添加此行:
default_type image/png;
正如从开发工具显示,浏览器接收文本/ HTML而不是图像/ PNG