Upgrade to Pro — share decks privately, control downloads, hide ads and more …

nginx and node.js

nginx and node.js

Kim Schlesinger

April 15, 2021
Tweet

More Decks by Kim Schlesinger

Other Decks in Technology

Transcript

  1. nginx and Node.js
    Kim Schlesinger
    Denver Node.js Meetup - May 2020
    @kimschles

    View full-size slide

  2. nginx can make your production
    Node.js apps faster.
    @kimschles

    View full-size slide

  3. Kim Schlesinger
    @kimschles

    View full-size slide

  4. Agenda
    1. About nginx
    2. Demo setup
    3. nginx as a Reverse Proxy, Web Cache and Load Balancer
    4. Recap
    @kimschles

    View full-size slide

  5. Agenda
    1. About nginx
    @kimschles

    View full-size slide

  6. Server
    Something that sends files to a web browser
    @kimschles

    View full-size slide

  7. About nginx
    • pronounced "engine-x"
    • A web server, reverse proxy and load balancer
    • First developed in 2004 by Igor Sysoev
    • Designed to solve the C10K Problem1
    • Written in C
    1 Concurrently handling ten thousand connections
    @kimschles

    View full-size slide

  8. Moar about nginx
    • Open Source nginx and nginx Plus
    • Serves 28.5% active sites on the web2
    • Apache is 2nd with 27.8% websites
    2
    Netcraft April 2020 Web Server Survey
    @kimschles

    View full-size slide

  9. How do I know which webserver is
    serving my content?
    @kimschles

    View full-size slide

  10. Look at the http response headers using
    your browser's dev tools.
    • Open dev tools
    • Go to the network tab
    • Refresh the page
    • Look at response headers
    @kimschles

    View full-size slide

  11. Agenda
    1. About nginx
    @kimschles

    View full-size slide

  12. Agenda
    1. About nginx
    2. Demo setup
    @kimschles

    View full-size slide

  13. Demo Setup
    • Digital Ocean Droplet
    • Ubuntu 18.04.3
    • iTerm2 Profiles
    • Dockerized Node.js apps
    • Code at github.com/kimschles/nginx-nodejs
    @kimschles

    View full-size slide

  14. Prerequisite Skills
    • Setting up a remote server
    • Using SSH to connect to the server
    • Building an app with Node.js and Express
    • Containerizing Node.js apps with Docker
    • Deploying apps using Docker
    @kimschles

    View full-size slide

  15. Thoughts on programming vs.
    configuration management
    @kimschles

    View full-size slide

  16. Agenda
    1. About nginx
    2. Demo setup
    3. nginx as a Reverse Proxy
    @kimschles

    View full-size slide

  17. Reverse Proxy
    A reverse proxy takes requests from the Internet and
    forwards them to servers in an internal network.3
    3
    MDN: Proxy Server
    @kimschles

    View full-size slide

  18. Reverse Proxy Demo!
    @kimschles

    View full-size slide

  19. The simplest possible
    configuration
    server {
    location / {
    proxy_pass http://:8080/;
    }
    }
    @kimschles

    View full-size slide

  20. One with a little more info
    server {
    location / {
    proxy_pass http://:8080/;
    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;
    }
    }
    @kimschles

    View full-size slide

  21. How to setup a Reverse Proxy
    • Download nginx
    • Find and edit the configuration file
    • /etc/nginx/sites-available/default
    • Reload nginx with service nginx reload
    • Check to see if nginx is the server
    @kimschles

    View full-size slide

  22. Agenda
    1. About nginx
    2. Demo setup
    3. nginx as a Reverse Proxy
    @kimschles

    View full-size slide

  23. Agenda
    1. About nginx
    2. Demo setup
    3. nginx as a Web Cache
    @kimschles

    View full-size slide

  24. Serve static content
    @kimschles

    View full-size slide

  25. index.html





    Hey!


    Hello Denver Node.js Meetup!
    alt="A husky puppy in the woods running toward the camera">
    Isn't this a cute puppy?


    @kimschles

    View full-size slide

  26. Serve Static HTML files
    server {
    location ~ \.(html)$ {
    root /var/www/staticfiles;
    expires 30d;
    }
    location / {
    proxy_pass http://:3000;
    }
    }
    @kimschles

    View full-size slide

  27. Agenda
    1. About nginx
    2. Demo setup
    3. nginx as a Web Cache
    @kimschles

    View full-size slide

  28. Agenda
    1. About nginx
    2. Demo setup
    3. nginx as a Load Balancer
    @kimschles

    View full-size slide

  29. Load Balancer
    @kimschles

    View full-size slide

  30. High Availability
    (lots of replicas)
    @kimschles

    View full-size slide

  31. Round Robin Load Balancer
    upstream nodeapps {
    server :3000;
    server :8080;
    }
    server {
    location / {
    proxy_pass http://nodeapps;
    }
    }
    @kimschles

    View full-size slide

  32. Least Connections Load Balancer
    upstream nodeapps {
    least_conn;
    server :3000;
    server :8080;
    }
    server {
    location / {
    proxy_pass http://nodeapps;
    }
    }
    @kimschles

    View full-size slide

  33. IP Hash Load Balancer
    upstream nodeapps {
    ip_hash;
    server :3000;
    server :8080;
    }
    server {
    location / {
    proxy_pass http://nodeapps;
    }
    }
    @kimschles

    View full-size slide

  34. Recap
    @kimschles

    View full-size slide

  35. nginx can make your production
    Node.js apps faster.
    @kimschles

    View full-size slide

  36. nginx.conf
    @kimschles

    View full-size slide

  37. Reverse Proxy
    @kimschles

    View full-size slide

  38. Web Cache
    @kimschles

    View full-size slide

  39. Load Balancer
    @kimschles

    View full-size slide

  40. There's more!
    @kimschles

    View full-size slide

  41. Resources
    • Full Example Configuration
    • 5 Performance Tips for Node.js Applications
    @kimschles

    View full-size slide

  42. kimschlesinger.com
    @kimschles
    @kimschles

    View full-size slide