What is Nginx #
Nginx (pronounced "engine-x") is a high-performance web server and reverse proxy server. It is known for its ability to handle a large number of simultaneous connections with low memory usage, making it an ideal choice for high-traffic websites and applications.
Why it is recommended not run Nginx in Docker?! #
While Nginx can be used in a container during development, it's better to run a single instance on the server and write rules to handle all applications on the server.
It makes much more ease to handle multiple domains or services running on a single machine, and you can avoid Networking hell between Docker containers.
Install Nginx #
Write following commands into the terminal:
sudo apt update
sudo apt install nginx
Verify install:
systemctl status nginx
Restart:
systemctl restart nginx
Verify settings before apply:
sudo nginx -t
Add Reverse Proxy confs HTTP #
Open Nginx config with the following command:
nano /etc/nginx/nginx.conf
Hint: You can save with CRTL + O
and exit with CRTL + X
on Mac Command + O / Command + X
This is an example to reverse Proxy HTTP traffic:
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
client_max_body_size 10M;
include /etc/nginx/mime.types;
default_type application/octet-stream;
gzip on;
gzip_disable "msie6";
server {
listen 80;
server_name selfhost.guru;
location / {
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:1234;
proxy_redirect off;
}
}
}
Add Reverse Proxy confs Websocket #
For Websocket connections the following configuration needed:
server {
listen 80;
server_name websocket.selfhost.guru;
location / {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://localhost:123456/;
}
}
Advanced Example Configuration #
Here is an advanced example how you can use multiple service and multiple domain in a single config:
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
client_max_body_size 10M;
include /etc/nginx/mime.types;
default_type application/octet-stream;
gzip on;
gzip_disable "msie6";
server {
listen 80;
server_name selfhost.guru;
location / {
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:1234;
proxy_redirect off;
}
}
# Subdomain domain
server {
listen 80;
server_name subdomain.selfhost.guru;
location / {
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:12345;
proxy_redirect off;
}
}
# Multiple domain point to same service
server {
listen 80;
server_name subdomain2.selfhost.guru, subdomain3.selfhost.guru;
location / {
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:5678;
proxy_redirect off;
}
}
}