$30 off During Our Annual Pro Sale. View details »

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
  2. nginx can make your production Node.js apps faster. @kimschles

  3. Kim Schlesinger @kimschles

  4. Agenda 1. About nginx 2. Demo setup 3. nginx as

    a Reverse Proxy, Web Cache and Load Balancer 4. Recap @kimschles
  5. Agenda 1. About nginx @kimschles

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

  7. @kimschles

  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
  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
  10. How do I know which webserver is serving my content?

    @kimschles
  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
  12. @kimschles

  13. Agenda 1. About nginx @kimschles

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

  15. Demo Setup • Digital Ocean Droplet • Ubuntu 18.04.3 •

    iTerm2 Profiles • Dockerized Node.js apps • Code at github.com/kimschles/nginx-nodejs @kimschles
  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
  17. Thoughts on programming vs. configuration management @kimschles

  18. Agenda 1. About nginx 2. Demo setup 3. nginx as

    a Reverse Proxy @kimschles
  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
  20. Reverse Proxy Demo! @kimschles

  21. The simplest possible configuration server { location / { proxy_pass

    http://<IP_ADDRESS_OF_YOUR_SERVER>:8080/; } } @kimschles
  22. One with a little more info server { location /

    { proxy_pass http://<IP_ADDRESS_OF_YOUR_SERVER>: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
  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
  24. Agenda 1. About nginx 2. Demo setup 3. nginx as

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

    a Web Cache @kimschles
  26. Serve static content @kimschles

  27. index.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport"

    content="width=device-width, initial-scale=1.0"> <title>Hey!</title> </head> <body> <h1>Hello Denver Node.js Meetup!</h1> <img src="https://images.unsplash.com/photo-1536239742832-f20229b8ae75?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60" alt="A husky puppy in the woods running toward the camera"> <p>Isn't this a cute puppy?</p> </body> </html> @kimschles
  28. Serve Static HTML files server { location ~ \.(html)$ {

    root /var/www/staticfiles; expires 30d; } location / { proxy_pass http://<IP_ADDRESS_OF_YOUR_SERVER>:3000; } } @kimschles
  29. Agenda 1. About nginx 2. Demo setup 3. nginx as

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

    a Load Balancer @kimschles
  31. Load Balancer @kimschles

  32. High Availability (lots of replicas) @kimschles

  33. Round Robin Load Balancer upstream nodeapps { server <IP_ADDRESS_OF_YOUR_SERVER>:3000; server

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

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

    server <IP_ADDRESS_OF_YOUR_SERVER>:8080; } server { location / { proxy_pass http://nodeapps; } } @kimschles
  36. Recap @kimschles

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

  38. nginx.conf @kimschles

  39. Reverse Proxy @kimschles

  40. Web Cache @kimschles

  41. Load Balancer @kimschles

  42. There's more! @kimschles

  43. Resources • Full Example Configuration • 5 Performance Tips for

    Node.js Applications @kimschles
  44. kimschlesinger.com @kimschles @kimschles