Tomcat X Nginx: Reverse Proxy

Tomcat X Nginx: Reverse Proxy

Tomcat7 di Centos 7 tidak bisa bind port 80. Lagipula, bukan best practice serve langsung pakai tomcat. Oleh sebab itu, di depan tomcat harus ada web server lain, bisa apache httpd, bisa juga Nginx. Artikel ini hanya akan berisi snipset konfigurasi reverse proxy tomcat pakai nginx.

Snipset Nginx vhost:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
###
# Desc: Nginx reserve proxy untuk aplikasi servlet tomcat
# Author: Sumarsono
# Create date: 2019-01-08
# Tested: Yes
##

server {
    listen          80;
    server_name     sub.domain.tld;
    
    error_log /var/log/nginx/sub/error.log warn;
    access_log /var/log/nginx/sub/access.log combined;

    location / {
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://localhost:8080/;
    }
}

Snipset server.xml:

1
2
3
4
5
6
7
8
9
...
<Engine name="Catalina" defaultHost="localhost">
        <Valve className="org.apache.catalina.valves.RemoteIpValve"
        internalProxies="127\.0\.[0-1]\.1"
        remoteIpHeader="x-forwarded-for"
        requestAttributesEnabled="true"
        protocolHeader="x-forwarded-proto"
        protocolHeaderHttpsValue="https"/> 
...

Selesai.