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. Agenda 1. About nginx 2. Demo setup 3. nginx as

    a Reverse Proxy, Web Cache and Load Balancer 4. Recap @kimschles
  2. 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
  3. 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
  4. 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
  5. Demo Setup • Digital Ocean Droplet • Ubuntu 18.04.3 •

    iTerm2 Profiles • Dockerized Node.js apps • Code at github.com/kimschles/nginx-nodejs @kimschles
  6. 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
  7. 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
  8. The simplest possible configuration server { location / { proxy_pass

    http://<IP_ADDRESS_OF_YOUR_SERVER>:8080/; } } @kimschles
  9. 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
  10. 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
  11. 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
  12. Serve Static HTML files server { location ~ \.(html)$ {

    root /var/www/staticfiles; expires 30d; } location / { proxy_pass http://<IP_ADDRESS_OF_YOUR_SERVER>:3000; } } @kimschles
  13. 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
  14. 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
  15. 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