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-80/webapps/ifm;
                if (-f $request_filename) {
                expires 1d;
                break;
                }
        }
    
        location ~ .*\.(js|css)$
                {
                root /usr/local/tomcat/tomcat-80/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
1; #全局错误日志定义类型,[ 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
65535; } #设定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 0; keepalive_timeout 65;#长连接超时时间,单位是秒 #FastCGI相关参数是为了改善网站的性能:减少资源占用,提高访问速度。下面参数看字面意思都能理解。 fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; fastcgi_buffer_size 64k; fastcgi_buffers 4 64k; fastcgi_busy_buffers_size 128k; fastcgi_temp_file_write_size 128k; #gzip模块设置 gzip on; #开启gzip压缩输出 gzip_min_length 1k; #最小压缩文件大小 gzip_buffers 4 16k; #压缩缓冲区 #gzip_http_version 1.0; #压缩版本(默认1.1,前端如果是squid2.5请使用1.0) gzip_comp_level 2; #压缩等级 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:8080 weight=3;#tomcat 8080端口 #服务器2 #server 120.25.153.204:8081 weight=1;#tomcat 8081端口 server 120.27.137.142 weight=3; } server { #监听的端口 listen 80; server_name aly.com; #编码格式 charset utf-8; #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 90; #nginx跟后端服务器连接超时时间(代理连接超时) proxy_send_timeout 90; #后端服务器数据回传时间(代理发送超时) proxy_read_timeout 90; #连接成功后,后端服务器响应时间(代理接收超时) proxy_buffer_size 4k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小 proxy_buffers 4 32k; #proxy_buffers缓冲区,网页平均在32k以下的设置 proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*2) proxy_temp_file_write_size 64k; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { root /usr/local/tomcat/tomcat-80/webapps/ifm; if (-f $request_filename) { expires 1d; break; } } location ~ .*\.(js|css)$ { root /usr/local/tomcat/tomcat-80/webapps/ifm; if (-f $request_filename) { expires 1d; break; } } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # 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 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 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; # } #} }

 

posted @ 2016-02-22 09:36  IT-Jack  阅读(39544)  评论(5编辑  收藏  举报