본문 바로가기
프로그램개발/온라인 마케팅(SEO,SEM)

웹페이지 최적화를 위한 몇 가지 방법

by 크레도스 2013. 10. 2.

출처:http://hobbiez.tistory.com/394

 

1. PHP 웹 페이지를 gzip으로 압축하여 전송


웹페이지를 gzip으로 압축하여 전송하기 위해서는

1
ob_start("ob_gzhandler");

를 소스 윗부분에 추가시켜준다.


그러면 해당 페이지를 gzip으로 압축하여 전송하게 되는데 보통, 70% 정도의 압축률을 보여준다.

텍스트라서 원체 작지만, 많은 요청을 받는 웹서버에서는 조금씩 모여 큰 도움이 될 수 있다.


압축을 하더라도 cpu 사용률이 크게 늘어나지는 않는다고 한다. 


2. JS, CSS 파일을 압축하여 전송


https://code.google.com/p/minify/ 를 이용하여 css와 js 파일도 압축하여 전송할 수 있다.


파일을 다운로드 받아서 서버에 압축을 풀어준다.

min 디렉토리만 있으면 되므로 min폴더를 루트에 풀어주고, 

루트에서 .htaccess 파일을 수정해준다. ( 없으면 만들고 )

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<IFMODULE mod_rewrite.c="">
RewriteEngine on
  
# You may need RewriteBase on some servers
#RewriteBase /min
  
# rewrite URLs like "/min/f=..." to "/min/?f=..."
RewriteRule ^([bfg]=.*)  index.php?$1 [L,NE]
</IFMODULE>
<IFMODULE mod_env.c="">
# In case AddOutputFilterByType has been added
SetEnv no-gzip
</IFMODULE>
RewriteRule ^(.*\.(css|js))$ ./min/index.php?f=$1 [L]


위와 같이 만들어주고, min 디렉토리로 이동한다. 

config.php 파일을 에디트한다. 

$min_cachePath = "./tmp";  로 수정해준다. 주석처리 된 것을 삭제해주고 경로를 수정해주면 된다.

그 후 tmp 디렉토리를 생성 해 준 후 chmod 707 tmp로 권한을 설정해준다.


이렇게 해주면 css 파일과 js 파일도 gzip으로 압축하여 전송하게 된다.

fiddler로 확인해보면 된다.


3. Expires 설정을 하여 캐쉬를 사용.

.htaccess 파일을  수정해준다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
ExpiresActive on
ExpiresByType application/x-javascript "access plus 1 month"
ExpiresByType text/css "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/bmp "access plus 1 month"
ExpiresByType image/cgm "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/tiff "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"
ExpiresByType text/javascript "access plus 1 month"
ExpiresByType text/x-javascript "access plus 1 month"

를 입력해준다.

이러면 캐시폴더에 있는 파일들은 다시 받거나 하지 않으므로 페이지 로딩 속도를 더 빠르게 할 수 있으며,

서버의 부하도 감소 시킬 수 있다.