Setting up nginx on Ubuntu for parse-server

Before You Begin

  1. Check for updates
    sudo apt-get update && sudo apt-get upgrade

Install nginx

  1. Install nginx
    sudo apt-get install -y nginx
  2. Decide how you want to access parse:
    a. example.com/parseapp/
    b. parseapp.example.com/ (don’t forget to add an A record for parseapp pointing to the IP address and include subdomain in Let’s Encrypt certificates)

Setup nginx for example.com/parseapp/

  1. Change directory
    cd /etc/nginx/sites-enabled/
  2. Open default
    sudo nano default
  3. Select all
    ctrl+shift+6
    ↓↓↓↓ to the bottom
    ctrl+K
  4. Paste the following in, changing example.com, parseapp, and portnumber
    # HTTP - redirect all requests to HTTPS
    server {
        listen 80;
        listen [::]:80 default_server ipv6only=on;
        return 301 https://\$host\$request_uri;
    }
    
    # HTTPS - serve HTML from /usr/share/nginx/html, proxy requests to /parse/
    # through to Parse Server
    server {
            listen 443;
            server_name example.com;
    
            root /usr/share/nginx/html;
            index index.html index.htm;
    
            ssl on;
            # Use certificate and key provided by Let's Encrypt:
            ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
            ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
            ssl_session_timeout 5m;
            ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
            ssl_prefer_server_ciphers on;
            ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';
    
            # Pass requests for example.com/parseapp/ to parse-server instance at localhost:portnumber
    
            location /parseapp/ {
                    proxy_set_header X-Real-IP \$remote_addr;
                    proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
                    proxy_set_header X-NginX-Proxy true;
                    proxy_pass http://localhost:portnumber/parse/;
                    proxy_ssl_session_reuse off;
                    proxy_set_header Host \$http_host;
                    proxy_redirect off;
            }
            location / {
                    try_files \$uri \$uri/ =404;
            }
    }
  5. Save and exit
  6. Restart nginx
    sudo service nginx restart

Setup nginx for parseapp.example.com/

  1. Change directory
    cd /etc/nginx/sites-enabled/
  2. Open default
    sudo nano default
  3. Select all
    ctrl+shift+6
    ↓↓↓↓ to the bottom
    ctrl+K
  4. Paste the following in, changing example.com, parseapp, and portnumber
    # HTTP - redirect all requests to HTTPS
    server {
        listen 80;
        listen [::]:80 default_server ipv6only=on;
        return 301 https://\$host\$request_uri;
    }
    
    # HTTPS - serve HTML from /usr/share/nginx/html, proxy requests to /parse/
    # through to Parse Server
    server {
            listen 443;
            server_name example.com;
    
            root /usr/share/nginx/html;
            index index.html index.htm;
    
            ssl on;
            # Use certificate and key provided by Let's Encrypt:
            ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
            ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
            ssl_session_timeout 5m;
            ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
            ssl_prefer_server_ciphers on;
            ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';
    
            location / {
                    try_files \$uri \$uri/ =404;
            }
    }
    
    server {
            listen 443;
            server_name parseapp.example.com;
    
            root /usr/share/nginx/html;
            index index.html index.htm;
    
            ssl on;
            # Use certificate and key provided by Let's Encrypt:
            ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
            ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
            ssl_session_timeout 5m;
            ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
            ssl_prefer_server_ciphers on;
            ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';
    
            # Pass requests for parseapp.example.com/ to Parse Server instance at localhost:portnumber/parse/
    
            location / {
                    proxy_set_header X-Real-IP \$remote_addr;
                    proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
                    proxy_set_header X-NginX-Proxy true;
                    proxy_pass http://localhost:portnumber/parse/;
                    proxy_ssl_session_reuse off;
                    proxy_set_header Host \$http_host;
                    proxy_redirect off;
            }
    }
  5. Save and exit
  6. Restart nginx
    sudo service nginx restart

Mini series

  1. Setting up a server at Linode
  2. Install Let’s Encrypt to Create SSL Certificates on Ubuntu
  3. Setting up MongoDB on Ubuntu
  4. Setting up nginx on Ubuntu for parse-server
  5. Setting up parse-server on Ubuntu

2 Replies to “Setting up nginx on Ubuntu for parse-server”

Comments are closed.