Configure Nginx web server for static content, reverse proxy, load balancing, and FastCGI applications following official best practices
Configure Nginx web server for serving static content, setting up reverse proxies, and connecting FastCGI applications based on the official Nginx Beginner's Guide.
This skill helps you set up and configure Nginx for common use cases including:
**Starting Nginx:**
**Control commands:**
```bash
nginx -s stop # Fast shutdown
nginx -s quit # Graceful shutdown (wait for current requests)
nginx -s reload # Reload configuration without downtime
nginx -s reopen # Reopen log files
```
**Checking running processes:**
```bash
ps -ax | grep nginx
```
**Using kill signals (alternative):**
```bash
kill -s QUIT <master_pid> # Graceful shutdown
```
**Configuration file location (typical):**
**Configuration concepts:**
**Hierarchy:**
- `events` context
- `http` context
- `server` context
- `location` context
**Setup steps:**
1. Create directories:
```bash
mkdir -p /data/www
mkdir -p /data/images
```
2. Add content:
```bash
echo "<h1>Welcome</h1>" > /data/www/index.html
```
3. Edit nginx.conf and add server block:
```nginx
http {
server {
location / {
root /data/www;
}
location /images/ {
root /data;
}
}
}
```
4. Reload configuration:
```bash
nginx -s reload
```
**How it works:**
**Use case:** Route requests to backend servers, serve images locally
**Configuration example:**
1. Define proxied backend server:
```nginx
server {
listen 8080;
root /data/up1;
location / {
}
}
```
2. Configure proxy server:
```nginx
server {
location / {
proxy_pass http://localhost:8080/;
}
location ~ \.(gif|jpg|png)$ {
root /data/images;
}
}
```
**How it works:**
**Use case:** Connect Nginx to PHP or other FastCGI applications
**Configuration:**
```nginx
server {
location / {
fastcgi_pass localhost:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
}
location ~ \.(gif|jpg|png)$ {
root /data/images;
}
}
```
**How it works:**
**After configuration changes:**
```bash
nginx -t
nginx -s reload
```
**Check logs:**
**Common issues:**
**Example 1: Static website**
```nginx
http {
server {
listen 80;
server_name example.com;
location / {
root /var/www/example.com;
index index.html;
}
}
}
```
**Example 2: Reverse proxy with API backend**
```nginx
http {
server {
listen 80;
location /api/ {
proxy_pass http://localhost:3000/;
}
location / {
root /var/www/frontend;
}
}
}
```
**Example 3: PHP application**
```nginx
http {
server {
listen 80;
root /var/www/php-app;
location / {
index index.php;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
}
```
Leave a review
No reviews yet. Be the first to review this skill!
# Download SKILL.md from killerskills.ai/api/skills/nginx-beginner-setup/raw