Nginx从入门到精通配置指南

360影视 欧美动漫 2025-04-07 20:58 3

摘要:Nginx 是一款高性能的HTTP 和反向代理服务器,同时支持负载均衡、邮件代理及静态资源缓存等功能。凭借其高并发处理能力(单机可达数万并发连接)和低内存消耗,Nginx 成为现代 Web 架构的核心组件。

一、Nginx 简介

Nginx 是一款高性能的 HTTP 和反向代理服务器,同时支持 负载均衡邮件代理静态资源缓存 等功能。凭借其高并发处理能力(单机可达数万并发连接)和低内存消耗,Nginx 成为现代 Web 架构的核心组件。

二、安装 Nginx

1. Linux 系统安装

CentOS/RHEL

bash

# 添加 EPEL 仓库

sudo yum install epel-release

# 安装 Nginx

sudo yum install nginx

# 启动并设置开机自启

sudo systemctl start nginx

sudo systemctl enable nginx

Ubuntu/Debian

bash

sudo apt update

sudo apt install nginx

sudo systemctl start nginx

sudo systemctl enable nginx

2. Windows 安装

从 Nginx 官网 下载 Windows 版本,解压后运行 nginx.exe。

三、Nginx 核心配置

1. 配置文件结构

Nginx 配置文件默认位于 /etc/nginx/nginx.conf,主要分为以下块:

nginx

# 全局配置(影响所有层级)

user nginx;

worker_processes auto;

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

# 事件模块配置

events {

worker_connections 1024;

}

# HTTP 模块配置

http {

include /etc/nginx/mime.types;

default_type application/octet-stream;

# 定义日志格式

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;

# Server 块配置(虚拟主机)

server {

listen 80;

server_name example.com;

location / {

root /usr/share/nginx/html;

index index.html;

}

}

}

2. 常用命令

bash

# 检查配置语法

nginx -t

# 重新加载配置(不中断服务)

nginx -s reload

# 停止服务

nginx -s stop

四、核心应用场景

1. 静态资源托管

nginx

server {

listen 80;

server_name static.example.com;

location / {

root /data/static;

# 启用文件缓存

expires 30d;

}

# 防止访问隐藏文件(如 .git)

location ~ /\. {

deny all;

}

}

2. 反向代理

将请求转发到后端应用(如 Node.js、Java):

nginx

server {

listen 80;

server_name api.example.com;

location / {

proxy_pass http://localhost:3000;

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

}

}

3. 负载均衡

nginx

http {

upstream backend {

# 定义后端服务器集群

server 192.168.1.100:8080 weight=3; # 权重3

server 192.168.1.101:8080;

server 192.168.1.102:8080 backup; # 备用服务器

}

server {

listen 80;

server_name loadbalancer.example.com;

location / {

proxy_pass http://backend;

# 启用健康检查(需搭配第三方模块)

health_check;

}

}

}

4. HTTPS 配置

nginx

server {

listen 443 ssl;

server_name secure.example.com;

ssl_certificate /etc/ssl/certs/example.com.crt;

ssl_certificate_key /etc/ssl/private/example.com.key;

# 强制HTTP跳转HTTPS

if ($scheme != "https") {

return 301 https://$host$request_uri;

}

location / {

root /data/secure;

index index.html;

}

}

五、高级技巧

1. 缓存优化

nginx

proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m inactive=60m;

server {

location / {

proxy_cache my_cache;

proxy_cache_valid 200 304 12h;

proxy_cache_use_stale error timeout updating;

}

}

2. 动静分离

nginx

server {

location /static/ {

alias /data/static/;

expires max;

}

location / {

proxy_pass http://backend;

}

}

3. 安全加固

nginx

# 隐藏版本号

server_tokens off;

# 防盗链

location ~* \.(jpg|png|gif)$ {

valid_referers none blocked *.example.com;

if ($invalid_referer) {

return 403;

}

}

# 限制请求速率

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

location /api/ {

limit_req zone=one burst=20;

proxy_pass http://backend;

}

六、调试与日志分析

1. 错误日志定位

bash

tail -f /var/log/nginx/error.log

2. 常用调试指令

nginx

# 记录请求处理时间

add_header X-Request-Time $request_time;

# 调试变量值

add_header X-Debug-IP $remote_addr;

七、总结

通过以上配置和技巧,你可以:

部署高性能静态资源服务器构建灵活的反向代理和负载均衡集群实现安全加固和性能优化快速定位和解决线上问题

来源:老客数据一点号

相关推荐