Use Redis via Python

Memorandum

The below codes is how to use redis via Python.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/usr/bin/python
# coding: UTF-8

import redis

r = redis.StrictRedis(host='localhost', port=6379)

# set/get string.
r.set('test1', 'aiueo')
r.expire('test1', 1000)
print r.get('test1') # aiueo

# set/get integer.
r.set('test2', 2)
print r.get('test2') # 2

# Check key exit.
print r.exists('test1') # True
print r.exists('test0') # False

# pattern match
keys = r.keys('test*')
if len(keys) > 0 :
for key in keys:
print '--------------------------------------------'
print key # test1, test2
print r.get(key) # aiueo, 2
print r.type(key) # Type : string, string
print r.ttl(key) # Expire : if not set, set "-1"

r.append('test1', '_kkkkkkk')
print r.get('test1') # aiueo_kkkkkkk

# delete cache.
r.delete('test1')
print r.exists('test1') # False

Thanks.

By grunt, uglify js & minify css on MacOSX.

By grunt, uglify js & minify css on MacOSX.

environment

  • MacOSX 10.11
  • grunt-cli v0.1.13
  • grunt v0.4.5

Install npm, Initilize npm

1
2
3
cd ~              # depending on your preference.
brew install npm
npm init
  • Result
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
About to write to /Users/kenzo/go/src/github.com/flag/public/package.json:

{
"name": "public",
"version": "1.0.0",
"description": "gruntfile",
"main": "Gruntfile.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "xxxxxxxx@gmail.com",
"license": "ISC",
"devDependencies": {
},
"dependencies": {
}
}

Install grunt-cli

1
npm install -g grunt-cli

With --save-dev option, add install module infomation to package.json.

Install grunt modules

1
2
3
4
5
6
7
8
9
10
11
12
npm install -g grunt --save-dev
npm install grunt-contrib-watch --save-dev
npm install grunt-contrib-copy --save-dev
npm install grunt-contrib-clean --save-dev
npm install grunt-contrib-cssmin --save-dev
npm install grunt-contrib-uglify --save-dev
npm install grunt-image --save-dev
npm install grunt-contrib-htmlmin --save-dev
npm install load-grunt-tasks --save-dev
npm install grunt-jsbeautifier --save-dev
npm install grunt-cssbeautifier --save-dev

Module map.

Module Detail
grunt-contrib-watch Monitoring update files.
grunt-contrib-copy Copy file or directory.
grunt-contrib-clean Clean file or directory.
grunt-contrib-cssmin Minify CSS files.
grunt-contrib-uglify Uglify & Compress Javascript files.
grunt-contrib-image Optimize image files (jpeg, jpg, gif, png, swf, etc…).
grunt-contrib-htmlmin Minify HTML files.
grunt-jsbeautifier beautify Javascript files.
grunt-cssbeautifier beautify Javascript files.

Confirm package.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
cat package.json

{
"name": "public",
"version": "1.0.0",
"description": "grunt",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "*********@gmail.com",
"license": "ISC",
"devDependencies": {
"grunt": "^0.4.5",
"grunt-contrib-clean": "^0.6.0",
"grunt-contrib-copy": "^0.8.2",
"grunt-contrib-cssmin": "^0.14.0",
"grunt-contrib-htmlmin": "^0.6.0",
"grunt-contrib-uglify": "^0.10.0",
"grunt-contrib-watch": "^0.6.1",
"grunt-cssbeautifier": "^0.1.2",
"grunt-image": "^1.1.1",
"grunt-jsbeautifier": "^0.2.10",
"load-grunt-tasks": "^3.3.0"
}
}
  • Add dependencies of grunt modules to package.json !

move to parent directory of css, js folder

1
cd /path/to/project/public/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
tree

/public/

├─Gruntfile.coffee # make `Gruntfile.coffee` at next step.

├── css
│ ├── bootstrap.css
│ ├── img.css
│ ├── style.css
│ └── reset.css

