2018年7月27日 16:52:16 新增信息

这个可能是Nginx版本的问题,最近在新的项目中用到了Nginx,是最近版本的Nginx,图片和js都可以加载出来。

改帖专门为使用nginx,通过nginx把请求转发到web服务器再返回客户端的时候,解决css和js和图片加载不出来的问题。

如果没安装nginx,请访问一下地址进行安装 http://www.cnblogs.com/sz-jack/p/5200283.html

之前对nginx负载均衡/反向代理虽然明白什么意思,可一直没做过,抽个空学了一下nginx,在搭建的时候发现通过nginx反向代理到web服务器的时候,发现web服务器的css和js等静态资源加载不了,网上找了一下资料解决了此问题,现分享一下给各位,希望能给遇到此问题的人一些帮助。

1.解决思路

当请求到web服务器js和css资源的时候,也是一个url指向到那些静态资源,这个时候nginx配置文件中配置了静态资源的访问方法,不是访问web服务器的静态资源,而且是访问nginx对应目录下面的静态资源。

客户端->nginx->nginx服务器css和js文件目录。

配置如下nginx.conf

#仅仅是对静态资源加载不出来的配置信息,下面会有全的配置文件信息。

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
#这目录是我web服务器的项目位置,这里的目录需要最好和web服务器的静态资源的目录一样,当请求web服务器的css文件的时候,nginx会获取url地址,然后根据url地址去 #访问url对应的本地目录资源
root /usr/local/tomcat/tomcat-/webapps/ifm;
if (-f $request_filename) {
expires 1d;
break;
}
} location ~ .*\.(js|css)$
{
root /usr/local/tomcat/tomcat-/webapps/ifm;
if (-f $request_filename) {
expires 1d;
break;
}

2016/02/20 18:49:42 [error] 8182#0: *968 open() "/usr/local/tomcat/tomcat-80/webapps/ifm/cache/global/img/gs.gif" failed (2: No such file or directory), client: 5.44.1

这个是nginx的error.log日志,我们看到在open 打开usr/local/tomcat/tomcat-80/webapps/ifm/cache/global/img/gs.gif这个文件的时候提示 No such file or directory,不存在此目录,这样可以直观的看到当访问图片和css等静态文件的时候它是会访问本地资源目录的

全的nginx配置文件如下

#定义Nginx运行的用户和用户组
user root;
#nginx进程数,建议设置为等于CPU总核心数。
worker_processes ;
#全局错误日志定义类型,[ debug | info | notice | warn | error | crit ]
error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#进程文件
pid logs/nginx.pid; #工作模式与连接数上限
events {
#参考事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; epoll模型是Linux 2.6以上版本内核中的高性能网络I/O模型,如果跑在FreeBSD上面,
#就用kqueue模型。
use epoll;
  #单个进程最大连接数(最大连接数=连接数*进程数)
worker_connections ;
} #设定http服务器
http {
include mime.types;#文件扩展名与文件类型映射表
default_type application/octet-stream; #默认文件类型 #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile off;
tcp_nopush on;#防止网络阻塞
tcp_nodelay on; #防止网络阻塞 #keepalive_timeout ;
keepalive_timeout ;#长连接超时时间,单位是秒 #FastCGI相关参数是为了改善网站的性能:减少资源占用,提高访问速度。下面参数看字面意思都能理解。
fastcgi_connect_timeout ;
fastcgi_send_timeout ;
fastcgi_read_timeout ;
fastcgi_buffer_size 64k;
fastcgi_buffers 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k; #gzip模块设置
gzip on; #开启gzip压缩输出
gzip_min_length 1k; #最小压缩文件大小
gzip_buffers 16k; #压缩缓冲区
#gzip_http_version 1.0; #压缩版本(默认1.,前端如果是squid2.5请使用1.)
gzip_comp_level ; #压缩等级
gzip_types text/plain application/x-javascript text/css application/xml;
#压缩类型,默认就已经包含text/html,所以下面就不用再写了,写上去也不会有问题,但是会有一个warn。
gzip_vary on;
#limit_zone crawler $binary_remote_addr 10m; #开启限制IP连接数的时候需要使用 upstream aly.com {
#服务器1
server 120.25.153.204: weight=;#tomcat 8080端口
#服务器2
#server 120.25.153.204: weight=;#tomcat 8081端口
server 120.27.137.142 weight=; } server {
#监听的端口
listen ;
server_name aly.com;
#编码格式
charset utf-; #access_log logs/host.access.log main; location /
{
proxy_pass http://aly.com;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
#后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#以下是一些反向代理的配置,可选。
proxy_set_header Host $host;
client_max_body_size 10m; #允许客户端请求的最大单文件字节数
client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数,
proxy_connect_timeout ; #nginx跟后端服务器连接超时时间(代理连接超时)
proxy_send_timeout ; #后端服务器数据回传时间(代理发送超时)
proxy_read_timeout ; #连接成功后,后端服务器响应时间(代理接收超时)
proxy_buffer_size 4k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小
proxy_buffers 32k; #proxy_buffers缓冲区,网页平均在32k以下的设置
proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*)
proxy_temp_file_write_size 64k;
} #error_page /.html; # redirect server error pages to the static page /50x.html
#
error_page /50x.html;
location = /50x.html {
root html;
} location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
root /usr/local/tomcat/tomcat-/webapps/ifm;
if (-f $request_filename) {
expires 1d;
break;
}
} location ~ .*\.(js|css)$
{
root /usr/local/tomcat/tomcat-/webapps/ifm;
if (-f $request_filename) {
expires 1d;
break;
}
} # proxy the PHP scripts to Apache listening on 127.0.0.1:
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#} # deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
} # another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen ;
# listen somename:;
# server_name somename alias another.alias; # location / {
# root html;
# index index.html index.htm;
# }
#} # HTTPS server
#
#server {
# listen ssl;
# server_name localhost; # ssl_certificate cert.pem;
# ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on; # location / {
# root html;
# index index.html index.htm;
# }
#} }

