Question

Ubuntu, Nginx, Tomcat 8, Letsencrypt configuration.

My configuration is as follows: Ubuntu, Nginx, Apache Tomcat 8. I only use Tomcat to run a servlet that queries MySQL and returns JSON. My web pages are served through Nginx. Then I installed LetEncrypt to Nginx. I enabled SSL/TLS encryption mode (Full (strict)) in CloudFlare. My Servlet is working fine when I use port 8080, and return the JSON response. To make it secure I changed Tomcat server.xml connector :

 <Connector port="8443" 
               protocol="org.apache.coyote.http11.Http11AprProtocol"
               maxThreads="150" 
               SSLEnabled="true"
               maxParameterCount="1000"
               URIEncoding="UTF-8"
               >
        <UpgradeProtocol className="org.apache.coyote.http2.Http2Protocol" />
        <SSLHostConfig>
          
            <Certificate certificateFile="/etc/letsencrypt/live/<my-domain>/cert.pem"
                       certificateKeyFile="/etc/letsencrypt/live/<my-domain>/privkey.pem"
                     certificateChainFile="/etc/letsencrypt/live/<my-domain>/chain.pem" />
        </SSLHostConfig>
    </Connector>

and commented

 <!-- Connector port="8080" 

Restarted tomcat, but I’m still able to invoke the tomcat servlet directly using 8080 port. But, when I call it from Ajax in my web page, I get this error: net::ERR_SSL_PROTOCOL_ERROR

My Question: How to allow my servlet to be called only from my web page.

Show comments

Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

KFSys
Site Moderator
Site Moderator badge
May 4, 2023

Hey @walsayer,

From what you’ve written, it seems like you just need to configure your Nginx as a reverse proxy to Tomcat for everything to work. Your Nginx configuration file (usually located at /etc/nginx/sites-available/default or /etc/nginx/conf.d/your_domain.conf) should include the following:

location /servlet_path {
    proxy_pass https://localhost:8443/servlet_path;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_ssl_certificate /etc/letsencrypt/live/<my-domain>/cert.pem;
    proxy_ssl_certificate_key /etc/letsencrypt/live/<my-domain>/privkey.pem;
    proxy_ssl_trusted_certificate /etc/letsencrypt/live/<my-domain>/chain.pem;
    proxy_ssl_verify on;
    proxy_ssl_verify_depth 2;
    proxy_ssl_session_reuse on;
}

Replace /servlet_path with the actual path to your servlet. This configuration will forward requests from Nginx to your Tomcat server over HTTPS, using the same SSL certificates. Note that you’ll need to have the ngx_http_proxy_module and ngx_http_ssl_module modules enabled in Nginx.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel