目录
一、Nginx 服务器配置
Nginx 是一个高性能的 HTTP 服务器,因其处理高并发连接的能力而广受欢迎。Nginx 是以事件驱动的方式处理请求,内存消耗低,适合于高流量的场景。
1. 安装 Nginx
-
在 Ubuntu/Debian 系统上:
sudo apt update sudo apt install nginx
-
在 CentOS 系统上:
sudo yum install nginx
-
启动 Nginx 服务:
sudo systemctl start nginx sudo systemctl enable nginx
2. Nginx 配置文件结构
Nginx 的配置文件主要存放在 /etc/nginx/nginx.conf
,并通过 include
指令引用其他配置文件。
-
nginx.conf
:主配置文件,定义了全局设置、事件处理、HTTP 模块等。 -
sites-available/
和sites-enabled/
:通常用于存放虚拟主机配置文件。sites-available/
存放可用的配置文件,sites-enabled/
存放启用的配置文件。
3. 配置虚拟主机
在 /etc/nginx/sites-available/
目录中创建一个配置文件,例如 example.com
,然后在 /etc/nginx/sites-enabled/
中创建符号链接。
-
创建配置文件:
sudo nano /etc/nginx/sites-available/example.com
配置示例:
server { listen 80; server_name example.com www.example.com; root /var/www/example.com; index index.html index.htm; location / { try_files $uri $uri/ =404; } }
-
启用配置文件:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
-
测试配置并重载 Nginx:
sudo nginx -t sudo systemctl reload nginx
4. SSL 配置(启用 HTTPS)
要启用 SSL,需要安装 SSL 证书并进行相应配置。假设你已经有了 SSL 证书文件(example.com.crt
和 example.com.key
)。
-
修改 Nginx 配置文件:
server { listen 80; server_name example.com www.example.com; return 301 https://$server_name$request_uri; } server { listen 443 ssl; server_name example.com www.example.com; ssl_certificate /path/to/example.com.crt; ssl_certificate_key /path/to/example.com.key; root /var/www/example.com; index index.html index.htm; location / { try_files $uri $uri/ =404; } }
-
重载 Nginx:
sudo systemctl reload nginx
5. 配置反向代理
Nginx 常被用作反向代理服务器,转发客户端请求到后端服务器(例如:Node.js、PHP-FPM)。
-
反向代理配置示例:
server { listen 80; server_name example.com; location / { proxy_pass http://127.0.0.1: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; } }
二、Apache 服务器配置
Apache 是功能强大的 Web 服务器,长期以来广泛应用于企业和个人网站。Apache 以其丰富的模块支持和灵活的配置方式,广泛用于 PHP、Python 等应用环境。
1. 安装 Apache
-
在 Ubuntu/Debian 系统上:
sudo apt update sudo apt install apache2
-
在 CentOS 系统上:
sudo yum install httpd
-
启动 Apache 服务:
sudo systemctl start apache2 # Ubuntu/Debian sudo systemctl start httpd # CentOS sudo systemctl enable apache2 # Ubuntu/Debian sudo systemctl enable httpd # CentOS
2. Apache 配置文件结构
-
httpd.conf
:主配置文件,通常包含全局配置和其他模块的加载。 -
sites-available/
和sites-enabled/
:Apache 也使用这两个目录来管理虚拟主机配置。
3. 配置虚拟主机
在 Apache 中配置虚拟主机需要编辑 /etc/apache2/sites-available/
目录中的配置文件,并启用该配置。
-
创建虚拟主机配置文件:
sudo nano /etc/apache2/sites-available/example.com.conf
配置示例:
<VirtualHost *:80> ServerAdmin webmaster@example.com ServerName example.com DocumentRoot /var/www/example.com ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>
-
启用配置并重载 Apache:
sudo a2ensite example.com.conf sudo systemctl reload apache2
4. SSL 配置(启用 HTTPS)
配置 SSL 证书的过程与 Nginx 类似。
-
创建 SSL 配置文件:
sudo nano /etc/apache2/sites-available/example.com-le-ssl.conf
配置示例:
<VirtualHost *:80> ServerName example.com Redirect permanent / https://example.com/ </VirtualHost> <VirtualHost *:443> ServerAdmin webmaster@example.com ServerName example.com DocumentRoot /var/www/example.com SSLEngine on SSLCertificateFile /path/to/example.com.crt SSLCertificateKeyFile /path/to/example.com.key ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>
-
启用 SSL 模块并重启 Apache:
sudo a2enmod ssl sudo a2ensite example.com-le-ssl.conf sudo systemctl reload apache2
5. 配置反向代理
Apache 可以通过 mod_proxy
模块配置反向代理。
-
反向代理配置示例:
<VirtualHost *:80> ServerName example.com ProxyPass / http://127.0.0.1:3000/ ProxyPassReverse / http://127.0.0.1:3000/ </VirtualHost>
-
启用
mod_proxy
模块并重启 Apache:sudo a2enmod proxy sudo a2enmod proxy_http sudo systemctl reload apache2
三、Nginx 与 Apache 的对比
特性 | Nginx | Apache |
---|---|---|
架构 | 事件驱动,非阻塞 | 进程/线程驱动,阻塞模式 |
性能 | 高效处理高并发,内存占用少,适合高流量场景 | 在高并发情况下性能较差,适合小型/中型站点 |
配置简便性 | 配置简洁、易于管理 | 配置灵活、模块化,适合复杂环境 |
支持的协议 | HTTP、HTTPS、SMTP、POP3 等 | HTTP、HTTPS、FTP、SMTP 等 |
反向代理支持 | 原生支持,性能优越 | 需要通过模块支持(mod_proxy ) |
负载均衡 | 原生支持负载均衡 | 需要使用 mod_proxy_balancer 等模块 |
四、总结
-
Nginx
:以高性能、高并发为特点,适合大流量、高负载的网站。配置简洁,尤其适用于静态资源的处理和反向代理。适合初学者和需要优化的网站。
-
Apache:功能强大、灵活,适合需要高度定制化、复杂配置的应用。适合有丰富模块需求和较为复杂的 Web 环境。
根据网站的需求,选择适合的 Web 服务器将有助于提高性能、降低成本并优化用户体验。