└── js
├── jquery-1.9.1.js
├── img.js
├── login.js
└── signup.js

Create Gruntfile.coffee or Gruntfile.json

  • Today, I create a Gruntfile.coffee.
  • For example, Only Uglify js, Minify css, Beautify js, css.
1
vim Gruntfile.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
module.exports = (grunt) ->

# current path.
path = require('path')
current = path.resolve('.')

# load npm task.
grunt.loadNpmTasks('grunt-contrib-cssmin');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-jsbeautifier');
grunt.loadNpmTasks('grunt-cssbeautifier');

grunt.initConfig

# define directory.
dir:
js: 'js'
css: 'css'
img: 'img'

# minify CSS.
cssmin:
all:
expand: true
cwd: current + '/<%= dir.css %>/'
src: '*.css'
dest: current + '/<%= dir.css %>/'

# uglify js.
uglify:
options:
mangle: true
compress: true
all:
expand: true
cwd: current + '/<%= dir.js %>/'
src: '*.js'
dest: current + '/<%= dir.js %>/'

# beautify js.
jsbeautifier:
files: '**/*.js'
options: []

# beautify css.
cssbeautifier:
files: '**/*.css'
options: []

grunt.registerTask 'default', ['cssmin', 'uglify']

Make symbolilc Link of ‘node_modules’

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
cd <path where Gruntfile.coffee exist>
ln -s ~/node_modules .
tree

/public/
├─Gruntfile.coffee
├─node_modules -> /Users/kenzo/node_modules

├── css
│ ├── bootstrap.css
│ ├── img.css
│ ├── style.css
│ └── reset.css

└── js
├── jquery-1.9.1.js
├── img.js
├── login.js
└── signup.js

Execute grunt command.

1
2
cd <path where Gruntfile.coffee exist>
grunt # By no parameter, execute default task.
  • If You want to execute only cssmin, excute command grunt cssmin

Thakns.

Ruby & gem インストール

備忘録です。

環境

  • CentOS 5.8 (Final)

ruby 2.1.2 インストール

1
2
3
4
5
6
7
# cd /usr/local/src
# wget http://cache.ruby-lang.org/pub/ruby/2.1/ruby-2.1.2.tar.gz
# tar xvfz ruby-2.1.2.tar.gz
# rm ruby-2.1.2.tar.gz
# cd ruby-2.1.2
# ./configure
# make; make install

gem インストール

1
2
3
4
# wget http://production.cf.rubygems.org/rubygems/rubygems-2.2.2.zip
# unzip rubygems-2.2.2.zip
# cd rubygems-2.2.2
# ruby setup.rb

以上

Nginx Basic認証設定、社内IPなど特定ipのみ許可

概要

サービス公開前にNginxで
Basic認証を掛ける必要がありました

ちょうど社内公開したときでもあり
Basic認証のポップアップが出るのが鬱陶しいのもあって
社内だけオフりたい、というときに以下のような設定をしました。

Basic認証設定

1
# yum install -y httpd-tools
1
2
3
4
# cd /etc/nginx
# htpasswd -c .htpasswd <Basic認証ユーザ>
New password: <Basic認証ユーザのパスワード入力>
Re-type new password: <もう一度、Basic認証ユーザのパスワード入力>

Nginx configureファイル編集

1
# vim /etc/nginx/conf.d/default.conf
1
2
3
4
5
6
7
8
9
location / {
....

# Basic認証設定
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/.htpasswd;

....
}

特定IPのみ許可したい場合

1
# vim /etc/nginx/conf.d/default.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
location / {
....

# add start -----
satisfy any;
allow <許可IP>;
allow <許可IP>;
deny all;
# add end -----

# Basic認証設定
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/.htpasswd;

....
}

あとは 許可IPを随時増やせば良いです。

Webアプリケーション開発におけるNginxあるあるかなと思います♪

以上です。

Nginxエラー対策 a client request body is buffered to a temporary file

概要

Nginxで以下のようなエラーが発生

[warn]とあるしものものしさは否めない汗