nginx实现动态分离,解决css和js等图片加载问题的更多相关文章

  1. SpringMvc架构下css、js、jpg加载失败问题

    SpringMvc架构下css.js.jpg加载失败问题 springMvc搭建成功后,页面出现一些错误,jsp.js等静态资源加载失败.导致页面没有显示任何样式以及 此处原因很简单,是因为相对路径在 ...

  2. js判断图片加载完成后获取图片实际宽高

    通常,我们会用jq的.width()/.height()方法获取图片的宽度/高度或者用js的.offsetwidth/.offsetheight方法来获取图片的宽度/高度,但这些方法在我们通过样式设置 ...

  3. 关于html,css,js三者的加载顺序问题

    <head lang="en"> <meta charset="utf-8"> <title></title> ...

  4. 性能优化之html、css、js三者的加载顺序

    前言 我们知道一个页面通常由,html,css,js三部分组成,一般我们会把css文件放在head头部加载,而js文件则放在页面的最底部加载,想要知道为什么大家都会不约而同的按照这个标准进行构建页面, ...

  5. js img图片加载失败,重新加载+断网检查

    我们常常会遇到img加载图片的时候因为网络问题或者图片过大导致图片加载失败的问题,页面就因为这张蹦掉的图变得不美观.所以我们需要图片加载失败的时候重新加载图片,前端图片加载优化 //js方法定义 fu ...

  6. welcome-file-list设置问题之css,js文件无法加载

    web.xml里的welcome-file-list里设置默认访问页面为/html/index.html 但是在访问时,页面CSS都没加载. 正常输入网址却没问题.用/html/index.jsp也没 ...

  7. html中的图片、css、js等路径加载问题

    网页文件的存取路径有3种:物理路径.绝对路径和相对路径. 物理路径就是你的文件放在主机上的具体位置,例如:D:\\image\\1.jpg 这种格式,该方法可以很快确定出你的文件,但是在网页显示路径基 ...

  8. mvc BundleConfig实现对Css、Js压缩打包加载

    Bundle不是.net Framework框架中的一员,使用Bundle首先要先添加引用,如下: nuget包管理--程序包管理控制台--Install-Package Microsoft.AspN ...

  9. nginx 反向代理转发导致css,js,图片失效

    为什么80%的码农都做不了架构师?>>>   需要添加以下配置 location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { proxy_pass htt ...

随机推荐

  1. C#实用杂记-EF全性能优化技巧

    原文链接:http://www.makmong.com/947.html#comment-31 EntityFramework 优化建议 2016年1月15日 下午4:54 LEILINKANG   ...

  2. markdown这么好用的东西我才知道。。。多么不折腾的我。。。

    markdown 锚点 努力吧 我的网站 之前有个域名phifan.com没续费被抢了,之后又买了phifan.cn没续费被抢了,还剩下个plusnet.cn说什么也不能再丢掉了! package c ...

  3. weblogic 12c下jxls导出excel报错Could not initialize class org.apache.poi.xssf.usermodel.XSSFVMLDrawing

    周一,开发反馈weblogic 12c下jxls导出excel报错,公司环境和UAT环境均报错,看日志如下: 2016-06-08 09:16:55,825 ERROR org.jxls.util.T ...

  4. [模仿][JS]新浪财经7*24直播

    使用新浪财经7*24直播的数据 简单的做一个山寨品 在线地址:[痛苦啊,有GFW,却没有vpn,往heroku上传浪费了好多时间...] http://wangxinsheng.herokuapp.c ...

  5. [Tips] Useful link ... on going

    1. CSS Bootstrap http://getbootstrap.com/ Bootstrap 中文文档 http://getbootstrap.com/2.3.2/ 最全的 Twitter ...

  6. How to upgrade workflow assembly in MOSS 2007

    This problem generally start when you are having an existing custom workflow and there are instances ...

  7. Python: 关于nose

    1. 使用rednose增强输出 pip install rednose nosetests --rednose tests 2. 使用coverage pip install coverage no ...

  8. Android—SQLITE数据库的设计和升降级

    Google为Andriod的较大的数据处理提供了SQLite,他在数据存储.管理.维护等各方面都相当出色,功能也非常的强大.SQLite具备下列特点: 1.轻量级 使用 SQLite 只需要带一个动 ...

  9. Android实战--电话拨号器

    今天跟着黑马视频建立一个android app--电话拨号器 首先新建一个android项目 activity_main_xml中的代码如下: <RelativeLayout xmlns:and ...

  10. C语言中的指针和内存泄漏

    引言 对于任何使用C语言的人,如果问他们C语言的最大烦恼是什么,其中许多人可能会回答说是指针和内存泄漏.这些的确是消耗了开发人员大多数调试时间的事项.指针和内存泄漏对某些开发人员来说似乎令人畏惧,但是 ...