Nginx config

Nginx is a powerful web proxy server. Recently, I used it to deploy my GoReactChatApp on VPS. The remaining applies to a ubuntu 18 server

Install

1
2
sudo apt update
sudo apt install nginx

Config fireware

1
sudo ufw app list

you should see if Nginx installed correctly

1
2
3
4
5
Available applications:
Nginx Full
Nginx HTTP
Nginx HTTPS
OpenSSH

Enable Nginx by

1
2
sudo ufw allow 'Nginx HTTP'
sudo ufw status #to check

About Nginx service

1
2
3
sudo service nginx start
sudo service nginx stop
sudo service nginx status

etc/nginx/nginx.conf

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
user your_user_name;
worker_processes 1;

error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;

events {
worker_connections 1024;
}

http {
include /etc/nginx/mime.types;
default_type application/octet-stream;

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

access_log /var/log/nginx/access.log main;

sendfile on;
#tcp_nopush on;

client_body_buffer_size 10m;
client_header_buffer_size 1k;
client_max_body_size 5m;
large_client_header_buffers 2 1k;
client_body_timeout 10;
client_header_timeout 10;
keepalive_timeout 5 5;
send_timeout 10;
server_tokens off;
#gzip on; on;
include /etc/nginx/conf.d/*.conf;
}

For a simple client/server project
with the floowing structure

1
2
3
4
5
6
/home/
-your_user_name/
-client/
-deploy/ #static files html/js/css
-server_logs
-server/ # your server could be node.js / golang

/etc/nginx/conf.d/default.conf

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
server {
#listen 80;
listen 80 default_server;
listen [::]:80 default_server;
server_name your_domain_name;

access_log /home/your_user_name/client/server_logs/host.access.log main;

location / {
root /home/your_user_name/client/deploy;
index index.html index.htm;
try_files $uri /index.html;
add_header X-Frame-Options SAMEORIGIN;
add_header X-Content-Type-Options nosniff;
add_header X-XSS-Protection "1; mode=block";
add_header Strict-Transport-Security "max-age=31536000; includeSubdomains;";
}
location /api {
proxy_pass http://localhost:5000;
}
location /ws {
proxy_pass http://localhost:5000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
location /uploads {
proxy_pass http://localhost:5000;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}


server_tokens off;

location ~ /\.ht {
deny all;
}
}

}