Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Manthan Ankolekar
Manthan Ankolekar

Posted on

     

NGINX Cheatsheet: A Quick Reference Guide

NGINX is a powerful and versatile web server that is widely used to serve static content, reverse proxy, and load balance across servers. Whether you're a beginner or an experienced user, having a cheatsheet handy can save time and help you navigate NGINX configurations efficiently. Let's dive into a quick reference guide for NGINX.

Installation and Basic Commands

Installation

sudoapt-get updatesudoapt-getinstallnginx
Enter fullscreen modeExit fullscreen mode

Start/Stop/Restart NGINX

sudoservice nginx startsudoservice nginx stopsudoservice nginx restart
Enter fullscreen modeExit fullscreen mode

Check NGINX Configuration

nginx-t
Enter fullscreen modeExit fullscreen mode

Configuration File Locations

  • Main configuration file:/etc/nginx/nginx.conf
  • Server block configuration:/etc/nginx/sites-available/
  • Enabled server block symlink:/etc/nginx/sites-enabled/

Server Blocks

Basic Server Block Structure

server{listen80;server_nameexample.comwww.example.com;location/{# Configuration for handling requests}}
Enter fullscreen modeExit fullscreen mode

Redirect HTTP to HTTPS

server{listen80;server_nameexample.comwww.example.com;return301https://$host$request_uri;}
Enter fullscreen modeExit fullscreen mode

Locations and Directives

Root Directive

location/{root/path/to/your/files;indexindex.html;}
Enter fullscreen modeExit fullscreen mode

Proxy Pass

location/app{proxy_passhttp://backend_server;proxy_set_headerHost$host;proxy_set_headerX-Real-IP$remote_addr;}
Enter fullscreen modeExit fullscreen mode

URL Rewriting

location/blog{rewrite^/blog/(.*)$/$1break;}
Enter fullscreen modeExit fullscreen mode

SSL Configuration

SSL Certificate

ssl_certificate/path/to/your/certificate.crt;ssl_certificate_key/path/to/your/private.key;
Enter fullscreen modeExit fullscreen mode

Enable SSL

server{listen443ssl;server_nameexample.com;# SSL configuration here}
Enter fullscreen modeExit fullscreen mode

SSL Redirect

server{listen80;server_nameexample.com;return301https://$host$request_uri;}
Enter fullscreen modeExit fullscreen mode

Load Balancing

Round Robin Load Balancing

upstreambackend{serverbackend1.example.com;serverbackend2.example.com;}server{location/{proxy_passhttp://backend;}}
Enter fullscreen modeExit fullscreen mode

This cheatsheet covers essential NGINX configurations, but remember to consult the official documentation for more in-depth details. NGINX's flexibility allows it to be used in various scenarios, making it a crucial tool for web server management.

Top comments(1)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
usaidyf profile image
Usaid Yousuf
  • Location
    Karachi, Pakistan
  • Education
    Little Folk's School
  • Work
    Freelance Full Stack Web Developer
  • Joined

Thanks! Saved it

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Software Developer | JavaScript | Angular | Nodejs | MongoDB | Express.js | Python | Firebase | MySQL | Postgresql |
  • Location
    Karnataka, India
  • Education
    Bachelor of Engineering in Computer Science
  • Work
    Software Developer
  • Joined

More fromManthan Ankolekar

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp