插件窝 干货文章 nginx多网站怎么配置

nginx多网站怎么配置

example server com index 793    来源:    2025-04-27

Nginx多网站配置指南

Nginx可以通过虚拟主机(server blocks)来配置多个网站。以下是详细的配置方法:

基础配置方法

1. 基于域名的虚拟主机(最常用)

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

    root /var/www/example.com;
    index index.html index.php;

    location / {
        try_files $uri $uri/ =404;
    }
}

server {
    listen 80;
    server_name example2.com www.example2.com;

    root /var/www/example2.com;
    index index.html index.php;

    location / {
        try_files $uri $uri/ =404;
    }
}

2. 基于端口的虚拟主机

server {
    listen 8080;
    server_name example.com;

    root /var/www/example.com;
    index index.html;
}

server {
    listen 8081;
    server_name example2.com;

    root /var/www/example2.com;
    index index.html;
}

3. 基于IP地址的虚拟主机

server {
    listen 192.168.1.10:80;
    server_name example.com;

    root /var/www/example.com;
    index index.html;
}

server {
    listen 192.168.1.11:80;
    server_name example2.com;

    root /var/www/example2.com;
    index index.html;
}

推荐的最佳实践

1. 使用独立的配置文件

  1. /etc/nginx/conf.d//etc/nginx/sites-available/目录下为每个网站创建单独的配置文件
  2. /etc/nginx/sites-enabled/中创建符号链接
# 创建配置文件
sudo nano /etc/nginx/sites-available/example.com.conf

# 创建符号链接
sudo ln -s /etc/nginx/sites-available/example.com.conf /etc/nginx/sites-enabled/

2. 标准配置文件示例

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

    root /var/www/example.com/public;
    index index.php index.html index.htm;

    access_log /var/log/nginx/example.com.access.log;
    error_log /var/log/nginx/example.com.error.log;

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

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php8.1-fpm.sock;
    }

    location ~ /\.ht {
        deny all;
    }
}

3. SSL配置(推荐所有网站使用HTTPS)

server {
    listen 80;
    server_name example.com www.example.com;
    return 301 https://$server_name$request_uri;
}

server {
    listen 443 ssl http2;
    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;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    root /var/www/example.com/public;
    index index.php index.html index.htm;

    # 其他配置...
}

常见问题解决

  1. 配置不生效

    • 检查nginx配置语法:sudo nginx -t
    • 重新加载配置:sudo systemctl reload nginx
    • 确保域名已正确解析到服务器IP
  2. 权限问题

    • 确保Nginx用户(通常是www-data或nginx)有权限访问网站目录
    • sudo chown -R www-data:www-data /var/www/example.com
    • sudo chmod -R 755 /var/www/example.com
  3. PHP不执行

    • 检查PHP-FPM是否运行
    • 确保fastcgi_pass指向正确的PHP-FPM socket文件
  4. 重定向循环

    • 检查SSL配置是否正确
    • 确保没有多余的return或rewrite规则

高级配置技巧

  1. 通配符子域名
server {
    listen 80;
    server_name *.example.com;

    set $subdomain "";
    if ($host ~* ^([a-z0-9-]+)\.example\.com$) {
        set $subdomain $1;
    }

    root /var/www/example.com/$subdomain;
}
  1. 负载均衡
upstream backend {
    server backend1.example.com;
    server backend2.example.com;
}

server {
    location / {
        proxy_pass http://backend;
    }
}
  1. 缓存静态文件
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
    expires 30d;
    add_header Cache-Control "public, no-transform";
}

配置完成后,记得测试并重新加载Nginx:

sudo nginx -t
sudo systemctl reload nginx