메뉴 건너뛰기

NGINX
2016.01.16 21:17

Nginx 설정 예제 (nginx.conf)

조회 수 236 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

참고용 (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/ezmaru/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/

http://wiki.nginx.org/NginxEmbeddedPerlMinifyJS


List of Articles
번호 분류 제목 글쓴이 날짜 조회 수
25 NGINX nginx location 설정 맨하탄노숙자 2016.02.18 158
24 NGINX nginx 파일 공유 및 암호 설정하기 맨하탄노숙자 2016.02.18 332
23 NGINX nginx에서 동영상 스트리밍 서비스하기 맨하탄노숙자 2016.02.18 111
22 NGINX Nginx Tuning For Best Performance 맨하탄노숙자 2016.02.10 74
21 NGINX NGINX Load Balancing – TCP Load Balancer 맨하탄노숙자 2016.01.28 140
20 NGINX Build ngx_pagespeed From Source 맨하탄노숙자 2016.01.19 47
19 NGINX Nginx, FastCGI 맨하탄노숙자 2016.01.16 356
» NGINX Nginx 설정 예제 (nginx.conf) 맨하탄노숙자 2016.01.16 236
17 NGINX Nginx의 오픈 파일 캐시 맨하탄노숙자 2016.01.16 185
16 NGINX Nginx 의 Proxy cache 대리 캐시 및 부하 균형 똑바로 맨하탄노숙자 2016.01.16 220
15 NGINX nginx Proxy Cache 맨하탄노숙자 2016.01.16 52
14 NGINX 우분투 NGINX(엔진엑스) Configure 옵션 맨하탄노숙자 2016.01.16 169
13 NGINX 502 Bad Gateway 해결법 맨하탄노숙자 2016.01.16 536
12 NGINX Response Headers에 서버정보 숨기기 맨하탄노숙자 2016.01.16 71
11 NGINX Geo module 이용한 아이피별 분류 처리 맨하탄노숙자 2016.01.16 28
10 NGINX sub_filter 이용한 HTML 내에 태그값 변경 맨하탄노숙자 2016.01.16 44
9 NGINX Nginx Cache(캐쉬) 설정관련 참고 맨하탄노숙자 2016.01.16 59
8 NGINX Tools: ngxtop - real-time metrics for nginx server (and others) 맨하탄노숙자 2016.01.11 36
7 NGINX ngxtop python 맨하탄노숙자 2016.01.11 18
6 NGINX <PYTHON>[pip 설치] 맨하탄노숙자 2016.01.11 124
Board Pagination Prev 1 ... 2 Next
/ 2