참고용 (gzip, minify js/css, robots.txt 등등)
# yum install perl-devel perl-ExtUtils-Embed
# yum install cpan
# cpan JavaScript::Minifier::XS
# cpan CSS::Minifier::XS
nginx 설치시 --with-http_perl_module 포함
# ./configure --prefix=/usr/local/nginx \
> --sbin-path=/usr/local/nginx/sbin/nginx \
> --conf-path=/usr/local/nginx/conf/nginx.conf \
> --pid-path=/log/nginx/nginx.pid \
> --error-log-path=/log/nginx/error.log \
> --http-log-path=/log/nginx/access.log \
> --with-http_ssl_module --with-http_perl_module
nginx.conf
user ec2-user;
# Server CPU core count
worker_processes 2;
pid /data/nginx/nginx.pid;
events {
# connection count per worker_process (concurrent count = worker_processes * worker_connections)
worker_connections 1024;
multi_accept on;
use epoll;
}
http {
# Let NGINX get the real client IP for its access logs
#set_real_ip_from 127.0.0.1;
#real_ip_header X-Forwarded-For;
include /usr/local/nginx/conf/mime.types;
default_type application/octet-stream;
# Logging Settings
access_log /log/nginx/access.log;
error_log /log/nginx/error.log;
# Log Format
log_format main '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
# Basic Settings
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 20;
client_max_body_size 15m;
client_body_timeout 60;
client_header_timeout 60;
client_body_buffer_size 1K;
client_header_buffer_size 1k;
large_client_header_buffers 4 8k;
send_timeout 60;
reset_timedout_connection on;
types_hash_max_size 2048;
server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
# Gzip Settings
gzip on;
#gzip_static on;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_min_length 512;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/css text/javascript text/xml text/plain text/x-component application/javascript application/x-javascript application/json application/xml application/rss+xml font/truetype application/x-font-ttf font/opentype application/vnd.ms-fontobject image/svg+xml;
perl_modules perl;
perl_require Minify.pm;
# Virtual Host Configs
#include /etc/nginx/conf.d/*.conf;
#include /etc/nginx/sites-enabled/*;
server {
# Configure the domain
listen 80;
server_name localhost;
port_in_redirect off;
server_tokens off;
autoindex off;
client_max_body_size 15m;
client_body_buffer_size 128k;
root /home/test;
# Define default caching of 24h
expires 86400s;
add_header Pragma public;
add_header Cache-Control "max-age=86400, public, must-revalidate, proxy-revalidate";
location / {
index index.html index.htm;
}
location ~* \.css$ {
try_files $uri.min.css @minify_css;
expires 31536000s;
access_log off;
log_not_found off;
add_header Pragma public;
add_header Cache-Control "max-age=31536000, public";
}
location ~* \.js$ {
try_files $uri.min.js @minify_js;
expires 31536000s;
access_log off;
log_not_found off;
add_header Pragma public;
add_header Cache-Control "max-age=31536000, public";
}
location @minify_css {
perl Minify::css_handler;
}
location @minify_js {
perl Minify::js_handler;
}
# Aggressive caching for static files
# If you alter static files often, please use
# add_header Cache-Control "max-age=31536000, public, must-revalidate, proxy-revalidate";
location ~* \.(asf|asx|wax|wmv|wmx|avi|bmp|class|divx|doc|docx|eot|exe|gif|gz|gzip|ico|jpg|jpeg|jpe|mdb|mid|midi|mov|qt|mp3|m4a|mp4|m4v|mpeg|mpg|mpe|mpp|odb|odc|odf|odg|odp|ods|odt|ogg|ogv|otf|pdf|png|pot|pps|ppt|pptx|ra|ram|svg|svgz|swf|tar|t?gz|tif|tiff|ttf|wav|webm|wma|woff|wri|xla|xls|xlsx|xlt|xlw|zip)$ {
expires 31536000s;
access_log off;
log_not_found off;
add_header Pragma public;
add_header Cache-Control "max-age=31536000, public";
}
# Deny access to hidden files
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
# Don't log robots.txt requests
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
# Deliver a static 404
#error_page 404 /404.html;
#location /404.html {
# internal;
#}
# Deliver 404 instead of 403 "Forbidden"
error_page 403 = 404;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# 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;
# server_name localhost;
# location / {
# root /home/ec2-user/web;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443;
# server_name localhost;
# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_timeout 5m;
# ssl_protocols SSLv2 SSLv3 TLSv1;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
Minify.pm
package Minify;
use nginx;
use JavaScript::Minifier::XS;
use CSS::Minifier::XS;
sub css_handler {
my $r = shift;
my $cache_file = $r->filename . ".min.css";
my $filename = $r->filename;
local $/=undef;
return DECLINED unless -f $filename;
open(INFILE, $filename) or die "Error reading file: $!";
my $css = <INFILE>;
close(INFILE);
open(OUTFILE, '>' . $cache_file) or die "Error writing file: $!";
print OUTFILE CSS::Minifier::XS::minify($css);
close(OUTFILE);
$r->send_http_header('text/css');
$r->sendfile($cache_file);
return OK;
}
sub js_handler {
my $r = shift;
my $cache_file = $r->filename . ".min.js";
my $filename = $r->filename;
local $/=undef;
return DECLINED unless -f $filename;
open(INFILE, $filename) or die "Error reading file: $!";
my $js = <INFILE>;
close(INFILE);
open(OUTFILE, '>' . $cache_file) or die "Error writing file: $!";
print OUTFILE JavaScript::Minifier::XS::minify($js);
close(OUTFILE);
$r->send_http_header('application/javascript');
$r->sendfile($cache_file);
return OK;
}
1;
robot.txt
User-agent: *
Disallow: /resources/
perl을 이용한 minify에 대한 참고 자료는 아래 링크에서 도움..
http://www.thatdoesntwork.net/tech-tricks/automatic-minification-with-nginx/