雑記

インフラのことだったりプログラムのことだったりどうでもいいこと書いたり。要は雑記。

nginx+varnish+apache+phpの環境構築(2)

前回の続き。

というわけで設定を晒してみる。
今回はLBのnginxの設定。

基本設定

  • nginx.confの設定
worker_processes 1;

error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;


events {
    worker_connections 1024;
}


http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    server_tokens off;

    log_format main '$remote_addr - $remote_user [$time_local] "$request" '
    '$status $body_bytes_sent "$http_referer" '
    '"$http_user_agent" "$http_x_forwarded_for"';

    sendfile on;
    tcp_nopush on;

    keepalive_timeout 30;
    keepalive_requests 100;


    ## disable etag
    etag off;

    ## include deflate config file
    include /etc/nginx/conf.d/deflate.conf;

    ## include site setting
    include /etc/nginx/vh/www.hogehoge.com.conf;

    server {
        listen 8888;

        location /_server_status {
        stub_status on;
        access_log off;
        allow 127.0.0.1;
        deny all;
        }
    }
}

インクルードファイルの設定

圧縮設定

  • deflate.conf
gzip on;
gzip_http_version 1.0;
gzip_types text/plain
text/css
text/xml
application/x-javascript
application/xml
application/rdf+xml;
gzip_disable "MSIE [1-6]\.";
gzip_disable "Mozilla/4";
gzip_comp_level 1;
gzip_buffers 4 8k;
gzip_min_length 1100;

VH設定

  • www.hogehoge.com.conf
#
# backend
#
upstream backend_static {
    server static.hogehoge.com:80;
    server app.hogehoge.com:80 backup;
}

upstream backend_app {
    server app.hogehoge.com:80;
}

upstream backend_cache {
    server cache.hogehoge.com:8080;
}

#
# http
# www.hogehoge.com
#
server {
    listen 80 default_server;
    server_name www.hogehoge.com;

    ## document root
    root /home/www/www.hogehoge.com/htdocs;

    ## directory index
    index index.html;

    ## log
    access_log /var/log/nginx/www.hogehoge.com/access.log main;
    error_log /var/log/nginx/www.hogehoge.com/error.log crit;

    ## error page
    error_page 404 /error/404.html;
    error_page 403 =404 /error/404.html;
    error_page 500 502 503 /error/50x.html;

    ## security
    # 許可しているmethod以外は拒否
    if ($request_method !~ ^(GET|POST|HEAD)$) {
        return 403;
    }

    # 許可しているhost以外は拒否
    if ($host !~ ^www\.hogehoge\.com)$) {
        return 403;
    }

    ## .ht**ファイルはアクセス禁止
    location ~ ^\.ht {
        deny all;
    }

    # proxy
    proxy_redirect off;
    proxy_connect_timeout 60s;
    proxy_intercept_errors on;


    location /error {
        internal;
    }

    ## 静的ファイルはSTATICへ
    location ~ ^/$ {
        proxy_pass http://backend_static;
    }

    location ~ ^/(static1|static2|static3) {
        ## 画像とかはログに記録しない
        if ($uri ~ \.(gif|js|css)$) {
            access_log off;
        }

        proxy_set_header Host static.hogehoge.com;
        proxy_pass http://backend_static;
        proxy_redirect http://static.hogehoge.com/ /;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }


    ## キャッシュさせたくないコンテンツはvarnish通さず直接appへ
    location ~ ^/form {
        proxy_set_header Host app.hogehoge.com;
        proxy_pass http://backend_app;
        proxy_redirect http://app.hogehoge.com/ /;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    ## 上記以外はcacheへ
    location / {
        proxy_set_header Host cache.hogehoge.com:8080;
        proxy_pass http://backend_cache;
        proxy_redirect http://app.hogehoge.com/ /;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

#
# hogehoge.com
#
server {
    listen 80;
    server_name hogehoge.com;
    rewrite (.*) http://www.hogehoge.com$1 permanent;
}
追記 2013/05/22

proxy_set_headerの記述箇所が間違っていたので修正。実際は別ファイルにしてインクルードさせています。