In this article, learn how to install nginx, and basically introduce the usage of nginx.
As a example, using Ubuntu 22.04 LTS

1. Installing Nginx on sever

On terminal, execute:

1
apt install nginx

It will automatically install the nginx for you, now execute:

1
nginx

which will run the nginx for you, you could visit your server IP address which supposed to have a welcome page of nginx if everything goes well.

If nothing there, please check your firewall configuration. In Ubuntu, please execute the following command to check the firewall status:

1
2
3
ufw status
ufw allow 80 # allowing port 80
ufw allow 443 # allowing port 443

2. Configuration Nginx

All nginx stuff will be install on /etc/nginx, and the configuration is at nginx.config, you could whether using vim or any you preferred editor to edit that file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
worker_processes auto;

events {
worker_connections 1024;
}

http {
# Add basic HTTP settings
include mime.types;
default_type application/octet-stream;

# Optional: Add some basic performance optimizations
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;

# HTTP redirects to HTTPS
server {
listen 80;
server_name [YOUR DOMAIN (if multi then separate by using space)];
return 301 https://$server_name$request_uri;
}

# actual server configuration
server {
listen 443 ssl http2;
server_name [YOUR DOMAIN (if multi then separate by using space)];

# SSL Configuration
ssl_certificate [/path/to/your/full_chain/certificate];
ssl_certificate_key [/path/to/your/full_chain/certificate_key];
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;
ssl_prefer_server_ciphers off;
ssl_session_timeout 1d;
ssl_session_cache shared:SSL:50m;
ssl_session_tickets off;

# Security headers
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Content-Type-Options "nosniff" always;
add_header X-XSS-Protection "1; mode=block" always;
add_header 'Access-Control-Allow-Origin' '*' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range' always;

location / {
proxy_pass [Your target address, e.g. http://localhost:3000];
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_set_header X-Forwarded-Port $server_port;
proxy_set_header X-Forwarded-Host $host;

proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
}
}
}

1
2
3
4
5
6
nginx -s stop # Stop nginx
nginx -s reload # Reload nginx
nginx -v # Check nginx version
nginx -t # Syntax checking of your configuration file
nginx # Start nginx server
lsof -i:[port] # Check all processes using port [port]