# 更新软件包列表
sudo apt update
# 安装Nginx
sudo apt install nginx
# 启动Nginx服务
sudo systemctl start nginx
# 设置开机自启
sudo systemctl enable nginx
# 添加EPEL仓库(CentOS/RHEL 7及以下)
sudo yum install epel-release
# 安装Nginx
sudo yum install nginx
# 启动Nginx服务
sudo systemctl start nginx
# 设置开机自启
sudo systemctl enable nginx
# 安装依赖
sudo apt install build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev
# 下载源码(以1.25.3版本为例)
wget https://nginx.org/download/nginx-1.25.3.tar.gz
tar -zxvf nginx-1.25.3.tar.gz
cd nginx-1.25.3
# 配置编译选项
./configure --prefix=/usr/local/nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module
# 编译并安装
make && sudo make install
# 启动Nginx
/usr/local/nginx/sbin/nginx
# 启动Nginx
sudo systemctl start nginx
# 停止Nginx
sudo systemctl stop nginx
# 重启Nginx
sudo systemctl restart nginx
# 重新加载配置(不中断服务)
sudo systemctl reload nginx
# 查看状态
sudo systemctl status nginx
Nginx主要配置文件通常位于:
- /etc/nginx/nginx.conf
(包管理器安装)
- /usr/local/nginx/conf/nginx.conf
(源码安装)
典型配置目录结构:
/etc/nginx/
├── nginx.conf # 主配置文件
├── conf.d/ # 额外配置文件目录
├── sites-available/ # 可用站点配置
├── sites-enabled/ # 已启用站点配置(通常是符号链接)
├── modules-available/ # 可用模块
└── modules-enabled/ # 已启用模块
# /etc/nginx/sites-available/example.com
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com/html;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
# 静态文件缓存
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 30d;
add_header Cache-Control "public, no-transform";
}
}
启用站点配置:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo nginx -t # 测试配置语法
sudo systemctl reload nginx
# 安装Certbot(Let's Encrypt客户端)
sudo apt install certbot python3-certbot-nginx
# 获取并安装证书
sudo certbot --nginx -d example.com -d www.example.com
# 设置自动续期
sudo certbot renew --dry-run
upstream backend {
server backend1.example.com weight=5;
server backend2.example.com;
server backend3.example.com;
# 可选: 负载均衡方法
# least_conn; # 最少连接
# ip_hash; # IP哈希
}
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
server {
listen 80;
server_name app.example.com;
location / {
proxy_pass http://localhost:3000; # 代理到本地3000端口
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
Nginx日志通常位于:
- 访问日志: /var/log/nginx/access.log
- 错误日志: /var/log/nginx/error.log
自定义日志格式:
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
}
worker_processes auto; # 自动设置为CPU核心数
worker_connections 1024; # 每个工作进程的最大连接数
gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
gzip_min_length 1024;
gzip_proxied any;
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m inactive=60m use_temp_path=off;
server {
location / {
proxy_cache my_cache;
proxy_cache_valid 200 302 10m;
proxy_cache_valid 404 1m;
}
}
sudo nginx -t
sudo systemctl status nginx
ps aux | grep nginx
sudo netstat -tulnp | grep nginx
# 或
sudo ss -tulnp | grep nginx
sudo tail -f /var/log/nginx/error.log
- 检查目录权限 - 确保index文件存在 - 检查SELinux状态(如启用)
通过以上步骤,您应该能够在Linux环境下成功安装、配置和使用Nginx作为Web服务器或反向代理服务器。