パラボラアンテナと星の日記

あることないこと

brew install nginx してプロキシサーバー動かして はじめて.pac書いてみた

brew でインストール

なんかプロキシサーバー使って遊びたくなったので、自分のMacにnginx入れてみた。 自身の経験としては、Apacheは遊びで多少経験ある(リバースプロキシ立てたことある)、nginxは商用含めてほとんど経験ない状態。 ググると情報はいっぱい出てくる。

(今回参考にした)

Macにbrewでnginx入れようとしたら意外にハマった。。 - 僕のYak Shavingは終わらない

brew install nginx                                                                                                                                          [berlin]
==> Installing nginx dependency: openssl
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/openssl-1.0.1j_1.yosemite.bottle.tar.gz
######################################################################## 100.0%
==> Pouring openssl-1.0.1j_1.yosemite.bottle.tar.gz
==> Caveats
A CA file has been bootstrapped using certificates from the system
keychain. To add additional certificates, place .pem files in
  /usr/local/etc/openssl/certs

and run
  /usr/local/opt/openssl/bin/c_rehash

This formula is keg-only, which means it was not symlinked into /usr/local.

Mac OS X already provides this software and installing another version in
parallel can cause all kinds of trouble.

Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries

Generally there are no consequences of this for you. If you build your
own software and it requires this formula, you'll need to add to your
build variables:

    LDFLAGS:  -L/usr/local/opt/openssl/lib
    CPPFLAGS: -I/usr/local/opt/openssl/include

==> Summary
🍺  /usr/local/Cellar/openssl/1.0.1j_1: 431 files, 15M
==> Installing nginx
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/nginx-1.6.2.yosemite.bottle.1.tar.gz
######################################################################## 100.0%
==> Pouring nginx-1.6.2.yosemite.bottle.1.tar.gz
==> Caveats
Docroot is: /usr/local/var/www

The default port has been set in /usr/local/etc/nginx/nginx.conf to 8080 so that
nginx can run without sudo.

To have launchd start nginx at login:
    ln -sfv /usr/local/opt/nginx/*.plist ~/Library/LaunchAgents
Then to load nginx now:
    launchctl load ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist
Or, if you don't want/need launchctl, you can just run:
    nginx

WARNING: launchctl will fail when run under tmux.
==> Summary
🍺  /usr/local/Cellar/nginx/1.6.2: 7 files, 920K
[/Users/berlin]which nginx                                                                                                                                                 [berlin]
/usr/local/bin/nginx
[/Users/berlin] 

なんかいっぱい出たけどまあよし。

localhost:8080で立ち上がってる

f:id:hoppie:20150105012824p:plain

(Jenkinsのファビコンェ..)(キャッシュですね)

launchctlの設定もしてみた。

ln -sfv /usr/local/opt/nginx/*.plist ~/Library/LaunchAgents 
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist

/usr/local/etc/nginx/nginx.confが設定ファイルで、いじると遊べる。 起動やリロードはnginxとかnginx -s reloadとか。

はじめて.pac書いてみた

ネットワーク構成を、JavaScrptで書けるやつ。 特定のドメインだけローカルのプロキシを咬ましてみようと思います。 確かwindowsでもこういうのあった。

参考にした:

polipo(dolipo)向けproxy.pac - otsune's SnakeOil - subtech

こんなのをdocrootに置いて

// proxy.pac

function FindProxyForURL(url, host)
{
//var proxy_yes = "PROXY 127.0.0.1:8080; DIRECT";
var proxy_yes = "PROXY 127.0.0.1:8080;";
var proxy_no  = "DIRECT";

    if (dnsDomainIs(host, "プロキシを通したいドメイン"))
        { return proxy_yes; }
return proxy_no;
}

ネットワーク構成で指定すればオーケー

f:id:hoppie:20150105012052p:plain

"プロキシを通したいドメイン"に"2ch.net"を指定してみる

f:id:hoppie:20150105013039p:plain

動いてる動いてる