読者です 読者をやめる 読者になる 読者になる

nginxとjettyの連携

jettyはpure javaのWebサーバ、サーブレット。spdyやwebsocketも対応。nginxからtomcatという連携もあるが、ならapache使えよと思わなくもないのでjettyにしてみた。

jetty

http://download.eclipse.org/jetty/ からリリース版をダウンロードしてインストール。

$ wget http://download.eclipse.org/jetty/9.2.9.v20150224/dist/jetty-distribution-9.2.9.v20150224.tar.gz
--2015-03-27 08:38:23--  http://download.eclipse.org/jetty/9.2.9.v20150224/dist/jetty-distribution-9.2.9.v20150224.tar.gz
download.eclipse.org をDNSに問いあわせています... 198.41.30.199
download.eclipse.org|198.41.30.199|:80 に接続しています... 接続しました。
HTTP による接続要求を送信しました、応答を待っています... 200 OK
長さ: 13471366 (13M) [application/x-gzip]
`jetty-distribution-9.2.9.v20150224.tar.gz' に保存中

100%[======================================>] 13,471,366  1.64M/s 時間 15s     

2015-03-27 08:38:38 (902 KB/s) - `jetty-distribution-9.2.9.v20150224.tar.gz' へ保存完了 [13471366/13471366]

$ zcat ../jetty-distribution-9.2.9.v20150224.tar.gz | tar xvf -
$ mv jetty-distribution-9.2.9.v20150224/ dist
$ cd dist
$ cd demo-base
$ ls
etc  lib  resources  start.d  start.ini  webapps
[kohchi@localhost demo-base]$ java -jar ../start.jar
Exception in thread "main" java.lang.UnsupportedClassVersionError: org/eclipse/jetty/start/Main : Unsupported major.minor version 51.0
	at java.lang.ClassLoader.defineClass1(Native Method)
	at java.lang.ClassLoader.defineClass(ClassLoader.java:643)
	at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
	at java.net.URLClassLoader.defineClass(URLClassLoader.java:277)
	at java.net.URLClassLoader.access$000(URLClassLoader.java:73)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:212)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:323)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
Could not find the main class: org.eclipse.jetty.start.Main. Program will exit.
$

とエラーがでる。javaのバージョンがまずい。

$ cat /etc/redhat-release 
CentOS release 6.5 (Final)
$ rpm -qa |grep jdk
java-1.6.0-openjdk-javadoc-1.6.0.0-5.1.13.3.el6_5.x86_64
java-1.6.0-openjdk-1.6.0.0-5.1.13.3.el6_5.x86_64
java-1.6.0-openjdk-devel-1.6.0.0-5.1.13.3.el6_5.x86_64
[kohchi@localhost demo-base]$ java -version
java version "1.6.0_30"
OpenJDK Runtime Environment (IcedTea6 1.13.3) (rhel-5.1.13.3.el6_5-x86_64)
OpenJDK 64-Bit Server VM (build 23.25-b01, mixed mode)
$

なのでyumでアップデートしてみた。

$ yum search openjdk
Loaded plugins: fastestmirror, refresh-packagekit, security
Determining fastest mirrors
 * base: ftp.jaist.ac.jp
 * extras: ftp.jaist.ac.jp
 * updates: ftp.jaist.ac.jp
============================= N/S Matched: openjdk =============================
java-1.6.0-openjdk.x86_64 : OpenJDK Runtime Environment
java-1.6.0-openjdk-demo.x86_64 : OpenJDK Demos
java-1.6.0-openjdk-devel.x86_64 : OpenJDK Development Environment
java-1.6.0-openjdk-javadoc.x86_64 : OpenJDK API Documentation
java-1.6.0-openjdk-src.x86_64 : OpenJDK Source Bundle
java-1.7.0-openjdk.x86_64 : OpenJDK Runtime Environment
java-1.7.0-openjdk-demo.x86_64 : OpenJDK Demos
java-1.7.0-openjdk-devel.x86_64 : OpenJDK Development Environment
java-1.7.0-openjdk-javadoc.noarch : OpenJDK API Documentation
java-1.7.0-openjdk-src.x86_64 : OpenJDK Source Bundle
java-1.8.0-openjdk.x86_64 : OpenJDK Runtime Environment
java-1.8.0-openjdk-demo.x86_64 : OpenJDK Demos
java-1.8.0-openjdk-devel.x86_64 : OpenJDK Development Environment
java-1.8.0-openjdk-headless.x86_64 : OpenJDK Runtime Environment
java-1.8.0-openjdk-javadoc.noarch : OpenJDK API Documentation
java-1.8.0-openjdk-src.x86_64 : OpenJDK Source Bundle
icedtea-web.x86_64 : Additional Java components for OpenJDK - Java browser
                   : plug-in and Web Start implementation

  Name and summary matches only, use "search all" for everything.
$ su
#
# yum install java-1.7.0-openjdk
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
 * base: ftp.jaist.ac.jp
 * extras: ftp.jaist.ac.jp
 * updates: ftp.jaist.ac.jp
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package java-1.7.0-openjdk.x86_64 1:1.7.0.75-2.5.4.0.el6_6 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package               Arch      Version                       Repository  Size
================================================================================
Installing:
 java-1.7.0-openjdk    x86_64    1:1.7.0.75-2.5.4.0.el6_6      updates     26 M

Transaction Summary
================================================================================
Install       1 Package(s)

