Skip to content

Instantly share code, notes, and snippets.

@nhancv
Last active October 30, 2021 12:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nhancv/3a46e406648da713736b78d9cc507799 to your computer and use it in GitHub Desktop.
Save nhancv/3a46e406648da713736b78d9cc507799 to your computer and use it in GitHub Desktop.
Nginx config sample
server {
listen 80;
listen [::]:80;
server_name api.nhancv.com;
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
}
server {
listen 80;
listen [::]:80;
root /home/nhancv/workspace/rilthot/build;
index index.html index.htm;
server_name rilthot.com www.rilthot.com;
location / {
try_files $uri $uri/ /index.html?$query_string;
}
}

Install nginx

sudo apt update
sudo apt install nginx
sudo systemctl enable nginx
sudo systemctl status nginx

Create domain config

sudo nano /etc/nginx/sites-available/api.nhancv.com
server {
    listen 80;
    listen [::]:80;

    server_name api.nhancv.com;

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_pass http://localhost:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}
sudo ln -s /etc/nginx/sites-available/api.nhancv.com /etc/nginx/sites-enabled/
sudo nginx -t
sudo service nginx reload

------
Your certificate and chain have been saved at:
   /etc/letsencrypt/live/api.nhancv.com/fullchain.pem
Your key file has been saved at:
   /etc/letsencrypt/live/api.nhancv.com/privkey.pem

Setup SSL Https

sudo apt update
sudo apt install software-properties-common -y
sudo add-apt-repository universe
sudo add-apt-repository ppa:certbot/certbot
sudo apt update
sudo apt install certbot python-certbot-nginx  -y
sudo certbot --nginx

* Select redirect all request to HTTPS, nginx will update domain config automatically
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment