Basic Nginx configuration
Often during a penetration test, phishing awareness campaign, or red team, I need to set up a webserver to host payloads, phishing websites, etc.
This short post is a collection of Nginx configuration directives that I
commonly use.
Example use cases: redirect HTTP traffic to HTTPS, enable PHP
processing, configure a reverse proxy for HTTP(s) or TCP/UDP streams,
add or remove HTTP headers in the responses or adding them for upstream servers.
Redirect HTTP traffic to HTTPS
server {
listen 80;
return 301 https://$host$request_uri;
}
Enable PHP
Enable PHP file processing on SSL website, with php-fpm
:
server {
listen 8443 ssl default_server;
server_name _;
ssl_certificate /etc/letsencrypt/live/DOMAIN-NAME/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/DOMAIN-NAME/privkey.pem;
root /var/www/html;
index index.html index.htm index.php;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
}
Streams
Forward TCP port 443
to target-host:8443
:
Note: stream
must be on the same level as http
- put such entry in
nginx.conf
file, not in VHOST configuration files.
stream {
server {
listen 443;
proxy_pass target-host:8443;
}
}
Redirect all DNS traffic:
stream {
server {
listen 53 udp;
proxy_pass 10.0.0.1:53;
}
}
Basic nginx.conf
with stream:
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
gzip on;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
stream {
server {
listen 443;
proxy_pass target-host:8443;
}
}
Adding or removing headers
Hide X-Server
header example:
location / {
proxy_pass http://127.0.0.1:8888/;
proxy_hide_header X-Server;
}
Add X-Frame-Options
header:
location / {
proxy_pass http://127.0.0.1:8888/;
add_header X-Frame-Options SAMEORIGIN;
}
Adding headers to proxy upstream
location / {
proxy_pass http://127.0.0.1:8888/;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Real-IP $remote_addr;
}