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 Slide

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

    View Slide

  3. Kim Schlesinger
    @kimschles

    View Slide

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

    View Slide

  5. Agenda
    1. About nginx
    @kimschles

    View Slide

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

    View Slide

  7. @kimschles

    View Slide

  8. 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 Slide

  9. 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 Slide

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

    View Slide

  11. 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 Slide

  12. @kimschles

    View Slide

  13. Agenda
    1. About nginx
    @kimschles

    View Slide

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

    View Slide

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

    View Slide

  16. 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 Slide

  17. Thoughts on programming vs.
    configuration management
    @kimschles

    View Slide

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

    View Slide

  19. 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 Slide

  20. Reverse Proxy Demo!
    @kimschles

    View Slide

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

    View Slide

  22. 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 Slide

  23. 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 Slide

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

    View Slide

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

    View Slide

  26. Serve static content
    @kimschles

    View Slide

  27. 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 Slide

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

    View Slide

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

    View Slide

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

    View Slide

  31. Load Balancer
    @kimschles

    View Slide

  32. High Availability
    (lots of replicas)
    @kimschles

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  36. Recap
    @kimschles

    View Slide

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

    View Slide

  38. nginx.conf
    @kimschles

    View Slide

  39. Reverse Proxy
    @kimschles

    View Slide

  40. Web Cache
    @kimschles

    View Slide

  41. Load Balancer
    @kimschles

    View Slide

  42. There's more!
    @kimschles

    View Slide

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

    View Slide

  44. kimschlesinger.com
    @kimschles
    @kimschles

    View Slide