7.5. Proxy Configuration for Uber JAR

This part describes the configuration of Nginx HTTP-server as a proxy for CUBA Uber JAR application.

NGINX

For Nginx there are 2 configurations described below. All examples were tested on Ubuntu 16.04.

  1. Direct Proxy

  2. Redirect to Path

For example, your web application works on http://localhost:8080/app.

Tip

Uber JAR application uses Jetty 9.2 server. It is required to preconfigure Jetty in JAR to dispatch Nginx headers by Jetty.

Jetty Setup
  • Using Internal jetty.xml

    First, create Jetty configuration file jetty.xml in the root of your project, copy and paste the following code:

    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">
    
    <Configure id="Server" class="org.eclipse.jetty.server.Server">
    
        <New id="httpConfig" class="org.eclipse.jetty.server.HttpConfiguration">
            <Set name="outputBufferSize">32768</Set>
            <Set name="requestHeaderSize">8192</Set>
            <Set name="responseHeaderSize">8192</Set>
    
            <Call name="addCustomizer">
                <Arg>
                    <New class="org.eclipse.jetty.server.ForwardedRequestCustomizer"/>
                </Arg>
            </Call>
        </New>
    
        <Call name="addConnector">
            <Arg>
                <New class="org.eclipse.jetty.server.ServerConnector">
                    <Arg name="server">
                        <Ref refid="Server"/>
                    </Arg>
                    <Arg name="factories">
                        <Array type="org.eclipse.jetty.server.ConnectionFactory">
                            <Item>
                                <New class="org.eclipse.jetty.server.HttpConnectionFactory">
                                    <Arg name="config">
                                        <Ref refid="httpConfig"/>
                                    </Arg>
                                </New>
                            </Item>
                        </Array>
                    </Arg>
                    <Set name="port">8080</Set>
                </New>
            </Arg>
        </Call>
    </Configure>

    Add webJettyConfPath property to the task buildUberJar in your build.gradle:

    task buildUberJar(type: CubaUberJarBuilding) {
        singleJar = true
        coreJettyEnvPath = 'modules/core/web/META-INF/jetty-env.xml'
        appProperties = ['cuba.automaticDatabaseUpdate' : true]
        webJettyConfPath = 'jetty.xml'
    }

    You may use Studio to generate jetty-env.xml by following Project Properties > Deployment Settings > Uber Jar tab, or use an example below:

    <?xml version="1.0"?>
    <!DOCTYPE Configure PUBLIC "-" "http://www.eclipse.org/jetty/configure_9_0.dtd">
    <Configure id='wac' class="org.eclipse.jetty.webapp.WebAppContext">
        <New id="CubaDS" class="org.eclipse.jetty.plus.jndi.Resource">
            <Arg/>
            <Arg>jdbc/CubaDS</Arg>
            <Arg>
                <New class="org.apache.commons.dbcp2.BasicDataSource">
                    <Set name="driverClassName">org.postgresql.Driver</Set>
                    <Set name="url">jdbc:postgresql://<Host>/<Database></Set>
                    <Set name="username"><User></Set>
                    <Set name="password"><Password></Set>
                    <Set name="maxIdle">2</Set>
                    <Set name="maxTotal">20</Set>
                    <Set name="maxWaitMillis">5000</Set>
                </New>
            </Arg>
        </New>
    </Configure>

    Build Uber JAR using the following command:

    gradlew buildUberJar

    Your application will be located in build/distributions/uberJar, the default name is app.jar.

    Run your application:

    java -jar app.jar

    Then install and configure Nginx as described in Tomcat section.

    Depending on your schema, you can access your site via http://localhost/app or http://localhost URL.

  • Using External jetty.xml

    Use the same configuration file jetty.xml from the project root, as described above. Place it in your home folder and do not modify buildUberJar task in build.gradle.

    Build Uber JAR using the following command:

    gradlew buildUberJar

    Your application will be located in build/distributions/uberJar folder, default name is app.jar.

    First, run the application with a parameter -jettyConfPath:

    java -jar app.jar -jettyConfPath jetty.xml

    Then install and configure Nginx as described in Tomcat section.

    Depending on your schema and setings in jetty.xml file, you can access your site via http://localhost/app or http://localhost URL.