插件窝 干货文章 Nginx初始化配置的方法

Nginx初始化配置的方法

Nginx 配置 example conf 383    来源:    2025-04-23

Nginx初始化配置指南

基本配置方法

1. 安装后初始配置

Nginx安装完成后,默认配置文件通常位于: - /etc/nginx/nginx.conf (Linux) - /usr/local/etc/nginx/nginx.conf (macOS) - C:\nginx\conf\nginx.conf (Windows)

2. 主要配置文件结构

# 全局块 - 影响Nginx整体运行的配置
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# events块 - 影响Nginx服务器与用户的网络连接
events {
    worker_connections 1024;
}

# http块 - 代理、缓存、日志等大多数功能配置
http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    # server块 - 虚拟主机配置
    server {
        listen 80;
        server_name example.com;

        # location块 - 请求路由配置
        location / {
            root /var/www/html;
            index index.html;
        }
    }
}

常用初始化配置步骤

1. 设置工作进程数

worker_processes auto; # 自动根据CPU核心数设置

2. 配置事件模型

events {
    worker_connections 1024; # 每个worker进程的最大连接数
    use epoll; # Linux高效事件模型
    multi_accept on; # 同时接受多个新连接
}

3. 基础HTTP配置

http {
    sendfile on; # 启用高效文件传输
    tcp_nopush on; # 优化数据包发送
    tcp_nodelay on; # 禁用Nagle算法

    keepalive_timeout 65; # 保持连接超时时间
    types_hash_max_size 2048;

    include /etc/nginx/mime.types; # MIME类型定义
    default_type application/octet-stream;

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    gzip on; # 启用Gzip压缩
    gzip_types text/plain text/css application/json application/javascript text/xml;
}

4. 配置虚拟主机

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

    root /var/www/example.com;
    index index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

配置检查与重载

  1. 测试配置文件语法:
sudo nginx -t
  1. 重载配置(不中断服务):
sudo nginx -s reload

安全基础配置

# 隐藏Nginx版本号
server_tokens off;

# 禁用不需要的HTTP方法
if ($request_method !~ ^(GET|HEAD|POST)$ ) {
    return 405;
}

# 防止点击劫持
add_header X-Frame-Options "SAMEORIGIN";

# XSS保护
add_header X-XSS-Protection "1; mode=block";

# 内容安全策略
add_header Content-Security-Policy "default-src 'self'";

性能优化配置

# 启用缓存
open_file_cache max=1000 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
open_file_cache_errors on;

# 缓冲区优化
client_body_buffer_size 10K;
client_header_buffer_size 1k;
client_max_body_size 8m;
large_client_header_buffers 2 1k;

# 超时设置
client_body_timeout 12;
client_header_timeout 12;
send_timeout 10;

多站点配置建议

推荐使用以下目录结构组织多个站点:

/etc/nginx/
├── nginx.conf
├── conf.d/
│   ├── example.com.conf
│   └── another-site.conf
├── sites-available/
│   ├── example.com
│   └── another-site
└── sites-enabled/
    ├── example.com -> ../sites-available/example.com
    └── another-site -> ../sites-available/another-site

在nginx.conf中包含这些配置:

http {
    # ...
    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

以上是Nginx初始配置的基本方法,根据实际需求可以进一步调整和优化。