一次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

相关推荐

  • PHP5.4的变化关注—What has changed in PHP 5.4.x

    What has changed in PHP 5.4.x Most improvements in PHP 5.4.x have no impact on existing code. There are a few incompatibilities and new features that should be …

    Linux干货 2015-06-17
  • linux 基础命令(四)

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

    Linux干货 2016-10-16
  • IoC/DIP其实是一种管理思想

    关于IoC的的概念提出来已经很多年了,其被用于一种面象对像的设计。我在这里再简单的回顾一下这个概念。我先谈技术,再说管理。 话说,我们有一个开关要控制一个灯的开和关这两个动作,最常见也是最没有技术含量的实现会是这个样子: 然后,有一天,我们发现需要对灯泡扩展一下,于是我们做了个抽象类: 但是,如果有一天,我们发现这个开关可能还要控制别的不单单是灯泡的东西,我…

    Linux干货 2016-08-15
  • 基础指令使用篇 Linux版

    回显,回声 echo echo hello > /dve/pts/1 打开图形界面 init 5   关闭图形界面 init 3 ctrl+alt+F1-6字符,F7图形界面 chvt 改变虚拟终端 chvt +数字 startx  图形的软件打开 poweroff centOS 6.8与7.2都正常关闭断电 hostname 显示…

    系统运维 2016-08-04
  • HTTP详解(3)-http1.0 和http1.1 区别

    翻了下HTTP1.1的协议标准RFC2616,下面是看到的一些它跟HTTP1.0的差别。 1. Persistent Connection持久连接        在HTTP1.0中,每对Request/Response都使用一个新的连接。      …

    Linux干货 2015-04-04
  • LVM详解

    Linux的LVM详解 LVM组成; LVM:logic volume manager .LVM即逻辑卷管理,现在使用版本为第二版,即version2 逻辑卷:pv,physical volume,即计算机上的磁盘设备,例如我的计算机上的/dev/sda3,/dev/sda5. 卷组:vg,volume group。一般由多个pv组成。 逻辑卷:lv,log…

    Linux干货 2016-02-14