HTTP Server
HTTP Serverとは
Web Browsers
Hypertext Transfer Protocol
(HTTP)
HTTP Request
HTTP Reply
HTTP Server
• HTTP Server(またWeb Server)はHTTP Protocolによ
り、Websiteのデータを提供するソフトウェアまたは
ハードウェアである
HTTP Server vs Application Server
• HTTP Server (Apache HTTPDなど)
• 静的なコンテンツ(Static Content)
• HTML, JavaScriptなど
• Application Server(Apache Tomcatなど)
• 動的なコンテンツ(Dynamic Content)
• JSP, Servletなど
• HTTP Server機能も一応もっている
Apache HTTPD vs Apache Tomcat
Static Contents
(JavaScript,HTML
など)
Servlet/JSP
Apache
HTTPD
Apache
Tomcat
HTTPD
Tomcat
40%
39%
18%
3%
Market Share
http://news.netcraft.com/archives/category/web-server-survey/
Open Source HTTP Servers
Apache vs NGINX
• Apache
• Documentation &
Support
• ユーザー数が多い
• モジュールが多い
• 動的にモジュール追
加や削除可能
• NGINX
• パフォーマンス
• RAM の消費量が少な
い
• 静的なコンテンツの提
供が早い
FeaturesBasicaccess
authentication
Digestaccess
authentication
SSL/LShttps
Virtualhosting
CommonGateway
Interface
FCGI
SCGI
WSGI
JavaServlets
SSI
ISAPI
Adminconsole
IPv6
HTTP/2
NGINX
○ ○ ○ ○ × ○ ○ ○ × ○ × ○ ○ ○
HTTPD
○ ○ ○ ○ ○ ○ ○ ○ × ○ ○ ○ ○ ○
https://en.wikipedia.org/wiki/Comparison_of_web_server_software
インストール Installation
• RPM Package Managerを使う場合(aptなど)
• Debian系
• sudo apt-get install apache
• ソースコードとBinariesは公式サイトからダウンロー
ドできます
• ただし、BinariesはWindowsとNetware用しかない
インストール Installation
確認
$ sudo service apache2 start
Browserにlocalhostを打ってみると
基本操作
apache2ctl
start – 起動する
stop – 停止する
restart – 再起動する
graceful – 全てのSessionが終わるまで待って再起動
graceful-stop – 全てのSessionが終わるまで待って停止する
configtest – configファイルをチェックする
status – 状態を表示する
fullstatus – すべての状態を表示する
自作HTMLを提供してみる
HTMLファイル作成
• $cd /var/www/html/
• $nano myindex.html
<HTML>
<BODY>
Hello Apache
</BODY>
</HTML>
myindex.html
自作HTMLを提供してみる
Directory Index 変更
• $ cd /etc/apache2/sites-available
• $ nano 000-default.conf
• <VirtualHost *80> block内に
• directoryIndex myindex.htmlを追加する
• Apacheをreload
• $ sudo service apache2 reload
• すると
sites-available & sites-enable
• Apacheのroot directory内
• 2つのフォルダがあって
• sites-available
• sites-enable
sites-available sites-enable
000-default.config @000-default.config
virtualhost2.config @virtualhost2.config
virtualhost3.config @virtualhost3.config
$ ln から作られたlink
簡単なVirtual Hostを作ってみる
/var/www/html/内にhtmlファイルを作成
$cd /etc/apache2/sites-available
新しいVirtual Host Configファイルを作る
• sudo nano myvirtualhost1.conf
<VirtualHost *:81>
DocumentRoot /var/www/html/
DirectoryIndex myindex2.html
</VirtualHost>
myvirtualhost.conf
<HTML>
<BODY>
Hello VirtualHost
</BODY>
</HTML>
myindex2.html
簡単なVirtual Hostを作ってみる
a2ensite & a2dissite
• ~/sites-available/ 内に
• sudo a2ensite myvirtualhost1.conf
• myvirtualhost1をenableする
• sudo a2disite myvirtualhost1.conf
• myvirtualhost1をdisableする
インストール
インストール方法
• RPMからインストール(aptやyumなど)
• 簡単
• sudo apt-get install nginx
• いろいろ自動的に作ってくれる
• sites-availableとsite-enabledなど
• ソースからインストールする
• 最新版
• 自由モジュールを追加/削減
インストール
ソースからインストールする場合
1. Nginxをダウンロードし、Unzipする
• wget http://nginx.org/download/nginx-x.x.x.tar.gz
• x-x-xはVersion (現在1.8.1はstable版)
• tar –xzf nginx-1.8.1.tar.gz
2. ConfigureしてmakeとInstall
• ./configure
• sudo make && make install
インストール
ソースからインストールする場合
3. Nginxを起動させる
• インストールした場所に移動(./configureの時表示された)
• Ubuntuの場合は /usr/local/nginx にデフォルトされます
• cd /usr/local/nginx
• Executableファィルを実行します
• Executableファィルはsbinフォルダにいます
• sudo ./sbin/nginx
インストール
ソースからインストールする場合
3. 確認
• Browserにhttp://ip-addressを入力
• Welcome to nginx! のページが見れば起動しているはずです
モジュールを追加
• インストール時、./configure –helpを入れたら追加可能なモ
ジュールが出ってくる
• 既にないモジュールはネットからダウンロードできる
モジュールを追加
• 例えばSSLとStubをインストールしたいなら
$ ./configure --with-http_ssl_module  --with-
http_stub_status_modul
$ make
$ sudo make install
HTMLを提供してみる
• nginxのroot folderに行ってhtmlのfolderに入ります
• $ cd html
• index.htmlをバックアップする
• mv index.html index-backup.html
• 新しいindex.htmlを作ってhtmlを入力します
• nano index.html
<HTML>
<BODY>
Hello World!!!!!!
</BODY>
</HTML>
Index.html
HTMLを提供してみる
• Browserに行くと新しいページが出ってきます
• html folderとindex.htmlを変更したいならnginx.config
で変更できます!
基本操作
• 起動
• ./nginx
• 停止(すぐに停止)
• ./nginx stop
• 停止(Processesが終わるまで待つ)
• ./nginx quit
• Configファイルを再読み込み
• ./nginx reload
• reloadはダウンタイムなしで新しいConfigをロードできま
す
基本操作
• Configファイルを確認するには –t を使う
$ sudo ./nginx –t
• もし間違ったら
nginx.conf設定
events{
}
http{
server{
}
}
nginx.conf EventsContextHTTPとServerContexts
nginx.conf設定
Includeの使い方
http{
type{
text/html html htm shtml;
text/css css;
text/xml xml;
image/gif gif;
image/jpeg jpeg jpg;
などなど
}
}
nginx.conf
nginx.conf設定
nginx.confを綺麗にしてくれる
http{
include mime.type;
}
nginx.conf
type{
text/html html htm;
text/css css;
text/xml xml;
image/gif gif;
image/jpeg jpeg jpg;
などなど
}
mime.type
sites-available & sites-enabled
• Apt-getでインストールした場合は自動的に作成さ
れます
• ソースからインストールした場合は存在しません
• includeを使って作成可能
1. フォルダを作成する
• $mkdir sites-available
• $mkdir sites-enabled
2. nginx.confに以下を追加する
• include sites-enabled/*
• これでApacheみたいに ln を使って他の設定ファイルリ
ンクをsites-enableを入れられます
Rootフォルダ設定
server{
root html;
index index.html index-2.html;
}
nginx.conf
Rootフォルダ名前
Index ページの名前
• Index.htmlが存在しない場合
index-2.htmlがデフォルトされ
ます
Location Blockを使う
server{
location / {
root html;
index index.html index-2.html;
}
}
nginx.conf
~/html/index.html
Index.htmlがない場合は
Index-2.htmlを提供する
http://www.demosite.com/
root と alias
http://localhost/demo/demo_index.html
server{
location /demo/ {
root demo_html; }
}
nginx.conf
server{
location /demo/ {
alias demo_html; }
}
nginx.conf
~/demo_html/demo/demo_index.html
~/demo_html/demo_index.html
VirtualHost
2つのサイトを同時に提供してみます
• website1.com
• Website2.com
• 以下のhtmlファイルを作成します
<HTML>
<BODY>
Welcome to Website1
</BODY>
</HTML>
website1/index.html
<HTML>
<BODY>
Welcome to Website2
</BODY>
</HTML>
website2/index.html
VirtualHost
nginx.confを以下通り設定します
nginx.conf
Server名
Server名をMatching出
来ないとdefault_server
の方に行く
VirtualHost
• Localhost 情報を入力します
$sudo nano /etc/hosts
VirtualHost
Curlを使って結果確認
NGINXでReverse Proxyを作ってみる
• Reverse Proxyとは
Users
HTTP Request
HTTP Reply
HTTP Server
または他の
Application Server
Reverse Proxy
mywebsite.com
forward
NGINXでReverse Proxyを作ってみる
Nginx Proxy Nginx Server
127.0.1:80
127.0.1:81
127.0.1:82
127.0.1:83
APP1
APP2
APP3
~/app1/index.html
~/app2/index.html
~/app3/index.html
<HTML>
<BODY>
Welcome to APP1
</BODY>
</HTML>
~/app1/index.html
<HTML>
<BODY>
Welcome to APP2
</BODY>
</HTML>
~/app1/index.html
<HTML>
<BODY>
Welcome to APP3
</BODY>
</HTML>
~/app1/index.html
NGINXでReverse Proxyを作ってみる
server{
listen 80;
server_name proxy.com
location /app1/ {
proxy_pass http://127.0.1:81/;
}
location /app2/{
proxy_pass http://127.0.1:82/;
}
location /app3/{
proxy_pass http://127.0.1:83/;
}
}
Proxyのnginx.config
NGINXでReverse Proxyを作ってみる
server{
listen 81
server_name app1;
location / {
alias /usr/share/nginx/html/app1/;
index index.html
}
}
server{
listen 82
server_name app2;
root /usr/share/nginx/html/
location / {
index index.html
}
}
server{
listen 83
server_name app3;
location /app3/ {
alias /usr/share/nginx/html/app3/;
index index.html
}
}
Serverのnginx.config
NGINXでReverse Proxyを作ってみる
結果を確認する
• $ curl http://localhost/app1/
• $ curl http://localhost/app2/
• $ curl http://localhost/app3/
NGINXでReverse Proxyを作ってみる
結果
http://localhost/app1/
http://localhost/app2/
http://localhost/app3/
NGINXでReverse Proxyを作ってみる
結果
http://localhost/app1/
http://localhost/app2/
http://localhost/app3/
NGINXでReverse Proxyを作ってみる
server{
listen 80;
server_name proxy.com
location /app1/ {
proxy_pass http://127.0.1:81/;
}
location /app2/{
proxy_pass http://127.0.1:82/;
}
location /app3/{
proxy_pass http://127.0.1:83/;
}
}
Proxyのnginx.config
Nginx Proxy
http://127.0.1:83/
server{
listen 83
server_name app3;
location /app3/ {
alias /usr/share/nginx/html/app3/;
index index.html
}
}
Nginx Server
NGINXをLoad Balancerとして使う
• Load Balancerとは
Server 1
Load Balancer
Server 1
Server 1
Requests
R5
簡単Load Balancerを作る
http{
upstream backend{
server 127.0.0.1:81;
server 127.0.0.1:82;
server 127.0.0.1:83;
}
server{
listen 80;
location / {
proxy_pass http//backend
}
}
}
Load Balancerのnginx.config
DefaultはRound Robinを使う
簡単Load Balancerを作る
• $curl http://website1.com/を連続に入れてみると
課題
• Nginx+Apacheを設定しましょう
Nginx
Round Robin
Load Balancer
Nginx Server
Apache Server
My name is Nginx
html
My name is Apache
html
Users
• Gzip - compression
mime.typeをconfig
科目

簡単なHTTPサーバの作成