Nginx 配置详解:让你的网站更快、更稳定、更安全

什么是 Nginx

Nginx 是一个高性能的 Web 服务器和反向代理服务器,它可以支持数百万并发连接,是运行最快的 Web 服务器之一。它还可以作为负载均衡器、缓存服务器和安全防护设备使用,被广泛应用于 Web 应用架构中。

安装 Nginx

在 Ubuntu 系统上,可以使用以下命令安装 Nginx:

sudo apt update
sudo apt install nginx

安装完成后,可以使用以下命令启动 Nginx:

sudo systemctl start nginx

如果想要在系统启动时自动启动 Nginx,则可以使用以下命令:

sudo systemctl enable nginx

Nginx 配置文件

Nginx 的配置文件位于 /etc/nginx 目录下,主配置文件为 nginx.conf。在该文件中,可以配置 Nginx 的全局选项、http 配置、server 配置和 location 配置等。

Nginx 配置详解:让你的网站更快、更稳定、更安全

全局选项

在全局选项中,可以配置 Nginx 的工作进程数、日志格式、日志路径等。以下是一个示例:

user www-data;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

http 配置

在 http 配置中,可以配置 http 服务器的全局选项、默认的 server 配置、缓存配置等。以下是一个示例:

http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    server_names_hash_bucket_size 64;
    client_max_body_size 10M;

    server {
        listen 80 default_server;
        server_name example.com;
        root /var/www/example.com;
        index index.html;
    }

    ...
}

server 配置

在 server 配置中,可以配置一个虚拟主机的选项、默认的 location 配置、SSL 配置等。以下是一个示例:

server {
    listen 80;
    server_name example.com;
    root /var/www/example.com;
    index index.html;

    location / {
        try_files $uri $uri/ /index.html;
    }

    location /api/ {
        proxy_pass http://localhost:3000;
    }
}

location 配置

在 location 配置中,可以配置针对某个 URI 的选项,如缓存时间、反向代理、重定向等。以下是一个示例:

location / {
    expires 1d;
    add_header Cache-Control "public";

    try_files $uri $uri/ /index.html;
}

location /api/ {
    proxy_pass http://localhost:3000;
}

常用 Nginx 模块

反向代理

Nginx 的反向代理模块可以将请求转发到后端应用服务器,从而实现负载均衡、高可用、高并发等功能。以下是一个示例:

server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://localhost:3000;
    }
}

负载均衡

Nginx 的负载均衡模块可以将请求分发到多个后端应用服务器,从而实现高可用、高并发等功能。以下是一个示例:

http {
    upstream backend {
        server 192.168.1.101;
        server 192.168.1.102;
        server 192.168.1.103;
    }

    server {
        listen 80;
        server_name example.com;

        location / {
            proxy_pass http://backend;
        }
    }
}

缓存

Nginx 的缓存模块可以缓存静态资源和动态页面,从而减轻后端服务器的压力,提高网站的访问速度。以下是一个示例:

http {
    proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=backend_cache:10m inactive=60m;
    proxy_cache_key "$scheme$request_method$host$request_uri";

    server {
        listen 80;
        server_name example.com;

        location / {
            proxy_cache backend_cache;
            proxy_cache_valid 200 60m;
            proxy_cache_valid 404 1m;
            proxy_cache_bypass $http_pragma;
            proxy_cache_revalidate on;
            proxy_cache_lock on;

            proxy_pass http://backend;
        }
    }
}

安全防护

Nginx 的安全防护模块可以防止恶意攻击、保护网站安全。以下是一些常见的安全防护配置:

防止 DDOS 攻击

http {
    limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;

    server {
        listen 80;
        server_name example.com;

        location / {
            limit_req zone=one burst=5;
            proxy_pass http://backend;
        }
    }
}

防止 SQL 注入攻击

http {
    server {
        listen 80;
        server_name example.com;

        location / {
            if ($query_string ~* "union.*select.*\(") {
                return 403;
            }

            proxy_pass http://backend;
        }
    }
}

防止 XSS 攻击

http {
    add_header X-Content-Type-Options nosniff;
    add_header X-XSS-Protection "1; mode=block";

    server {
        listen 80;
        server_name example.com;

        location / {
            proxy_pass http://backend;
        }
    }
}

总结

Nginx 是一个高性能的 Web 服务器和反向代理服务器,可以支持数百万并发连接,被广泛应用于 Web 应用架构中。通过 Nginx 的反向代理、负载均衡、缓存和安全防护等模块,可以让你的网站更快、更稳定、更安全。

最后编辑于:2023/09/28作者: 心语漫舞