Skip to content

Instantly share code, notes, and snippets.

@khansun
Created July 15, 2022 17:25
Show Gist options
  • Save khansun/6cf5d63fed958128ed963d072b48629a to your computer and use it in GitHub Desktop.
Save khansun/6cf5d63fed958128ed963d072b48629a to your computer and use it in GitHub Desktop.
Enable HTTPS to your domain with Nginx

Requirements:

  • Ubuntu 18.04 LTS or higher
  • Nginx
  • App running on a specified port: i.e. 9001
  • DNS entry for the domain: i.e. app.example.com
  • SSL certificate for the domain: i.e. app.example.com

Make a new configuration file: app.example.com

  • sudo touch /etc/nginx/sites-avaialable/app.example.com

If you have CA bundle separately from the certificate:

Change to the directory where the certificate and the bundle are stored.

  • cd /home/user/ssl_cert
  • cat example.cert example_ca_bundle.cert >> example_combined.cert

Insert the following lines:

  • sudo vim /etc/nginx/sites-available/app.example.com

server {
listen 80;
server_name app.example.com www.app.example.com; # Edit this to your domain name
rewrite ^ https://$host$request_uri permanent;
}

server {
listen 443 ssl;

server_name app.example.com;                                               
# Edit the above _YOUR-DOMAIN_ to your domain name
   
ssl_certificate /home/user/ssl_cert/example_combined.cert;       
# If you use Lets Encrypt, you should just need to change the domain. 
# Otherwise, change this to the path to full path to your domains public certificate file.
   
ssl_certificate_key /home/user/ssl_cert/example.key;     
# If you use Let's Encrypt, you should just need to change the domain.
# Otherwise, change this to the direct path to your domains private key certificate file.
   
ssl_session_cache builtin:1000 shared:SSL:10m;                        
# Defining option to share SSL Connection with Passed Proxy
   
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
# Defining used protocol versions. 
   
ssl_ciphers HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4; 
# Defining ciphers to use. 
   
ssl_prefer_server_ciphers on;                                         
# Enabling ciphers
   
access_log /var/log/nginx/access.log;                                 
# Log Location. the Nginx User must have R/W permissions. Usually by ownership.

location / {
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_pass http://localhost:9001;
#proxy_pass unix:/path/to/php7.3.sock # This is an example of how to define a unix socket.
proxy_read_timeout 90;
}

} # Don't leave this out! It "closes" the server block we started this file with. 

Create the symbolic link at sites-enabled: app.example.com

  • sudo ln -s /etc/nginx/sites-avaialable/app.example.com /etc/nginx/sites-enabled/app.example.com.conf

Test the configuration file:

  • sudo nginx -t

Restart the nginx service:

  • sudo systemctl restart nginx
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment