NGINX
Use NGINX as a reverse proxy for GlassFish, host several domains with server blocks, add Let's Encrypt certificates and fix 413 errors.
3 min readUpdated
NGINX is a web server that is often placed in front of an application server as a reverse proxy: it accepts the public HTTP and HTTPS traffic and forwards requests to the application. This note sets up NGINX on Debian or Ubuntu as a proxy for a GlassFish application, adds server blocks for several domains, issues TLS certificates with Certbot, and fixes the “413 Request Entity Too Large” error.
Set up a reverse proxy
Install NGINX:
sudo apt install nginxCreate a site configuration file in /etc/nginx/sites-available/:
sudo vim /etc/nginx/sites-available/glassfish.confThis configuration forwards all requests for domain.tld to the application appname on a GlassFish server listening on port 8080 of the same machine:
upstream glassfish { server 127.0.0.1:8080 weight=100 max_fails=5 fail_timeout=5;}
server { listen 80; server_name domain.tld;
location / { 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://glassfish/appname/; }}- The
upstreamblock names the back end. After 5 failed attempts (max_fails) within 5 seconds (fail_timeout), NGINX treats the server as unavailable for 5 seconds.weightonly matters when the block lists more than one server. - The
X-Forwarded-*headers pass the original host name and client address to the application. - Because
proxy_passends with a path, a request for/pageis forwarded to/appname/page.
Enable the site by linking it into sites-enabled:
sudo ln -s /etc/nginx/sites-available/glassfish.conf /etc/nginx/sites-enabled/Check the configuration syntax:
sudo nginx -tRestart NGINX and make it start at boot:
sudo systemctl restart nginxsudo systemctl enable nginxOn older systems without systemd, the equivalents were service nginx restart and update-rc.d -f nginx enable.
Host several domains with server blocks
Each server block answers for the names in its server_name. You can put several in one file, but a separate file per site in sites-available is clearer, for example /etc/nginx/sites-available/www.example.com and /etc/nginx/sites-available/www.example.net. Link each one into sites-enabled as above.
For example, to redirect every request for the second domain to the first:
server { listen 80; server_name www.example.net; return 301 http://www.example.com/;}Add TLS certificates with Certbot
Install Certbot with its NGINX plugin. On current Debian and Ubuntu the package is python3-certbot-nginx (python-certbot-nginx on older releases):
sudo apt install python3-certbot-nginxRequest certificates for your domains. The NGINX plugin proves domain control through the running server and adds the certificate settings to the matching server block:
sudo certbot --nginx -d example.com -d www.example.comCheck the configuration and reload NGINX:
sudo nginx -t && sudo nginx -s reloadRenew certificates automatically
Let’s Encrypt certificates are valid for 90 days. The Debian and Ubuntu packages install a systemd timer that runs certbot renew twice a day, so no further setup is usually needed. Without the timer, add a cron job. Open the root crontab:
sudo crontab -eRun the renewal every day at noon. certbot renew renews only certificates that expire within the next 30 days, and --quiet suppresses output:
0 12 * * * /usr/bin/certbot renew --quietCertificates obtained with the NGINX plugin are renewed the same way, and NGINX is reloaded to use them.
Stop or remove NGINX
Stop the service:
sudo systemctl stop nginxRemove the package but keep its configuration files:
sudo apt remove nginxRemove the package together with the configuration files it installed:
sudo apt purge nginxFix “413 Request Entity Too Large”
NGINX rejects request bodies larger than client_max_body_size, which defaults to 1 MB. Large uploads then fail with status 413. Raise the limit at the level you need:
In the http block, for all sites:
http { ... client_max_body_size 100M;}In a server block, for one site or application:
server { ... client_max_body_size 100M;}In a location block, for one path such as an upload directory:
location /uploads { ... client_max_body_size 100M;}Run sudo nginx -t and reload NGINX after the change. A value of 0 disables the check.