一次css页面加载异常的折腾

1       原始需求

近期在搭建平台,因多域名会分割流量,所以希望将类似

ansible.178linux.com  salt.178linux.com qa.178linux.com 这些平台整合为一个平台,所示如下

ansible.178linux.com =è www.178linux.com/doc/ansible

salt.178linux.com =è www.178linux.com/doc/salt

qa.178linux.com =è www.178linux.com/doc/qa

以些方式最大程度提高平台整体权重。整合过程中出现一个css页面加载异常问题特总结分享

2       问题回放

一次css页面加载异常的折腾

如图: 右浏览器页面css,js,图片等样式无法加载,显示丑陋,

Chrome F12 debug追踪后发现有部分样式不加载,但该页面所有请求均能正常请求并被回应,状态值均为200,

2.1     初步怀疑

2.1.1  css,js目录权限问题  —失败

这个问题容易解决,验证也不成问题,

# cd /data/webapps/doc

# chown www. ansible -R

2.1.2  样式文件copy遗漏  —失败

确保所有样式文件均没有遗漏,

但测试下来仍然样式渲染不正常

2.2     确认所有请求回应数据

没有其它办法,只能对比doc.178linux.com正常请求来逐个请求和回应数据逐一查看,确认每个请求和回应的数据是否全部都一样.经仔细查看果然发现问题了.

1.     部分css.js,能正常被辨别被正常解析为 text/css  text/javascripts,部分只能被解析为text/html

一次css页面加载异常的折腾

2.     查看页面源码并比对发现所有源码是一样的

3.       google  Content-Type: text/html 发现如下几篇文章

https://developer.mozilla.org/en-US/docs/Web/Security/Securing_your_site/Configuring_server_MIME_types

http://stackoverflow.com/questions/5413107/headercontent-type-text-css-is-working-in-firefox-and-chrome-but-in-inter 

2.2.1  初步怀疑

2.2.1.1 /etc/nginx/mime.types文件没有定义 css,js解析结构  —失败

和运营环境正常配置的nginx对比后没有异常

2.2.1.2 浏览器缓存或浏览器支持问题 –失败

一次css页面加载异常的折腾

发现firefox,chrome均有问题,但ie正常,但原因还是无从得知

 

3       从源开始

3.1     Nginx配置

server
	{
		listen 80 default;
		server_name www.178linux.com;
		index  index.php index.html;
		root  /data/webapps/;
                access_log /var/log/nginx/access.log;
                error_log /var/log/nginx/error.log;

		error_page   404   /404.html;
		location ~ [^/]\.php(/|$){
			try_files $uri =404;
			fastcgi_pass  127.0.0.1:9000;
			fastcgi_index index.php;
			include fastcgi.conf;
			#include pathinfo.conf;
			}

		location  ~  /doc/ansible/ {
		        index  index.php index.html;
			try_files $uri $uri/ =404;
			fastcgi_pass  127.0.0.1:9000;
			fastcgi_index index.php;
			include fastcgi.conf;
		}

		location /nginx_status {
			stub_status on;
			access_log   off;
		}

		location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
			{
			expires      30d;
			}

		location ~ .*\.(js|css)?$
			{
			expires      12h;
			}


               if (-f $request_filename/index.html){
               		rewrite (.*) $1/index.html break;
  	         }
	       if (-f $request_filename/index.php){
               		rewrite (.*) $1/index.php;
	         }
               if (!-f $request_filename){
        	        rewrite (.*) /index.php;
 		} 

	}
server
	{
		server_name doc.178linux.com;
		index  index.php index.html;
		root  /data/tran/build/html/;
                access_log /var/log/nginx/access.log;
                error_log /var/log/nginx/error.log;

		error_page   404   /404.html;
		location ~ [^/]\.php(/|$){
			try_files $uri =404;
			fastcgi_pass  127.0.0.1:9000;
			fastcgi_index index.php;
			include fastcgi.conf;
			#include pathinfo.conf;
			}

		location /nginx_status {
			stub_status on;
			access_log   off;
		}

		location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
			{
			expires      30d;
			}

		location ~ .*\.(js|css)?$
			{
			expires      12h;
			}


               if (-f $request_filename/index.html){
               		rewrite (.*) $1/index.html break;
  	         }
	       if (-f $request_filename/index.php){
               		rewrite (.*) $1/index.php;
	         }
               if (!-f $request_filename){
        	        rewrite (.*) /index.php;
 		} 

	}

清理思绪,从头再来,几经检查觉得可能性最大的还是nginx的配置问题,从这个点出发再次切入,增加如下配置后刷新再看,问题解决。 

找了一番官网发现没有特别合适的说明,从配置上看我的个人理解是:

Location匹配到字段后将不会继续查找其它匹配字段,因本页面中即有简单的html页面也有css,js等样式在解析过程中找不到对应配置,所以根据配置规则全部解析为html方式,但css,js样式以html的方式是无法正常解析,所以导致样式加载异常。

一次css页面加载异常的折腾

 

原创文章,作者:stanley,如若转载,请注明出处:http://www.178linux.com/5215

赞 (0)
stanleystanley
上一篇 2015-06-08 11:09
下一篇 2015-06-11 13:15

相关推荐

  • rpm包管理

    前言    RPM(简称RPM,全称为The RPM Package Manager)是在Linux下广泛使用的软件包管理器。RPM此名词可能是指.rpm的文件格式的软件包,也可能是指其本身的软件包管理器(RPM Package Manager)。最早由Red Hat研制,现在也由开源社区开发。RPM通常随附于Linux发行版,…

    Linux干货 2015-05-13
  • 马哥教育网络班20期+第6周课程练习

    请详细总结vim编辑器的使用并完成以下练习题 vim三种模式: 用法:vim [option…] FILE 编辑模式:     字符跳转:         h:向左移动一个字符     &…

    系统运维 2016-07-22
  • 马哥教育网络班20期+第4周课程练习

    1、复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限。 [root@localhost ~]# cp -r /etc/skel/ /home/tuser1 [root@localhost ~]# chmod&n…

    系统运维 2016-07-07
  • Hello World 集中营

    编程的人应该都知道什么是Hello World。这是一个最简单的程序,其只在屏幕上输出“Hello World”字样,这通常是初学者的在学习编程时的第一个示例。把打印出 “Hello World” 作为第一个范例程序,现在已经成为编程语言学习的传统。“Hello World”起源于Brian Kernighan 和Dennis MacAlistair Rit…

    2016-05-17
  • Linux基础知识—文件系统和常用命令解读

    一、    计算机组成         计算机组成(Computer Composition)指的是计算机系统结构(Computer Architecture)的逻辑实现,包括机器机内的数据流和控制流的组成及逻辑设计等。其任务是在指令集系统结构确定分配给硬件系…

    Linux干货 2016-09-18
  • 用PHP编写Hadoop的MapReduce程序

    Hadoop流 虽然Hadoop是用java写的,但是Hadoop提供了Hadoop流,Hadoop流提供一个API, 允许用户使用任何语言编写map函数和reduce函数.Hadoop流动关键是,它使用UNIX标准流作为程序与Hadoop之间的接口。因此,任何程序只要可以从标准输入流中读取数据,并且可以把数据写入标准输出流中,那么就可以通过Hadoop流使…

    Linux干货 2015-04-13