今回の内容
Windows版のnginxにてWebサーバーを構築します。
nginx
インストール
以下のURLからWindows版をダウンロードし、適当な場所に解凍します。
※今回は「C:\dev」に環境を構築しています。
https://nginx.org/en/download.html
確認
コマンドプロンプトにて以下のコマンドよりnginxを起動します。
start nginx
ブラウザで「http://localhost」を確認すると「Welcom to nginx!」と表示されればインストールは完了です。
確認後は以下のコマンドにてnginxを停止しておきます
nginx -s stop
サービス化
以下のサイトより「WinSW」をダウンロードします。
https://github.com/kohsuke/winsw/releases
「nginx.exe」と同じ場所に配置します。
「WinSW.NET4.xml」というファイルを同じ場所に作成し以下の内容をコピーします
<service> <id>nginx</id> <name>nginx</name> <description>nginx</description> <logpath>c:\dev\nginx\logs</logpath> <logmode>roll</logmode> <depend></depend> <executable>c:\dev\nginx\nginx.exe</executable> <startargument></startargument> <stopexecutable>c:\dev\nginx\nginx.exe</stopexecutable> <stopargument>-s</stopargument> <stopargument>stop</stopargument> </service>
※「c:\dev\nginx」の部分はnginxをインストールした場所に設定してください
以下のコマンドによりサービス化を実行します。
WinSW.NET4.exe install
結果として「Installing the service with id 'nginx'」が表示されれば確認は完了です。
管理ツールよりサービスを起動し、「nginx」を起動しておいてください。
リバースプロキシ設定
PythonのURLに対してリバースプロキシの設定するため「nginx.conf」の一部を修正します。
server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { #root html; #index index.html index.htm; proxy_pass http://backend; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } upstream backend { server localhost:5000; }
サービスより「nginx」を再起動し、以下のURLにアクセスすると前回と同様の内容がブラウザに表示されます。
これでHTTPプロトコルによってPythonのコードが実行されたのが確認できました。
http://localhost/get?chipid=XXX000
{"chip_id":"XXX000","unix_time":1575778996}
アクセスログも出力されているかと思います。
アクセスログはnginxをインストールした場所の「logs」に保存されます
127.0.0.1 - - [08/Dec/2019:13:23:16 +0900] "GET /get?chipid=XXX000 HTTP/1.1" 200 44 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36"
次回
自動給餌機を管理するためのデータ保存場所としてSqliteを構築していこうかと思います。
動画
参考文献
https://qiita.com/schwarz471/items/9b44adfbec006eab60b0
https://komeiy.hatenablog.com/entry/2014/12/10/231714
https://qiita.com/sugasaki/items/83542f5614bc54a9475f
http://cointoss.hatenablog.com/entry/2017/03/06/125442