要求されるバッファサイズが設定量より多く、一時ファイルを利用しますよ、
という警告です。

1
[warn] 1493#1493: *210 a client request body is buffered to a temporary file /var/cache/nginx/client_temp/0000000001 ... request: "POST /article/save HTTP/1.1",

そのバッファサイズの設定量を変更をしてまず対策しました。

取り急ぎ、t2.microで最小構成でとりあえず作れ!というミッションだったので
50kくらいにしておきました。

1
2
3
4
5
6
7
8
9
http {

...

client_body_buffer_size 50k;

...

}

取り急ぎは上記設定でwarn は吐き出されないようになりました。

もしまた吐き出されるようになった場合は、
その数値を元にインスタンスのスケールアップも検討しようと思います。

ちなみにバッファのキャッシュ先ディレクトリ(client_body_temp_path)はデフォルトで/var/cache/nginx/clientとなっていたので設定しませんでした。

以上です。

Elasticsearch インデックス一覧・マッピング一覧・マッピング設定

前提

Elasticsearch のインストールされているサーバ内での作業を想定しています。

インデックス一覧

1
curl -XGET localhost:9200/_aliases?pretty
1
2
3
4
5
6
7
8
{
"logstash-2015.09.22" : {
"aliases" : { }
},
"logstash-2015.09.21" : {
"aliases" : { }
},
}
  • prettyをクエリストリングに指定すると
    jsonが整形されて表示されます。

インデックスのマッピング一覧

1
$ curl -XGET localhost:9200/_mapping?pretty
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
{
"logstash-2015.09.22" : {
"mappings" : {
"project_production_nginx" : {
"properties" : {
"@log_name" : {
"type" : "string"
},
"@timestamp" : {
"type" : "date",
"format" : "dateOptionalTime"
},
"body_bytes_sent" : {
"type" : "long"
},
"bytes_sent" : {
"type" : "long"
},
"forwardedfor" : {
"type" : "string"
},
"host" : {
"type" : "string"
},
"https" : {
"type" : "string"
},
"log_level" : {
"type" : "string"
},
"message" : {
"type" : "string"
},
"pid" : {
"type" : "string"
},
"query_string" : {
"type" : "string"
},
"referer" : {
"type" : "string"
},
"remote_addr" : {
"type" : "string"
},
"request_length" : {
"type" : "long"
},
"request_method" : {
"type" : "string"
},
"request_time" : {
"type" : "double"
},
"request_uri" : {
"type" : "string"
},
"status" : {
"type" : "long"
},
"tid" : {
"type" : "string"
},
"upstream_response_time" : {
"type" : "long"
},
"uri" : {
"type" : "string"
},
"useragent" : {
"type" : "string"
}
}
}
}
}
}

マッピング設定

jsonファイル作成

1
2
$ cd /etc/elasticsearch/
$ vim el_mapping.json
  • el_mapping.json

以下のように設定し保存

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
{
"template": "logstash-*",
"mappings" : {
"_default_" : {
"dynamic_templates":[{
"string_template":{
"match":"*",
"mapping":{
"type":"string",
"index":"not_analyzed"
},
"match_mapping_type":"string"
}
}],
"properties" : {
"@timestamp" : {
"type" : "date",
"format" : "dateOptionalTime"
},
"body_bytes_sent" : {
"type" : "long"
},
"bytes_sent" : {
"type" : "long"
},
"geoip" : {
"properties" : {
"location" : {
"type" : "geo_point",
"lat_lon" : true,
"geohash" : true,
"geohash_prefix" : true,
"geohash_precision" : 10
}
}
},
"request_length" : {
"type" : "long"
},
"request_time" : {
"type" : "double"
},
"status" : {
"type" : "long"
},
"upstream_response_time" : {
"type" : "long"
},
"coordinate" : {
"type" : "double"
},
"country" : {
"type" : "string"
},
"lat" : {
"type" : "double"
},
"location_properties" : {
"type" : "geo_point"
},
"lon" : {
"type" : "double"
}
}
}
}
}

マッピング設定実行

1
curl -X PUT localhost:9200/_template/template_1 --data @el_mapping.json

マッピング設定一覧

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
curl -X GET localhost:9200/_template/?pretty

# 一覧が出力される
...
...
},
"template_1" : {
"order" : 0,
"template" : "flag_prd.accesslog-*",
"settings" : { },
"mappings" : {
"_default_" : {
"dynamic_templates" : [ {
"string_template" : {
"mapping" : {
"index" : "not_analyzed",
"type" : "string"
},
"match_mapping_type" : "string",
"match" : "*"
}
} ],
"properties" : {
...
...

マッピング設定削除

1
curl -X DELETE localhost:9200/_template/template_1

string型はまとめてdynamic_templatesで定義してます。

1
2
3
4
5
6
7
8
9
10
"dynamic_templates":[{
"string_template":{
"match":"*",
"mapping":{
"type":"string",
"index":"not_analyzed"
},
"match_mapping_type":"string"
}
}],

インデックス削除

インデックスを指定し削除します。

1
2
3
curl -X DELETE localhost:9200/logstash-2015.09.22

{"acknowledged":true}

インデックス全削除

1
2
3
$ curl -XDELETE 'http://localhost:9200/*'

{"acknowledged":true}

サービス開始前で試行錯誤してるときに何度か利用しました。
極力使わない。

指定インデックス削除

  • 例) .kibana インデックス削除
1
$ curl -XDELETE 'http://localhost:9200/.kibana'

各フィールド設定は以下まとめていただいている方がいらっしゃいました。
ありがとうございます♪

Kibana+Elasticsearchで文字列の完全一致と部分一致検索の両方を実現する

Kibana エラー対応 - Discover: An error occurred with your request. Reset your inputs and try again.

問題

ある日、Kibana > Discover にアクセスすると以下のようなエラー表示で
Searchingが完了できない状態に陥った。

ElasticSearchログ確認

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
tail -f /var/log/elasticsearch/elasticsearch.log

Failed to execute [org.elasticsearch.action.search.SearchRequest@8307e49] while moving to second phase
java.lang.ClassCastException: java.lang.Long cannot be cast to org.apache.lucene.util.BytesRef
at org.apache.lucene.search.FieldComparator$TermOrdValComparator.compareValues(FieldComparator.java:902)
at org.apache.lucene.search.TopDocs$MergeSortQueue.lessThan(TopDocs.java:172)
at org.apache.lucene.search.TopDocs$MergeSortQueue.lessThan(TopDocs.java:120)
at org.apache.lucene.util.PriorityQueue.upHeap(PriorityQueue.java:225)
at org.apache.lucene.util.PriorityQueue.add(PriorityQueue.java:133)
at org.apache.lucene.search.TopDocs.merge(TopDocs.java:234)
at org.elasticsearch.search.controller.SearchPhaseController.sortDocs(SearchPhaseController.java:239)
at org.elasticsearch.action.search.type.TransportSearchQueryThenFetchAction$AsyncAction.moveToSecondPhase(TransportSearchQueryThenFetchAction.java:89)
at org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction.innerMoveToSecondPhase(TransportSearchTypeAction.java:403)
at org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction.onFirstPhaseResult(TransportSearchTypeAction.java:202)
at org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction$1.onResult(TransportSearchTypeAction.java:178)
at org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction$1.onResult(TransportSearchTypeAction.java:175)
at org.elasticsearch.search.action.SearchServiceTransportAction$23.run(SearchServiceTransportAction.java:568)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)

キャストがうまくいってないバグ出てました。

マッピングがうまくいってないのかな?
と問題を想定して色々設定変更していたらここにたどり着いた。

解決

  1. Kibana > Settings > Advanced
  2. sort:options {“unmapped_type”: “boolean”} → {“ummapped_type”: “date” } に変更

これで治った!

GithubでもClose Issueとして残ってましたね。

Auto detect field type when sorting on unmapped_type fields

Nginxエラー調査 「duplicate MIME type 'text/html' in /etc/nginx/nginx.conf」

概要

エラーログをslackに通知させるようにしてるとほんと便利。

時たまなんですが、なんだこれ?というのが送られてくる。

その一つが掲題のエラー。

1
duplicate MIME type "text/html" in /etc/nginx/nginx.conf

nginx.confを見てみると
gzip_typesで設定した text/html でした。

直訳すると

1
/etc/nginx/nginx.confでMIMEタイプ「text/html」が重複しています。

じゃ、消せばいいかなってことなので消せば解決しました。

/etc/nginx/nginx.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
gzip              on;
gzip_static on;
gzip_http_version 1.0;
gzip_types text/plain
text/html
text/xml
text/css
application/xml
application/xhtml+xml
application/rss+xml
application/atom_xml
application/javascript
application/x-javascript;
gzip_disable "MSIE [1-11]\.(?!.*SV1)";
gzip_disable "Mozilla/4";
gzip_comp_level 9;
gzip_vary on;

別にどこかで指定されているの?

結論を言うと

  • ngx_http_gzip_moduleをインストールしており
  • gzip on としている
    と、デフォルトでtext/htmlがMIMEタイプが指定されます。

以下公式サイトを見るとわかります。

gzip_types

text/htmlタイプは常に圧縮対象としているそうです。

なので、gzip で圧縮処理をする場合は
text/htmlが不要です。

ということでした。

NginxでGeoIP設定しアクセスログにアクセスポイントを追加する

概要

GeoIP Libraryを設定後、access.logにアクセスポイントを表示

GeoIP データファイル取得

GeoIPライブラリを提供しているMaxMindサイトから取得可能です。
有料版もありますが、取り急ぎは無料版で試します。

1
2
3
4
5
6
# mkdir -p /usr/share/GeoIP/
# cd /usr/share/GeoIP/
# wget -N http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
# wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
# gunzip GeoIP.dat.gz
# gunzip GeoLiteCity.dat.gz

nginx.conf設定

※ログのフォーマットはltsvにしています。

1
# vim /etc/nginx/nginx.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
http {

...
...

geoip_country /usr/share/GeoIP/GeoIP.dat;
geoip_city /usr/share/GeoIP/GeoLiteCity.dat;

log_format ltsv 'time:$time_iso8601\t'
'remote_addr:$remote_addr\t'
'request_method:$request_method\t'
'request_length:$request_length\t'
'request_uri:$request_uri\t'
'https:$https\t'
'uri:$uri\t'
'query_string:$query_string\t'
'status:$status\t'
'bytes_sent:$bytes_sent\t'
'body_bytes_sent:$body_bytes_sent\t'
'referer:$http_referer\t'
'useragent:$http_user_agent\t'
'forwardedfor:$http_x_forwarded_for\t'
'request_time:$request_time\t'
'upstream_response_time:$upstream_response_time\t'
'host:$host\t'

# geoIP setting --- start ---
'geoip_country_name:$geoip_city_country_name\t' # 国名
'geoip_country_code3:$geoip_city_country_code3\t' # JPNとかUSAとか
'geoip_city:$geoip_city\t' # 都市名
'geoip_latitude:$geoip_latitude\t' # 緯度
'geoip_longitude:$geoip_longitude'; # 経度
# geoIP setting --- end ---


access_log /var/log/nginx/access.log ltsv;
...
...

include /etc/nginx/conf.d/*.conf;
}

Nginx再起動

1
# systemctl restart nginx

アクセスログ確認

1
2
3
# tail -f /var/log/nginx/access.log

time:2015-10-01T18:01:48+09:00 remote_addr:xxx.xxx.xx.xx request_method:GET request_length:882 request_uri:/public/img/icon/favicon.ico https: uri:/public/img/icon/favicon.ico query_string:- status:200 bytes_sent:4791 body_bytes_sent:4096 referer:http://theflag.jp/ useragent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36 forwardedfor:- request_time:0.001 upstream_response_time:0.001 host:theflag.jp geoip_country_name:Japan geoip_country_code3:JPN geoip_city:Tokyo geoip_latitude:35.6850 geoip_longitude:139.7514

取得できました♪

これをfluentdで流してKibanaでかっこよく表示しましょう。

以上です。

インストール済みNginx にgeoIP Library追加

概要

元々yumでNignxをインストールしていましたが
IP制限やfluentdでip情報を割り出す必要があり
geoIPが必要になりました。

その為には
新たにNginxをソースからダウンロードしLibrary追加しコンパイルし直す必要があります。

geoIPインストール理由

  • ログにip情報を付加しfluentdで解析する。
  • IP許可制限が可能になる。
    • 海外から不正アクセスがあった為対応しました。

環境

  • CentOS7
  • Nginx1.8
  • EC2

現状のNginxの利用可能モジュール確認

1
2
3
4
5
6
7
# nginx -V

nginx version: nginx/1.8.0
built by gcc 4.8.3 20140911 (Red Hat 4.8.3-9) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-mail --with-mail_ssl_module --with-file-aio --with-ipv6 --with-http_spdy_module --with-cc-opt='-O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic'

--with-http_geoip_moduleがない!

ので geoIPをインストールしてNginxをコンパイルし直します。

GeoIP Libraryインストール

1
$ sudo yum install -y GeoIP GeoIP-devel

同バージョンのNginxのtar.gzをダウンロード

※今回はNginxは1.8.0なのでnginx-1.8.0.tar.gzをダウンロードします。

1
2
3
4
$ cd /usr/local/src
$ wget http://nginx.org/download/nginx-1.8.0.tar.gz
$ tar zxvf nginx-1.8.0.tar.gz
$ cd nginx-1.8.0

先ほどnginx -Vで確認したconfigure argumentsをコピって
そこに--with-http_geoip_moduleを追加してコンパイルする。

Nginx再コンパイル

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
$ cd /usr/local/src/nginx-1.8.0

$ ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-mail --with-mail_ssl_module --with-file-aio --with-ipv6 --with-http_spdy_module --with-http_geoip_module --with-cc-opt='-O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic'

...
checking for GeoIP library ... found ← ちゃんとインストール済み!
checking for GeoIP IPv6 support ... found ← ちゃんとインストール済み!
...
Configuration summary
+ using system PCRE library
+ using system OpenSSL library
+ md5: using OpenSSL library
+ sha1: using OpenSSL library
+ using system zlib library

nginx path prefix: "/etc/nginx"
nginx binary file: "/usr/sbin/nginx"
nginx configuration prefix: "/etc/nginx"
nginx configuration file: "/etc/nginx/nginx.conf"
nginx pid file: "/var/run/nginx.pid"
nginx error log file: "/var/log/nginx/error.log"
nginx http access log file: "/var/log/nginx/access.log"
nginx http client request body temporary files: "/var/cache/nginx/client_temp"
nginx http proxy temporary files: "/var/cache/nginx/proxy_temp"
nginx http fastcgi temporary files: "/var/cache/nginx/fastcgi_temp"
nginx http uwsgi temporary files: "/var/cache/nginx/uwsgi_temp"
nginx http scgi temporary files: "/var/cache/nginx/scgi_temp"


$ make
$ make install

再度Nginxモジュール確認

1
2
3
4
5
6
7
nginx -V

nginx version: nginx/1.8.0
built by gcc 4.8.3 20140911 (Red Hat 4.8.3-9) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-mail --with-mail_ssl_module --with-file-aio --with-ipv6 --with-http_spdy_module --with-http_geoip_module --with-cc-opt='-O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic'

--with-http_geoip_module
利用可能なモジュールとして設定されていることが確認できました。

以下設定ファイルも特に変更することなく
systemctl restart nginxしても問題なかったです。

1
2
/etc/nginx/nginx.conf
/etc/nginx/conf.d/*.conf

以上です。