Total download size: 26 M
Installed size: 91 M
Is this ok [y/N]: y
Downloading Packages:
java-1.7.0-openjdk-1.7.0.75-2.5.4.0.el6_6.x86_64.rpm     |  26 MB     00:07     
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : 1:java-1.7.0-openjdk-1.7.0.75-2.5.4.0.el6_6.x86_64           1/1 
  Verifying  : 1:java-1.7.0-openjdk-1.7.0.75-2.5.4.0.el6_6.x86_64           1/1 

Installed:
  java-1.7.0-openjdk.x86_64 1:1.7.0.75-2.5.4.0.el6_6                            

Complete!
# yum install java-1.7.0-openjdk-devel
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
 * base: ftp.jaist.ac.jp
 * extras: ftp.jaist.ac.jp
 * updates: ftp.jaist.ac.jp
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package java-1.7.0-openjdk-devel.x86_64 1:1.7.0.75-2.5.4.0.el6_6 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package                    Arch     Version                    Repository
                                                                           Size
================================================================================
Installing:
 java-1.7.0-openjdk-devel   x86_64   1:1.7.0.75-2.5.4.0.el6_6   updates   9.4 M

Transaction Summary
================================================================================
Install       1 Package(s)

Total download size: 9.4 M
Installed size: 36 M
Is this ok [y/N]: y
Downloading Packages:
java-1.7.0-openjdk-devel-1.7.0.75-2.5.4.0.el6_6.x86_64.r | 9.4 MB     00:03     
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : 1:java-1.7.0-openjdk-devel-1.7.0.75-2.5.4.0.el6_6.x86_64     1/1 
  Verifying  : 1:java-1.7.0-openjdk-devel-1.7.0.75-2.5.4.0.el6_6.x86_64     1/1 

Installed:
  java-1.7.0-openjdk-devel.x86_64 1:1.7.0.75-2.5.4.0.el6_6                      

Complete!
# yum install java-1.7.0-openjdk-javadoc
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
 * base: ftp.jaist.ac.jp
 * extras: ftp.jaist.ac.jp
 * updates: ftp.jaist.ac.jp
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package java-1.7.0-openjdk-javadoc.noarch 1:1.7.0.75-2.5.4.0.el6_6 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package                     Arch    Version                     Repository
                                                                           Size
================================================================================
Installing:
 java-1.7.0-openjdk-javadoc  noarch  1:1.7.0.75-2.5.4.0.el6_6    updates   16 M

Transaction Summary
================================================================================
Install       1 Package(s)

Total download size: 16 M
Installed size: 232 M
Is this ok [y/N]: y
Downloading Packages:
java-1.7.0-openjdk-javadoc-1.7.0.75-2.5.4.0.el6_6.noarch |  16 MB     00:10     
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : 1:java-1.7.0-openjdk-javadoc-1.7.0.75-2.5.4.0.el6_6.noarch   1/1 
  Verifying  : 1:java-1.7.0-openjdk-javadoc-1.7.0.75-2.5.4.0.el6_6.noarch   1/1 

Installed:
  java-1.7.0-openjdk-javadoc.noarch 1:1.7.0.75-2.5.4.0.el6_6                    

Complete!
[root@localhost demo-base]# rpm -qa | grep jdk
java-1.6.0-openjdk-javadoc-1.6.0.0-5.1.13.3.el6_5.x86_64
java-1.7.0-openjdk-javadoc-1.7.0.75-2.5.4.0.el6_6.noarch
java-1.6.0-openjdk-1.6.0.0-5.1.13.3.el6_5.x86_64
java-1.6.0-openjdk-devel-1.6.0.0-5.1.13.3.el6_5.x86_64
java-1.7.0-openjdk-devel-1.7.0.75-2.5.4.0.el6_6.x86_64
java-1.7.0-openjdk-1.7.0.75-2.5.4.0.el6_6.x86_64
[root@localhost demo-base]# java -version
java version "1.7.0_75"
OpenJDK Runtime Environment (rhel-2.5.4.0.el6_6-x86_64 u75-b13)
OpenJDK 64-Bit Server VM (build 24.75-b04, mixed mode)
# 

再度、起動してみる。

# java -jar ../start.jar
2015-03-27 08:59:05.829:INFO::main: Logging initialized @862ms
2015-03-27 08:59:06.248:WARN::main: demo test-realm is deployed. DO NOT USE IN PRODUCTION!
2015-03-27 08:59:06.250:INFO:oejs.Server:main: jetty-9.2.9.v20150224
2015-03-27 08:59:06.268:INFO:oejdp.ScanningAppProvider:main: Deployment monitor [file:/home/foo/jetty/dist/demo-base/webapps/] at interval 1
....

http://localhost:8080 で接続できた。
なお、jettyを起動時に動作させるには、dist/bin/jetty.sh を /etc/init.d 配下にコピーする。このスクリプトには必要な環境変数設定(JETTY_HOME)などの説明が書いてあるのでそれを参考に設定する。

nginxをつかってリダイレクト

80から8080にリダイレクトさせる設定を行う。

# cd /etc/nginx/conf.d
# cat jetty.conf 
#
# Redirecting to jetty servlect
#
server {

    listen       80;
    server_name  _;

    access_log  /var/log/nginx/jetty.access.log  main;

    location /jetty/ {
	proxy_http_version 1.1;
	proxy_pass http://localhost:8080/;
	proxy_redirect http://localhost:8080/ /;
	proxy_cookie_path /jetty /;
    }
}
# /etc/init.d/nginx start

http://localhost/jetty/ これで接続可能。