Ansibleで環境構築(8) - Nginxの設定
今回はNginxです。
最近Apacheから乗り換えたので、あんまり設定はよくわかってなかったり。
./roles/blog/tasks/main.yml
- include: nginx.yml
いつものようにNginx用のファイルを読み込むだけです。
./roles/blog/tasks/nginx.yml
---
- name: be sure Apache2 is uninstalled
apt: pkg={{ item }} state=absent purge=yes force=yes purge=yes
with_items:
- apache2*
tags: apt
- name: be sure ppa:nginx/stable repository is installed
apt_repository: repo='ppa:nginx/stable'
tags: nginx
- name: be sure nginx is installed
apt: pkg=nginx-extras update_cache=yes state=latest
notify:
- restart nginx
tags: nginx
- name: be sure nginx is configured
template: src={{ item.src }} dest={{ item.dest }} owner=root group=root
with_items:
- { src: ./etc/nginx/nginx.conf.j2, dest: /etc/nginx/nginx.conf }
- { src: ./etc/nginx/sites-available/default.j2, dest: /etc/nginx/sites-available/default }
- { src: ./etc/nginx/sites-available/backend.j2, dest: /etc/nginx/sites-available/backend }
notify:
- restart nginx
tags: nginx
- name: be sure nginx configures symbolic link is created
file: >
src=/etc/nginx/sites-available/backend
dest=/etc/nginx/sites-enabled/backend
state=link
notify:
- restart nginx
tags: nginx
- name: admin_ssh_user added to www-data group
command: gpasswd -a {{ admin_ssh_user }} www-data
- name: be sure nginx is running and enabled
service: name=nginx state=running enabled=yes
tags: nginx
消さなくても問題は無いんですが、同系統のサービスが2つあるのが気持ち悪いのでApacheを消してからNginxをインストールしています。
UbuntuではNginxのパッケージとしてnginx-light
/nginx
(nginx-full
と同じ)/nginx-extras
があるのですが、Nginxは後でモジュール入れ替えができないので全部入りのnginx-extras
にしています。Ansibleで構成管理している分には別に何でもいいのですが。
設定ファイルが多かったり、シンボリックリンク張ってたりしますが、基本的なNginxの導入方法だと思います。
あとは、メンテナンスをしやすくするためにwww-dataグループをログインユーザーに追加しています。
この手の設定はタスク単位で書いていくのがいいのか、ロール全体でuser.ymlみたいな形でまとめたほうがいいのかちょっと迷いますね。
./roles/blog/handlers/main.yml
- name: restart nginx
service: name=nginx state=restarted
ここの追加も定型ですね。
./roles/blog/templates/etc/nginx/nginx.conf.j2
user www-data;
pid /var/run/nginx.pid;
worker_processes 4;
worker_cpu_affinity 001 010 100 111;
worker_rlimit_nofile 4096; # = worker_processes * events.worker_connections
events {
worker_connections 1024;
multi_accept off;
use epoll;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 5;
types_hash_max_size 2048;
server_tokens off;
# server_names_hash_bucket_size 64;
server_name_in_redirect off;
port_in_redirect off;
client_max_body_size 16m;
client_body_buffer_size 256k;
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
log_format backend '$http_x_forwarded_for - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent"';
access_log /var/log/nginx/access.log main;
error_log /var/log/nginx/error.log;
gzip on;
gzip_static on;
gzip_disable "MSIE [1-6]\.";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 1;
gzip_buffers 16 8k;
gzip_min_length 1k;
gzip_http_version 1.0;
gzip_types text/plain text/xml text/css text/javascript
application/xhtml+xml application/xml application/rss+xml application/atom_xml
application/javascript application/x-javascript
application/x-httpd-php;
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=one:4m max_size=1000m inactive=120m;
proxy_temp_path /var/tmp/nginx;
proxy_cache_key "$scheme://$host$request_uri";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Accept-Encoding "";
proxy_connect_timeout 5;
proxy_send_timeout 10;
proxy_read_timeout 120;
proxy_hide_header X-Pingback;
proxy_hide_header X-Powered-By;
proxy_hide_header Etag;
proxy_hide_header Vary;
proxy_cache_use_stale timeout invalid_header http_500 http_502 http_503 http_504;
proxy_cache_lock on;
proxy_cache_lock_timeout 5s;
upstream backend {
server unix:/var/run/nginx-backend.sock;
}
upstream phpfpm {
server unix:/var/run/php5-fpm.sock;
}
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
Nginxのメインの設定です。
Proxyを設定する構成になっているのですが、もう色々設定しすぎてちょっとよくわからないですね。
設定項目については別記事で調べながら解説していきたいと思います(課題)。
./roles/blog/templates/etc/nginx/sites-available/default.j2
# front
server {
listen 80;
server_name _;
server_tokens off;
root /var/www;
index index.php index.html index.htm;
charset utf-8;
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
rewrite ^(.*)(index|home|default)\.html? $1 permanent;
# access deny
location ~ /\. { deny all; }
location = /wp-config.php { deny all; }
location ^~ /wp-admin/includes { deny all; }
location ^~ /wp-content/backup-db { deny all; }
set $mobile '';
if ($http_user_agent ~* '(DoCoMo|J-PHONE|Vodafone|MOT-|UP\.Browser|DDIPOCKET|ASTEL|PDXGW|Palmscape|Xiino|sharp pda browser|Windows CE|L-mode|WILLCOM|SoftBank|Semulator|Vemulator|J-EMULATOR|emobile|mixi-mobile-converter)') {
set $mobile '@ktai';
}
if ($http_user_agent ~* '(iPhone|iPod|incognito|webmate|Android.+Mobile|dream|CUPCAKE|froyo|BlackBerry|webOS|s8000|bada|IEMobile|Googlebot\-Mobile|AdsBot\-Google)') {
set $mobile '@smartphone';
}
if ($http_user_agent ~* '(iPad|Android((?!Mobile).)+$)') {
set $mobile "@tablet";
}
location ~* /.*images {
expires 30d;
break;
}
location ~* /wp-(content|admin|includes) {
index index.php index.html index.htm;
if ($request_filename ~* .*\.(xml|gz)) {
expires 1d;
break;
}
if ($request_filename ~* .*\.(txt|html?|js|css|swf)) {
expires 30d;
break;
}
if ($request_filename ~* .*\.(ico|jpe?g|gif|png|wmv|flv|mpg|gz)) {
expires 30d;
break;
}
if ($request_filename ~ .*\.php) {
proxy_pass http://backend;
break;
}
}
location /feed {
proxy_pass http://backend;
}
location ~ .*\.php {
proxy_pass http://backend;
}
location ~* /wp-(content|admin|includes) {
if ($request_filename ~ .*\.php) {
proxy_pass http://backend;
break;
}
}
location / {
set $do_not_cache 0;
if ($http_cookie ~* "comment_author_|wordpress_(?!test_cookie)|wp-postpass_" ) {
set $do_not_cache 1;
}
if ($request_method = POST) {
set $do_not_cache 1;
}
proxy_no_cache $do_not_cache;
proxy_cache_bypass $do_not_cache;
proxy_redirect off;
proxy_cache one;
proxy_cache_key "$scheme://$host$request_uri$mobile";
proxy_cache_valid 200 5m;
proxy_cache_valid 404 5m;
proxy_pass http://backend;
}
}
こちらも同様。
モバイル判定のタブレット部分だけは頑張って足しました(ただしWindows Tabletが入っていない)。
./roles/blog/templates/etc/sites-available/backend.j2
# backend
server {
listen unix:/var/run/nginx-backend.sock;
server_name _;
root /var/www;
access_log /var/log/nginx/backend.access.log backend;
gzip off;
gzip_vary off;
location / {
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?$args /index.php?q=$uri&$args;
}
location ~ \.php$ {
try_files $uri =404;
expires off;
fastcgi_pass phpfpm;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_param REMOTE_ADDR $http_x_real_ip;
fastcgi_pass_header "X-Accel-Redirect";
fastcgi_pass_header "X-Accel-Buffering";
fastcgi_pass_header "X-Accel-Charset";
fastcgi_pass_header "X-Accel-Expires";
fastcgi_pass_header "X-Accel-Limit-Rate";
}
}
そしてこちらも同様。