LoginSignup
1
1

More than 5 years have passed since last update.

nginx unit 0.3でvirtualenvを試す

Last updated at Posted at 2018-01-20

nginx unit 0.3 beta リリース

https://www.nginx.com/blog/unit-0-3-beta-release-available-now/
去年末にnginx unitの0.3がリリースされていた。

virtualenvに対応したということだったので、ちょっと試してみた。
unit-pythonの3が入らなかったのでとりあえずpython2.7.5で。(気が向いたらビルドからやってみる)

環境

  • vagrant(Centos7.4)
  • openresty/1.13.6.1()
  • unit 0.3
  • python2.7.5

準備

python

$ virtualenv test
$ source bin/activate
$ pip install routes
$ cat app/index.py 
from routes import Mapper
import json

def application(environ, response):

    map = Mapper()
    map.connect(None, '/error/{action}/{id}', controller='error')
    map.connect('home', '/', controller='main', action='index')

    result = map.match(environ['REQUEST_URI']);

    response('200 OK', [
        ('Content-type', 'text/plain')])

    return [json.dumps(result)]

unit

あらたに追加されたhomeにvirtualenvのパスを書いてやると良いらしい。

$ cat /etc/unit/unit.conf 
{
    "listeners": {
        "*:8000": {
            "application": "test"
        }
    },
    "applications": {
        "test": {
            "type": "python",
            "workers": 4,
            "working_directory": "/var/www/test",
            "path": "app",
            "home": ".",
            "module": "index"
        }
    }
}

nginx

ほとんどデフォルトのままなのでいるとこだけ。

    upstream unit-python {
        server 127.0.0.1:8000;
    }

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location = /favicon.ico {
            empty_gif;
        }

        location / {
            proxy_pass http://unit-python/;
            proxy_set_header Host $host;
        }

        #error_page  404              /404.html;

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

起動

sudo systemctl start nginx
sudo systemctl start unit
sudo curl -X PUT -d @/etc/unit/unit.conf --unix-socket /var/run/control.unit.sock http://localhost/

スクリーンショット 2018-01-20 12.28.40.png

いけた。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1