Setting up NginX
Last modified by Manuel Leduc on 2024/02/14
Set up
Example running XWiki on the Glassfish 2 Application Server:
- NginX listens on <domain>:80 and redirects HTTP queries to <domain>:8080 and thus NginX is referred to as "frontend" and Glassfish as "backend".
- Glassfish is installed in the /user/local/glassfish/glassfish folder.
For more information you can consult the NginX manual.
NginX configuration example with <domain> being vostrets.ru (/etc/nginx.conf):
user www-data;
worker_processes 2; # This should be equal to number of CPUs
worker_rlimit_nofile 20000; # Max open file descriptors, increase if nginx is serving large amount of static data for many users
events
{
worker_connections 1024; # number of max connections per worker (worker_connections * worker_processes = max connections)
use epoll;
multi_accept on;
}
http
{
include mime.types;
default_type application/octet-stream;
server_tokens off; # don't send nginx version to end users
sendfile on;
tcp_nopush on;
tcp_nodelay on;
gzip on;
gzip_comp_level 4; # increase for better compression (values 1 to 9, 1 = fastest, 9 = slowest/best compression)
gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript; # compress multiple mime types
gzip_vary on; # send Vary: Accept-Encoding header
gzip_proxied any; # enable compression for proxied requests
server
{
listen 80;
server_name vostrets.ru;
# redirect all http://vostrets.ru/* requests to http://www.vostrets.ru/*
rewrite ^(.*) http://www.vostrets.ru$1 permanent;
}
server
{
listen 80;
server_name www.vostrets.ru;
charset utf-8;
access_log /var/log/nginx_access.log;
# count skin images for static data, though they are in "bin" path
location ~* ^/xwiki/bin/skin/(.*)\.(jpg|jpeg|gif|png|ico)$
{
access_log off;
rewrite ^/xwiki/bin/skin/(.*) /xwiki/$1 permanent;
expires max;
}
# fetch all the data, which doesn't lie in "bin" path, as static data
location ~* ^/xwiki(?!/bin/).+\.(jpg|jpeg|gif|png|ico|css|js)$
{
access_log off;
# ${root} is the path, where the static files lie (i.e. ${root}/xwiki/skins/toucan/logo.png)
root /user/local/glassfish/glassfish/domains/default_domain/applications/j2ee-modules;
expires max;
}
# forward all http://vostrets.ru/ requests to http://vostrets.ru:8080/
location /
{
proxy_pass http://localhost:8080;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Proto $scheme;
expires 0m;
}
# ...
}
}
worker_processes 2; # This should be equal to number of CPUs
worker_rlimit_nofile 20000; # Max open file descriptors, increase if nginx is serving large amount of static data for many users
events
{
worker_connections 1024; # number of max connections per worker (worker_connections * worker_processes = max connections)
use epoll;
multi_accept on;
}
http
{
include mime.types;
default_type application/octet-stream;
server_tokens off; # don't send nginx version to end users
sendfile on;
tcp_nopush on;
tcp_nodelay on;
gzip on;
gzip_comp_level 4; # increase for better compression (values 1 to 9, 1 = fastest, 9 = slowest/best compression)
gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript; # compress multiple mime types
gzip_vary on; # send Vary: Accept-Encoding header
gzip_proxied any; # enable compression for proxied requests
server
{
listen 80;
server_name vostrets.ru;
# redirect all http://vostrets.ru/* requests to http://www.vostrets.ru/*
rewrite ^(.*) http://www.vostrets.ru$1 permanent;
}
server
{
listen 80;
server_name www.vostrets.ru;
charset utf-8;
access_log /var/log/nginx_access.log;
# count skin images for static data, though they are in "bin" path
location ~* ^/xwiki/bin/skin/(.*)\.(jpg|jpeg|gif|png|ico)$
{
access_log off;
rewrite ^/xwiki/bin/skin/(.*) /xwiki/$1 permanent;
expires max;
}
# fetch all the data, which doesn't lie in "bin" path, as static data
location ~* ^/xwiki(?!/bin/).+\.(jpg|jpeg|gif|png|ico|css|js)$
{
access_log off;
# ${root} is the path, where the static files lie (i.e. ${root}/xwiki/skins/toucan/logo.png)
root /user/local/glassfish/glassfish/domains/default_domain/applications/j2ee-modules;
expires max;
}
# forward all http://vostrets.ru/ requests to http://vostrets.ru:8080/
location /
{
proxy_pass http://localhost:8080;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Proto $scheme;
expires 0m;
}
# ...
}
}
Troubleshooting
Request Entity Too Large
If you find the following message in your logs: Failed to load resource: the server responded with a status of 413 (Request Entity Too Large) then you might want to try to increase the request body size since it seems nginx does not allow request bodies larger than 1MB by default.