Skip to content

Ubuntu Nginx

Cooper Filby edited this page Oct 9, 2017 · 2 revisions

NGINX and REVERSE PROXY SERVER

Installing Nginx

More Info

  1. Update apt-get: sudo apt-get update

  2. Install Nginx: sudo apt-get install nginx

  3. Do you want to continue: Y

  4. Add Nginx to Firewall:

    a. List of all apps that ufw knows: sudo ufw app list

    i.	You should see a list of application profiles including Nginx
    

    b. If SSL is not configured, we only need to allow traffic on port 80 which is Nginx HTTP (Nginx HTTPS is port 443 [ TLS/SSL encrypted]): sudo ufw allow ‘Nginx HTTP’

    c. Verify status: sudo ufw status

    i.	You should see HTTP traffic allowed.
    

Check the webserver

  1. Check Nginx webserver: systemctl status nginx

    a. Ngnix should be active

  2. Test Nginx by typing in your browser: http://<server_ipaddress>

    a. You should get a Welcome to nginx! Page

Manage Nginx

Here are some common commands used when managing nginx:

  • Stop webserver: sudo systemctl stop nginx
  • Start webserver: sudo systemctl start nginx
  • Restart webserver: sudo systemctl restart nginx
  • Reload without dropping connections: sudo systemctl reload nginx
  • To stop Nginx from starting at boot time: sudo systemctl disable nginx
  • To re-enable Nginx to start at boot time: sudo systemctl enable nginx

Secure Nginx with Let’s Encrypt (TLS/SSL)

Note: This is not recommended for low connectivity deployments. Let's Encrypt needs internet connectivity to renew it's SSL Certificate.

  1. More Info a. This will work with a specified domain and then we can add Nginx Full to the ufw and not only Nginx HTTP

Setup Nginx as Reverse Proxy

  1. Using the sudo account: sudo nano /etc/nginx/sites-available/default
  2. Add the following lines:
####   # For security-by-obscurity: stop displaying nginx version
    server_tokens off;
    set_real_ip_from <server_ip_address>;
    set_real_ip_from 127.0.0.1;

####   # This section is needed to proxy web-socket connections
    map $http_upgrade $connection_upgrade {
        default upgrade;
        ''      close;
    }

    upstream <app_name> {
        server 127.0.0.1:8080;
    }

    server {
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;

####   # These two lines, root and index are irrelevant, add index.php to the index if you are using PHP
        root /usr/share/nginx/html;
        index index.html index.htm index.nginx-debian.html;

####   # Put your domain name
        server_name <your_domain>;

        access_log /var/log/nginx/meteor.access.log;
        error_log  /var/log/nginx/meteor.error.log;

        proxy_buffers 4 256k;
        proxy_buffer_size 128k;

####   # Type this within the location stanza
        location \ {
            proxy_pass http://localhost:8080;
            proxy_http_version 1.1;

            proxy_set_header    Upgrade         $http_upgrade;
            proxy_set_header    Connection      $connection_upgrade;
            proxy_set_header    Host            $host;
            proxy_cache_bypass                  $http_upgrade;
            proxy_set_header    X-Real-IP       $remote_addr;
            proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }
  1. Save the file and check if there are any errors: sudo nginx -t If it is successful...
  2. Restart nginx: sudo service nginx restart
  3. Check nginx status: systemctl status nginx.service

Next Steps

After installing Nginx we'll need to install NodeJS and PM2.