欢迎光临文昌市中国灵车网
详情描述

一、Nginx配置文件结构

1. 主配置文件位置

/etc/nginx/nginx.conf  (Linux)
/usr/local/nginx/conf/nginx.conf  (源码安装)

2. 配置文件层级结构

# 全局块 - 设置影响nginx全局的指令
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# events块 - 配置影响nginx服务器与用户的网络连接
events {
    worker_connections 1024;
    use epoll;
}

# http块 - 可以嵌套多个server,配置代理、缓存、日志等绝大多数功能
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"';

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

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

    # 可以包含其他配置文件
    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

二、虚拟主机搭建过程

1. 基于域名的虚拟主机

步骤1:创建网站目录

sudo mkdir -p /var/www/example.com
sudo mkdir -p /var/www/test.com

步骤2:创建配置文件

# 创建虚拟主机配置文件
sudo nano /etc/nginx/conf.d/example.com.conf

配置文件内容:

server {
    # 监听端口
    listen 80;

    # 域名(可以有多个,用空格分隔)
    server_name example.com www.example.com;

    # 网站根目录
    root /var/www/example.com;

    # 默认索引文件
    index index.html index.htm;

    # 访问日志
    access_log /var/log/nginx/example.com_access.log;
    error_log /var/log/nginx/example.com_error.log;

    # 主要location配置
    location / {
        # 尝试按顺序查找文件
        try_files $uri $uri/ =404;
    }

    # 静态文件缓存
    location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
        expires 1y;
        add_header Cache-Control "public, immutable";
    }
}

2. 基于端口的虚拟主机

server {
    listen 8080;
    server_name localhost;
    root /var/www/port8080;
    index index.html;
}

server {
    listen 8081;
    server_name localhost;
    root /var/www/port8081;
    index index.html;
}

3. 基于IP的虚拟主机

server {
    listen 192.168.1.100:80;
    server_name _;
    root /var/www/ip-site;
    index index.html;
}

三、location匹配规则详解

# 1. 精确匹配(优先级最高)
location = /exact-path {
    # 只匹配 /exact-path
}

# 2. 前缀匹配
location /prefix {
    # 匹配以 /prefix 开头的所有路径
}

# 3. 正则表达式匹配(区分大小写)
location ~ \.php$ {
    # 匹配所有以 .php 结尾的请求
}

# 4. 正则表达式匹配(不区分大小写)
location ~* \.(jpg|jpeg|png)$ {
    # 匹配图片文件,不区分大小写
}

# 5. 优先前缀匹配
location ^~ /images/ {
    # 如果匹配,则停止继续搜索正则表达式
}

# 匹配优先级:= > ^~ > ~/~* > 普通前缀匹配

四、完整的虚拟主机配置示例

1. PHP网站配置

server {
    listen 80;
    server_name phpsite.com;
    root /var/www/phpsite;
    index index.php index.html;

    access_log /var/log/nginx/phpsite_access.log;
    error_log /var/log/nginx/phpsite_error.log;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

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

        # 安全设置
        fastcgi_param PHP_ADMIN_VALUE "open_basedir=$document_root:/tmp:/proc";
    }

    # 禁止访问敏感文件
    location ~ /\.(ht|git|svn) {
        deny all;
    }

    # 静态文件缓存
    location ~* \.(css|js|jpg|jpeg|png|gif|ico|woff|woff2|ttf|svg)$ {
        expires 30d;
        add_header Cache-Control "public, immutable";
    }
}

2. 反向代理配置

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

    location / {
        proxy_pass http://localhost:3000;

        # 重要:传递原始主机头
        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;

        # WebSocket支持
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";

        # 超时设置
        proxy_connect_timeout 60s;
        proxy_send_timeout 60s;
        proxy_read_timeout 60s;
    }
}

五、配置验证和管理命令

# 1. 测试配置文件语法
sudo nginx -t

# 2. 重新加载配置(不中断服务)
sudo nginx -s reload

# 3. 停止nginx
sudo nginx -s stop

# 4. 重启nginx
sudo nginx -s reopen

# 5. 查看nginx版本和配置信息
nginx -V

# 6. 检查配置文件所在位置
nginx -t 2>&1 | grep file

六、最佳实践建议

配置分离:每个虚拟主机使用独立配置文件 启用Gzip压缩:减少传输数据量 设置安全头:增加安全性 配置日志轮转:使用logrotate 限制客户端请求:防止滥用 启用HTTPS:使用Let's Encrypt免费证书 设置适当的缓存策略 配置错误页面:自定义404、50x页面

七、HTTPS配置示例

server {
    listen 443 ssl http2;
    server_name secure.example.com;

    ssl_certificate /etc/ssl/certs/example.com.crt;
    ssl_certificate_key /etc/ssl/private/example.com.key;

    # SSL优化配置
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers on;

    # HSTS
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;

    root /var/www/secure-site;
    # ... 其他配置
}

# HTTP重定向到HTTPS
server {
    listen 80;
    server_name secure.example.com;
    return 301 https://$server_name$request_uri;
}

通过以上配置,你可以根据实际需求搭建各种类型的虚拟主机,确保配置的正确性和安全性。