nginx实现动态分离,解决css和js等图片加载问题
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等图片加载问题的更多相关文章
- SpringMvc架构下css、js、jpg加载失败问题
SpringMvc架构下css.js.jpg加载失败问题 springMvc搭建成功后,页面出现一些错误,jsp.js等静态资源加载失败.导致页面没有显示任何样式以及 此处原因很简单,是因为相对路径在 ...
- js判断图片加载完成后获取图片实际宽高
通常,我们会用jq的.width()/.height()方法获取图片的宽度/高度或者用js的.offsetwidth/.offsetheight方法来获取图片的宽度/高度,但这些方法在我们通过样式设置 ...
- 关于html,css,js三者的加载顺序问题
<head lang="en"> <meta charset="utf-8"> <title></title> ...
- 性能优化之html、css、js三者的加载顺序
前言 我们知道一个页面通常由,html,css,js三部分组成,一般我们会把css文件放在head头部加载,而js文件则放在页面的最底部加载,想要知道为什么大家都会不约而同的按照这个标准进行构建页面, ...
- js img图片加载失败,重新加载+断网检查
我们常常会遇到img加载图片的时候因为网络问题或者图片过大导致图片加载失败的问题,页面就因为这张蹦掉的图变得不美观.所以我们需要图片加载失败的时候重新加载图片,前端图片加载优化 //js方法定义 fu ...
- welcome-file-list设置问题之css,js文件无法加载
web.xml里的welcome-file-list里设置默认访问页面为/html/index.html 但是在访问时,页面CSS都没加载. 正常输入网址却没问题.用/html/index.jsp也没 ...
- html中的图片、css、js等路径加载问题
网页文件的存取路径有3种:物理路径.绝对路径和相对路径. 物理路径就是你的文件放在主机上的具体位置,例如:D:\\image\\1.jpg 这种格式,该方法可以很快确定出你的文件,但是在网页显示路径基 ...
- mvc BundleConfig实现对Css、Js压缩打包加载
Bundle不是.net Framework框架中的一员,使用Bundle首先要先添加引用,如下: nuget包管理--程序包管理控制台--Install-Package Microsoft.AspN ...
- nginx 反向代理转发导致css,js,图片失效
为什么80%的码农都做不了架构师?>>> 需要添加以下配置 location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { proxy_pass htt ...
随机推荐
- ASP.NET 使用AJAX让GridView的数据行显示提示框(ToolTip)
介绍ASP.NET AJAX可以使你的web应用程序具有更丰富的功能和更多的用户响应. 本文中,我将演示如何通过ASP.NET AJAX的帮助,给像GridView这样的数据绑定控件的数据行增加pop ...
- C# SortedList类概念和示例
SortedList 类 [C#] 命名空间: System.Collections 表示键/值对的集合,这些键和值按键排序并可按照键和索引访问. SortedList 是 Hashtable 和 A ...
- 【JS复习笔记】01 基本语法
数字: JS只有一种数字类型,相当于double.(不知道为什么,我每次打double输入法都会出现逗比了三个字) NaN是一个数值,可以用isNaN(number)检测NaN Infinity表示所 ...
- asp.net.mvc4在vs2010怎样创建mvc项目及它的结构
1.打开vs2012,创建mvc项目 文件-->新建--> 项目--> web--> asp.net.Mvc 4web应用程序-->基本模板
- 重新想象 Windows 8.1 Store Apps (72) - 新增控件: AppBar, CommandBar
[源码下载] 重新想象 Windows 8.1 Store Apps (72) - 新增控件: AppBar, CommandBar 作者:webabcd 介绍重新想象 Windows 8.1 Sto ...
- 回文串---吉哥系列故事——完美队形II
HDU 4513 Problem Description 吉哥又想出了一个新的完美队形游戏! 假设有n个人按顺序站在他的面前,他们的身高分别是h[1], h[2] ... h[n],吉哥希望从中挑出 ...
- .NET AES加解密(128位)
AES加密(128位): /// <summary> /// 有密码的AES加密 /// </summary> internal static string Encrypt(s ...
- android开发布局文件imageview 图片等比例缩放:
ImageView的属性scaleType,如果等比缩放的话,就使用CenterInside,如果想固定大小的话,就CenterCrop <?xml version="1.0" ...
- 【GOF23设计模式】命令模式
来源:http://www.bjsxt.com/ 一.[GOF23设计模式]_命令模式.数据库事务机制底层架构实现.撤销和回复 package com.test.command; public cla ...
- Q:解决每天第一次打开MSCRM系统展示慢的问题
问题:第天第一次打开系统时,需要加载很长时间,基本为1分多钟,而第二次打开只需5秒. 解决方案:利用IIS中的Session. 一.打开IIS,选择打开服务器功能中“Session State”. 二 ...