开发小技巧之Nginx配置二级域名

一、为什么需要配置二级域名?

在Web开发中,二级域名(如api.example.comblog.example.com)是常见的需求。其核心价值体现在:

  1. 功能隔离:通过不同子域名区分前后端服务(如api.example.com负责API,www.example.com负责前端)
  2. 安全增强:可为不同子域名配置独立SSL证书,实现更细粒度的HTTPS控制
  3. 负载优化:将静态资源(static.example.com)与动态内容分离,提升服务器性能
  4. 品牌统一:保持主域名一致性,同时支持多业务线独立运营

以电商网站为例,配置pay.example.com处理支付,user.example.com管理账户,可显著降低系统耦合度。

二、配置前的必要准备

1. DNS解析设置

需在域名服务商处添加CNAME记录:

  1. 子域名 记录类型
  2. api CNAME 服务器IP或主域名

关键点

  • 等待DNS生效(通常5-30分钟)
  • 避免循环CNAME(如A指向B,B又指向A)
  • 推荐使用TTL 300秒加速更新

2. 服务器环境要求

  • Nginx版本≥1.12(支持HTTP/2)
  • 防火墙开放80/443端口
  • 足够的系统资源(建议2核4G起)

三、Nginx配置实战

1. 基础配置模板

  1. server {
  2. listen 80;
  3. server_name api.example.com; # 关键配置项
  4. location / {
  5. proxy_pass http://localhost:3000; # 后端服务地址
  6. proxy_set_header Host $host;
  7. proxy_set_header X-Real-IP $remote_addr;
  8. }
  9. }

参数详解

  • server_name:必须精确匹配子域名
  • proxy_pass:建议使用内网IP避免DNS解析延迟
  • proxy_set_header:确保后端获取真实客户端信息

2. HTTPS配置方案

方案一:独立证书

  1. server {
  2. listen 443 ssl;
  3. server_name api.example.com;
  4. ssl_certificate /path/to/api.example.com.crt;
  5. ssl_certificate_key /path/to/api.example.com.key;
  6. # 安全增强配置
  7. ssl_protocols TLSv1.2 TLSv1.3;
  8. ssl_ciphers HIGH:!aNULL:!MD5;
  9. ...
  10. }

方案二:通配符证书(推荐)

  1. server {
  2. listen 443 ssl;
  3. server_name *.example.com;
  4. ssl_certificate /path/to/wildcard.crt;
  5. ssl_certificate_key /path/to/wildcard.key;
  6. ...
  7. }

选择建议

  • 5个子域名以下:独立证书
  • 5+子域名:通配符证书(年费约$150-$500)

3. 高级配置技巧

3.1 负载均衡

  1. upstream api_servers {
  2. server 10.0.0.1:3000 weight=3;
  3. server 10.0.0.2:3000;
  4. server 10.0.0.3:3000 backup;
  5. }
  6. server {
  7. ...
  8. location / {
  9. proxy_pass http://api_servers;
  10. }
  11. }

3.2 静态资源优化

  1. server {
  2. server_name static.example.com;
  3. location / {
  4. root /var/www/static;
  5. expires 1y;
  6. add_header Cache-Control "public";
  7. }
  8. }

3.3 跨域支持

  1. server {
  2. ...
  3. location / {
  4. add_header 'Access-Control-Allow-Origin' '*';
  5. add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
  6. }
  7. }

四、常见问题解决方案

1. 配置不生效

排查步骤

  1. 执行nginx -t测试配置
  2. 检查nginx.conf是否包含include /etc/nginx/conf.d/*.conf;
  3. 查看错误日志:tail -f /var/log/nginx/error.log

2. 502 Bad Gateway

常见原因

  • 后端服务未启动
  • 防火墙阻止连接
  • 代理超时设置过短

解决方案

  1. location / {
  2. proxy_connect_timeout 60s;
  3. proxy_read_timeout 60s;
  4. proxy_send_timeout 60s;
  5. ...
  6. }

3. HTTPS证书警告

检查项

  • 证书链是否完整(需包含中间证书)
  • 证书有效期
  • 域名是否与证书匹配

五、性能优化建议

  1. 启用HTTP/2
    1. server {
    2. listen 443 ssl http2;
    3. ...
    4. }
  2. 启用Gzip压缩
    1. gzip on;
    2. gzip_types text/plain application/json;
    3. gzip_min_length 1k;
  3. 连接池优化
    1. upstream api_servers {
    2. keepalive 32;
    3. server 10.0.0.1:3000;
    4. }

六、自动化配置方案

推荐使用Ansible实现批量配置:

  1. - name: Configure Nginx subdomain
  2. hosts: web_servers
  3. tasks:
  4. - name: Create config file
  5. template:
  6. src: subdomain.conf.j2
  7. dest: /etc/nginx/conf.d/{{ subdomain }}.conf
  8. notify:
  9. - Reload Nginx

七、安全加固措施

  1. 限制访问IP
    1. server {
    2. allow 192.168.1.0/24;
    3. deny all;
    4. ...
    5. }
  2. 禁用危险方法
    1. if ($request_method !~ ^(GET|HEAD|POST)$ ) {
    2. return 444;
    3. }
  3. 定期更新Nginx
    1. # Ubuntu系统
    2. sudo apt update && sudo apt upgrade nginx

通过以上配置,开发者可以高效实现二级域名管理,同时确保系统安全性和性能。实际部署时建议先在测试环境验证,再逐步推广到生产环境。