Copyright 2025 HNCloud Limited.
香港联合通讯国际有限公司
Nginx服务器域名配置核心实践
为域名配置Nginx服务器是Web服务部署的基础操作,直接决定服务的可达性、性能与安全性。以下为关键配置步骤及技术要点,基于Nginx 1.18+版本我们在此为大家整理了Nginx域名配置核心实践分享。
1. 基础域名绑定
创建/etc/nginx/conf.d/example.com.conf配置文件:
nginx
server {
listen 80; 监听HTTP端口
server_name example.com www.example.com; 绑定主域名与子域名
root /var/www/example.com; 网站根目录
index index.html index.htm; 默认索引文件
location / {
try_files $uri $uri/ =404; 文件路径匹配规则
}
access_log /var/log/nginx/example.com.access.log; 访问日志路径
error_log /var/log/nginx/example.com.error.log; 错误日志路径
}
执行配置测试与重载:
nginx -t && systemctl reload nginx 验证配置并热加载
2. HTTPS强制加密
申请SSL证书后(以Let's Encrypt为例):
nginx
server {
listen 80;
server_name example.com www.example.com;
return 301 https://$host$request_uri; HTTP强制跳转HTTPS
}
server {
listen 443 ssl http2; 启用HTTP/2
server_name example.com www.example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; 证书路径
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; 私钥路径
ssl_protocols TLSv1.2 TLSv1.3; 安全协议版本
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256; 加密套件
ssl_prefer_server_ciphers on;
root /var/www/example.com;
index index.html;
安全响应头配置
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
add_header X-Content-Type-Options "nosniff" always;
}
3. 多域名服务路由
单文件配置多域名:
nginx
主站点配置
server {
listen 80;
server_name example.com;
root /sites/primary;
...
}
博客子目录分离
server {
listen 80;
server_name blog.example.com;
root /sites/blog; 独立内容目录
location /static/ {
expires 30d; 静态资源缓存
add_header Cache-Control "public";
}
}
API子域名反向代理
server {
listen 80;
server_name api.example.com;
location / {
proxy_pass http://127.0.0.1:8000; 转发到本机应用
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
连接超时控制
proxy_connect_timeout 5s;
proxy_read_timeout 30s;
}
}
4. 高级路由控制
nginx
#基于路径的路由
location ~ ^/shop/(.*)$ {
proxy_pass http://backend_app/$1; 路径截取转发
}
#文件类型限制
location /uploads/ {
location ~ \.php$ {
deny all; 禁止执行PHP文件
}
}
访问频率限制
limit_req_zone $binary_remote_addr zone=api_limit:10m rate=10r/s;
location /api/ {
limit_req zone=api_limit burst=20; 每秒10请求,允许20突发
proxy_pass http://api_backend;
}
地理访问控制
geo $blocked_country {
default 0;
CN 1; 示例:限制特定国家
}
server {
if ($blocked_country) {
return 403; 拒绝访问
}
}
5. 性能与安全优化
静态资源缓存
location ~* \.(jpg|css|js)$ {
expires 365d;
add_header Cache-Control "public, immutable";
}
客户端请求限制
client_max_body_size 10m; 文件上传大小
client_body_timeout 12s; 请求体超时
隐藏服务器信息
server_tokens off; 移除Nginx版本号
more_clear_headers Server; 清除Server头
防DDoS基础
limit_conn_zone $binary_remote_addr zone=conn_limit:10m;
limit_conn conn_limit 50; 单IP最大连接数
6. 调试与监控
nginx
实时状态监控
location /nginx_status {
stub_status on; 启用状态页
access_log off;
allow 192.168.1.0/24; 限制内网访问
deny all;
}
自定义错误页
error_page 404 /404.html;
location = /404.html {
root /etc/nginx/error_pages;
internal;
}
调试日志记录
location /debug {
access_log /var/log/nginx/debug.log main buffer=32k flush=1m;
set $debug_header $http_x_debug;
...
}
Nginx服务器的域名配置过程中,还有一些需要注意的细节。大家需要关注配置验证中,每次修改后执行
Nginx-t
验证语法,来确保我们的修改正常不会影响到正常服务。还有就是权限管理方面,需要保障Nginx用户(通常为www-data或nginx)对网站目录有读取权限。连接复用种,要启用
keepalive_timeout 75s;
保持TCP连接减少握手开销。
日志轮转需要配置logrotate防止日志文件无限增长。如果是灰度发布使用upstream模块实现蓝绿部署:
nginx
upstream backend {
server 10.0.0.1:8000 weight=90; 主版本
server 10.0.0.2:8000 weight=10; 新版本
}
以上就是为大家分享的一次完整配置Nginx服务器过程。要通过对版本控制如Git管理配置文件,采用include指令拆分模块化配置(如将SSL配置独立为ssl_params.conf)。生产环境应启用实时监控(Prometheus+Granafa)与自动化配置检查(Ansible)。遵循最小权限原则,避免过度开放路径访问,定期审计配置安全性。
上一篇:为什么说专线节点是企业级网络连接基础
下一篇:蓝光存储技术解析一文说清其原理和应用