Skip to content

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:

Terminal window
sudo apt install nginx

Create a site configuration file in /etc/nginx/sites-available/:

Terminal window
sudo vim /etc/nginx/sites-available/glassfish.conf

This configuration forwards all requests for domain.tld to the application appname on a GlassFish server listening on port 8080 of the same machine:

/etc/nginx/sites-available/glassfish.conf
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 upstream block names the back end. After 5 failed attempts (max_fails) within 5 seconds (fail_timeout), NGINX treats the server as unavailable for 5 seconds. weight only 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_pass ends with a path, a request for /page is forwarded to /appname/page.

Enable the site by linking it into sites-enabled:

Terminal window
sudo ln -s /etc/nginx/sites-available/glassfish.conf /etc/nginx/sites-enabled/

Check the configuration syntax:

Terminal window
sudo nginx -t

Restart NGINX and make it start at boot:

Terminal window
sudo systemctl restart nginx
sudo systemctl enable nginx

On 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:

/etc/nginx/sites-available/www.example.net
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):

Terminal window
sudo apt install python3-certbot-nginx

Request certificates for your domains. The NGINX plugin proves domain control through the running server and adds the certificate settings to the matching server block:

Terminal window
sudo certbot --nginx -d example.com -d www.example.com

Check the configuration and reload NGINX:

Terminal window
sudo nginx -t && sudo nginx -s reload

Renew 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:

Terminal window
sudo crontab -e

Run 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 --quiet

Certificates obtained with the NGINX plugin are renewed the same way, and NGINX is reloaded to use them.

Stop or remove NGINX

Stop the service:

Terminal window
sudo systemctl stop nginx

Remove the package but keep its configuration files:

Terminal window
sudo apt remove nginx

Remove the package together with the configuration files it installed:

Terminal window
sudo apt purge nginx

Fix “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.

References

Related notes

4 min readTürkçe

Ollama

GPU'suz, yalnız CPU ile çalışan makinelerde Ollama ile yerel LLM çalıştırma ve hızı belirleyen ayarlar.

  • AI & Data
  • DevOps & Containers
7 min readTürkçe

Podman

Podman kurulumu, rootless container'ları systemd ve Quadlet ile kullanıcı servisi olarak çalıştırma ve sık görülen hatalar.

  • DevOps & Containers
4 min read

Kubernetes

Run containers and local Kubernetes on macOS with Podman, Podman Desktop, kubectl and minikube, and describe workloads in Kubernetes YAML.

  • DevOps & Containers

Type to search the whole site.