Creating a server block for node.js
create a directory
1 2 |
sudo mkdir -p /var/www/fl.com sudo chown -R $USER:$USER /var/www/fl.com |
create a server block config file for fl.com
1 |
sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/fl.com |
add there is the next config /etc/nginx/sites-available/fl.com
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
upstream fl-com-app { ip_hash; server 127.0.0.1:3002; } server { listen 443 ssl; listen [::]:443 ssl; include snippets/snakeoil.conf; root /var/www/fl.com; index index.html; server_name fl.com www.fl.com; location / { proxy_pass http://fl-com-app; } } server { listen 80; listen [::]:80; server_name fl.com www.fl.com; return 301 https://$host$request_uri; } |
create a symbolic link
1 |
sudo ln -s /etc/nginx/sites-available/fl.com /etc/nginx/sites-enabled/ |
test configuration
1 |
sudo nginx -t |
then restart nginx
1 |
sudo systemctl restart nginx |
add the fl.com to the /etc/hosts
1 |
127.0.0.1 fl.com |
Open in browser https://fl.com and you’ll get your created server block. There we’ll be an error 502 or whatever. the fl.com will redirect everything to the 3002 port, so you have to run some server on this port.
#Set up the servers for React with GraphQL
In this case, I’m going to create two server blocks development.com and production.com.
The development.com redirects users to two different hosts http://127.0.0.1:3000 to create-react-app and http://127.0.0.1:3002 to the nodemon server.
The production.com redirects users only to the one host, afterward routing is done by using express
Here are configs:
/etc/nginx/sites-available/development.com
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 |
upstream development-com-3000 { ip_hash; server 127.0.0.1:3000; } upstream development-com-3002 { ip_hash; server 127.0.0.1:3002; } server { listen 443 ssl; listen [::]:443 ssl; include snippets/snakeoil.conf; root /var/www/development.com; index index.html; server_name development.com www.development.com; location / { proxy_pass http://development-com-3000; } location ~ /graphql/* { proxy_pass http://development-com-3002; } } server { listen 80; listen [::]:80; server_name development.com www.development.com; return 301 https://$host$request_uri; } |
/etc/nginx/sites-available/development.com
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
upstream production-com-3002 { ip_hash; server 127.0.0.1:3002; } server { listen 443 ssl; listen [::]:443 ssl; include snippets/snakeoil.conf; root /var/www/production.com; index index.html; server_name production.com www.production.com; location / { proxy_pass http://production-com-3002; } } server { listen 80; listen [::]:80; server_name production.com www.production.com; return 301 https://$host$request_uri; } |
How to turn on gzip in nginx
How to enable/disable gzip compression in nginx on a Plesk server
You can add this code in the /etc/nginx/sites-available/default
1 2 3 4 5 6 7 8 |
server { gzip on; gzip_disable "MSIE [1-6]\\.(?!.*SV1)"; gzip_proxied any; gzip_comp_level 5; gzip_types text/plain text/css application/javascript application/x-javascript text/xml application/xml application/rss+xml text/javascript image/x-icon image/bmp image/svg+xml; gzip_vary on; } |
and then
1 |
sudo nginx -t |
1 |
sudo systemctl restart nginx |
that’s it
the end