Skip to main content

NGINX Web Server

Server Block Structure

# Define a server block (listens on port 80 by default for HTTP)
server {
  listen [port]; # Optional, default is 80
  server_name example.com www.example.com;
  root /path/to/root;
  index index.html;
}

Location Block

# Define location blocks for specific path handling
location / {
  # Configuration for the root location
}

location /static/ {
  # Configuration for the "/static/" path
}

Listen on Port

server {
  # Standard HTTP Protocol
  listen 80;

  # Standard HTTPS Protocol
  listen 443 ssl;

  # Listen on 80 using IPv6
  listen [::]:80;

  # Listen only on using IPv6
  listen [::]:80 ipv6only=on;
}

Redirects

# Redirects www to non-www
server {
  listen 80;
  server_name www.example.com;
  return 301 http: //example.com$request_uri;
}

Domain Name

server {
  # Listen to yourdomain.com
  server_name yourdomain.com;

  # Listen to multiple domains
  server_name yourdomain.com www.yourdomain.com;

  # Listen to all domains
  server_name *.yourdomain.com;

  # Listen to all top-level domains
  server_name yourdomain.*;

  # Listen to unspecified Hostname (Listens to IP address itself)
  server_name "";
}