nginx redirect to Github Pages
つまり
-
運営したいURL(サーバ) http://takagi.makei.nu
-
運営したいコンテンツ http://pharaohkj.github.io/takagi.makei.nu/)
とあって、リバースプロキシ nginx を使って、github-pagesのインフラを使わせてもらおうという話。
まずgithub-pages
- Githubでプロジェクトを作る。今回は takagi.makei.nu
- gh-pages というブランチを作る
- コンテンツを書く
- http://pharaohkj.github.io/takagi.makei.nu/ でページが見られるのを確認する。
nginxのconfを書く
- CentOSのパッケージでは
/etc/nginx/conf.d/xxx.conf
に書く流儀らしいので、/etc/nginx/conf.d/takagimakei.nu.conf
を作成 - 中身はこんな感じ
- server_name は cname/a レコードで設定したもの
- redirectのログは出しておく(noticeじゃないと出ないらしい)
- 書いたら
/etc/init.d/nginx configtest
からのservice nginx reload
server {
access_log /var/log/nginx/takagi.makei.nu.access.log main;
error_log /var/log/nginx/takagi.makei.nu.error.log notice;
server_name takagi.makei.nu;
rewrite_log on;
# rewrite ^(.*) http://pharaohkj.github.io/takagi.makei.nu$1 ;
location / {
rewrite ^/(.+) $1 break;
proxy_pass http://pharaohkj.github.io/takagi.makei.nu/$1;
}
}
テスト
curlを詳細出力実行でヘッダやレスポンスがみられてわかりやすい。あと、nginxのログも見ること。rewriteはハマりやすい。
$ curl http://takagi.makei.nu -v
* STATE: INIT => CONNECT handle 0x60002d7e0; line 1011 (connection #-5000)
* Rebuilt URL to: http://takagi.makei.nu/
* Hostname was NOT found in DNS cache
* Trying 49.212.145.99...
* Adding handle: conn: 0x60006a4d0
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* 0x60002d7e0 is at send pipe head!
* - Conn 0 (0x60006a4d0) send_pipe: 1, recv_pipe: 0
* STATE: CONNECT => WAITCONNECT handle 0x60002d7e0; line 1058 (connection #0)
* Connected to takagi.makei.nu (49.212.145.99) port 80 (#0)
* STATE: WAITCONNECT => DO handle 0x60002d7e0; line 1177 (connection #0)
> GET / HTTP/1.1
> User-Agent: curl/7.37.0
> Host: takagi.makei.nu
> Accept: */*
>
* STATE: DO => DO_DONE handle 0x60002d7e0; line 1263 (connection #0)
* STATE: DO_DONE => WAITPERFORM handle 0x60002d7e0; line 1384 (connection #0)
* STATE: WAITPERFORM => PERFORM handle 0x60002d7e0; line 1395 (connection #0)
* HTTP 1.1 or later with persistent connection, pipelining supported
< HTTP/1.1 200 OK
* Server nginx/1.6.0 is not blacklisted
< Server: nginx/1.6.0
< Date: Tue, 16 Sep 2014 10:13:24 GMT
< Content-Type: text/html; charset=utf-8
< Content-Length: 1041
< Connection: keep-alive
< Last-Modified: Tue, 16 Sep 2014 04:04:34 GMT
< Expires: Tue, 16 Sep 2014 10:23:24 GMT
< Cache-Control: max-age=600
< Accept-Ranges: bytes
< Via: 1.1 varnish
< Age: 0
< X-Served-By: cache-ty68-TYO
< X-Cache: MISS
< X-Cache-Hits: 0
< X-Timer: S1410862404.676017,VS0,VE170
< Vary: Accept-Encoding
<
(コンテンツの中身)