首页 / 日本服务器 / 正文
Nginx 常用配置详解,nginx常用配置参数

Time:2025年01月07日 Read:7 评论:42 作者:y21dr45

Nginx 是一款高性能的 HTTP 和反向代理服务器,被广泛应用于负载均衡、静态文件服务以及作为邮件代理服务器等,本文将详细介绍 Nginx 的一些常用配置,帮助大家更好地理解和使用它。

Nginx 常用配置详解,nginx常用配置参数

一、基本配置

1.全局块配置

全局块主要用于配置影响 Nginx 服务器整体运行的指令,通常放在配置文件的最顶部。

user  nginx;
worker_processes  auto;
error_log  /var/log/nginx/error.log warn;
pid        /run/nginx.pid;

user: 指定 Nginx 进程要以哪个用户身份运行,通常是nobody 用户。

worker_processes: 设置 Nginx 使用的工作进程数,通常设置为自动(auto)或 CPU 核数。

error_log: 定义错误日志的存储位置和日志级别。

pid: 指定存放主进程 ID 的文件路径。

2.events 块配置

events 块涉及的指令主要影响 Nginx 服务器与用户的网络连接。

events {
    worker_connections  1024;
}

worker_connections: 定义每个工作进程的最大连接数。

3.http 块配置

http 块包含多个指令,用于优化服务器性能。

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    
    access_log  /var/log/nginx/access.log  main;
    
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;
    
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
    
    include /etc/nginx/conf.d/*.conf;
}

log_format: 定义访问日志的格式。

access_log: 设置访问日志的存储位置。

sendfile: 开启高效传输文件模式。

tcp_nopushtcp_nodelay: 用于提升网络性能。

keepalive_timeout: 设置客户端连接保持活动的超时时间。

types_hash_max_size: 设置存储 MIME 类型的哈希表大小。

include: 引入其他配置文件。

二、server 块配置

server 块用于定义一个虚拟主机,可以包含多个 location 块。

server {
    listen       80;
    server_name  example.com www.example.com;
    
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
    
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

listen: 指定监听的端口号。

server_name: 定义服务器名称或域名。

location: 匹配请求的 URL 路径,并进行相应处理。

error_page: 定义错误页面。

三、location 块配置

location 块用于匹配请求的 URL 路径,并进行相应的处理。

location / {
    root   /usr/share/nginx/html;
    index  index.html index.htm;
}

root: 设置网站根目录。

index: 设置默认访问文件。

四、负载均衡配置

Nginx 支持多种负载均衡策略,常见的是轮询和加权轮询。

upstream node_js {
    server 127.0.0.1:8081;
    server 127.0.0.1:8082;
    server 127.0.0.1:8083;
}
server {
    listen       80;
    server_name  yourdomain.com;
    
    location / {
        proxy_pass http://node_js;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

upstream: 定义一组上游服务器。

proxy_pass: 将请求转发到上游服务器组。

proxy_set_header: 设置请求头信息。

五、SSL/TLS 配置

启用 SSL/TLS 来加密数据传输。

server {
    listen 443 ssl;
    server_name secure.example.com;
    
    ssl_certificate /etc/nginx/ssl/your_ssl.crt;
    ssl_certificate_key /etc/nginx/ssl/your_ssl.key;
    
    location / {
        root   /var/www/secure;
        index  index.html;
    }
}

ssl_certificate: 指定 SSL 证书文件路径。

ssl_certificate_key: 指定 SSL 证书密钥文件路径。

六、缓存配置

配置缓存以减少上游服务器的压力。

http {
    ...
    proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m use_temp_path=off;
    
    server {
        listen 80;
        server_name yourapp.com;
        
        location / {
            proxy_pass http://upstream_server;
            proxy_cache my_cache;
            proxy_cache_valid 200 5m;
        }
    }
}

proxy_cache_path: 设置缓存路径和参数。

proxy_cache: 启用缓存。

proxy_cache_valid: 设置缓存有效期。

通过以上配置示例,可以看出 Nginx 的配置非常灵活,可以满足各种需求,合理的配置能够显著提升服务器的性能和安全性,希望本文对大家有所帮助!

标签: nginx常用配置 
排行榜
关于我们
「好主机」服务器测评网专注于为用户提供专业、真实的服务器评测与高性价比推荐。我们通过硬核性能测试、稳定性追踪及用户真实评价,帮助企业和个人用户快速找到最适合的服务器解决方案。无论是云服务器、物理服务器还是企业级服务器,好主机都是您值得信赖的选购指南!
快捷菜单1
服务器测评
VPS测评
VPS测评
服务器资讯
服务器资讯
扫码关注
鲁ICP备2022041413号-1