Nginx
一、使用系统自带的包管理器安装
1. Ubuntu / Debian 系统:
sudo apt update
sudo apt install nginx
- 默认安装路径 /etc/ningx/
- 启动服务:
sudo systemctl start nginx
sudo systemctl enable nginx
2. CentOS / RHEL / Rocky / AlmaLinux:
sudo yum install epel-release
sudo yum install nginx
后者使用dnf(CentOS8+/RHEL 8+):
sudo dnf install nginx
3. Arch Linux / Manjaro:
sudo pacman -S nginx
二、手动编译安装
适合需要:
- 添加自定义模块(如RTMP、PageSpeed)
- 精细优化参数
- 使用特定版本的Ngingx
1. 安装依赖
Ubuntu / Debian
sudo apt update
sudo apt install build-essential libpcre3 libpcre3-dev zlib1g-dev libssl-dev
CentOS / RHEL:
sudo yum groupinstall "Development Tools"
sudo yum install pcre pcre-devel zlib zlib-devel openssl openssl-devel
2. 下载源码
wget http://nginx.org/download/nginx-1.24.0.tar.gz
tar -zxvf nginx-1.24.0.tar.gz
cd nginx-1.24.0
3. 编译配置(可根据需要启用模块)
./configure --prefix=/opt/nginx --with-http_ssl_module --with-http_v2_module --with-stream --with-http_gzip_static_module
根据需要添加更多模块,例如:
基本模块
--with-http_ssl_module支持HTTPS(TLS/SSL)协议,反向代理或提供HTTPS服务时必须启用。--with-http_v2_module支持HTTP/2协议,提升多并发、减少延迟,推荐开启。需要同时启用http_ssl_module。--with-http_gzip_static_module支持直接发送.gz静态压缩文件(如style.css.gz),无需实施压缩,减轻服务器负担。--with-http_stub_status_module启用Nginx状态页,便于监控连接数、活跃连接等,常用于location / status监控。--with-http_realip_module允许通过代理头部(如`X-Forwarded-For)获取真实IP,常用于Cloudflare/CDN后面.--with-threads启用多线程处理请求,可提升性能,特别是文件传输.--with-file-aio启用异步文件I/O,提高高并发下的性能,适用于大文件或高负载场景.
时间模块(并发相关)
--with-select_module启用select事件驱动(兼容性强,但效率低).默认启用,不推荐特意配置--with-poll_module启用poll事件驱动,比select好--with-epoll_moduleLinux高性能并发机制(推荐Linux使用),默认启用
流(TCP/UDP)模块
--with-stream启用TCP/UDP的四层代理(代理Mysql、Redis、Socket服务),非HTTP协议也能代理--with-stream_ssl_module支持为TCP/UDP代理配置SSL(如Mysql over TLS)--with-stream_realip_module获取流式代理中的真实客户端IP(与http_realip类似).--with-stream_ssl_preread_module实现SNI提前读取功能,可用于TCP SSL多站点分流(比如通过域名将请求路由到不同后端)
邮件模块
--with-email启用邮件代理(SMTP、IMAP、POP3),可用于反向代理邮件服务器--with-mail_ssl_module为邮件代理启用SSL/TLS支持
调试与开发相关模块
--with-debug启用调试日志模式,开发或排错时使用--with-google_preftools_module启用google的性能分析工具(如tcmalloc),用于优化性能。--with-cpp_test_module测试用模块,开发人员使用,几乎不用
4. 编译并安装
sudo make install
安装完成后:
- 主程序位于
/opt/nginx/sbin/nginx - 配置文件在
/opt/nginx/conf/nginx.conf
5. 启动nginx
/opt/nginx/sbin/nginx
默认配置文件路径为: /opt/nginx/conf/nginx.conf
三、配置systemd启动服务
创建systemd服务文件
sudo vim /etc/systemd/system/nginx.service
粘贴以下内容:
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=network.target
[Service]
Type=forking
ExecStart=/opt/nginx/sbin/nginx
ExecReload=/opt/nginx/sbin/nginx -s reload
ExecStop=/opt/nginx/sbin/nginx -s quit
PIDFile=/opt/nginx/logs/nginx.pid
PrivateTmp=true
[Install]
WantedBy=multi-user.target
保存并退出
2. 重新加载systemd并启用服务
sudo systemctl daemon-reexec
sudo systemctl daemon-reload
sudo systemctl enable nginx
sudo systemctl start nginx
检查状态
sudo systemctl status nginx
如果启动成功,会看到Active: active(running).
配置脚本
server {
listen 80;
server_name localhost;
charset utf-8;
client_max_body_size 128M;
index index.php index.html;
access_log /mnt/d/Demo/test/kc_api/api/runtime/logs/access.log;
error_log /mnt/d/Demo/test/kc_api/api/runtime/logs/error.log debug;
# 后端 PHP
location ~ \.php$ {
root /mnt/d/Demo/test/kc_api/api/web;
include fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
try_files $uri =404;
}
# 后端其他 API
location / {
root /mnt/d/Demo/test/kc_api/api/web;
index index.php;
# 尝试是否是静态文件
# 不存在就交给 /index.php 处理(如 Laravel、Yii 等)
try_files $uri $uri/ /index.php$is_args$args;
}
#location /pages/ {
# alias /mnt/d/Demo/test/dist/;
#}
# 开头固定以pages,([^/]+)捕获第一个路径段(项目名)$1,(/.*)?可选的后缀路径$2,$为结尾
location ~ ^/pages/([^/]+)(/.*)?$ {
alias /mnt/d/demo/test/dist/$1$2;
index index.html;
# $uri:尝试是否存在 /当前请求路径
# $uri/: 尝试是否是目录
# /index.php 如果前面都不存在,返回首页(适用于前端路由)
# 注意: 如果vue使用的不是history则不需要添加内容,如果使用的是alias则需要使用绝对路径
try_files $uri $uri/ /index.html;
}
# 禁止访问隐藏文件
location ~* /\. {
deny all;
}
}
四、生成免费证书
通过 python3-certbot-nginx 或 python3-certbot-apache 工具,就能完成证书申请并自动配置好 Nginx/Apache。
- 安装Cerbot
sudo apt update
sudo apt install certbot python3-certbot-nginx -y
- 确保nginx的域名
sudo vim /etc/nginx/sites-available/yourdomain.conf
note
⚠️注意:yourdomain 必须是你在 Cloudflare 里配置的域名,否则证书申请会失败。
- 申请证书
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
- --nginx 让 certbot 自动检测并修改你的 Nginx 配置
- -d 后面加域名,可以同时写多个(必须在 DNS 上正确解析到服务器 IP)
执行过程中会问:
- 邮箱(接收过期提醒)
- 是否同意条款(Y)
- 是否重定向 HTTP → HTTPS(选 2 Yes,自动 301 跳转)
- 完成后,Certbot 会自动在 /etc/nginx/sites-available/yourdomain.conf 添加类似配置:
server {
listen 443 ssl;
server_name yourdomain.com www.yourdomain.com;
ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;
include snippets/ssl-params.conf;
}
- fullchain.pem:包含 服务器证书 + 中间证书,用于让浏览器验证你的网站身份
- privkey.pem:私钥,用来配合证书加密数据
note
⚠️注意:如果你有多个域名,需要为每个域名申请证书,并分别配置到 Nginx 里。
👉没有这两行,Nginx 就无法启用 HTTPS。
- 验证配置并重启
sudo nginx -t
sudo systemctl reload nginx
- 自动续期
sudo certbot renew --dry-run
每天会自动检查证书是否过期,如果过期会自动续期。
- SSL/TlS协议版本
ssl_protocols TLSv1.2 TLSv1.3;
- TLSv1.2 是最常用的协议,但安全性较差,建议升级到 TLSv1.3
- TLSv1.3 是最新的协议,安全性最高,但兼容性较差,建议先升级到 TLSv1.2
- TLSv1.1 已被废弃,不建议使用
- SSL/TlS加密套件
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384';
ssl_prefer_server_ciphers on;
- HIGH 表示使用高强度的加密算法
- !aNULL 表示不使用匿名加密算法
- !MD5 表示不使用 MD5 哈希算法
- 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384' 表示优先使用 ECDHE 算法,然后是 RSA 算法,最后是 AES 算法
- ssl_prefer_server_ciphers on 表示优先使用服务器推荐的加密套件
如果启用 TLS 1.3,一般 Nginx 会使用默认套件,无需单独配置。
warning
安全性:避免已知弱算法(如 RC4、DES、3DES、MD5)
支持前向保密(PFS):推荐使用 ECDHE
性能:AES-GCM 或 CHACHA20-POLY1305 性能最好
- 其他安全优化
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 1h;
ssl_stapling on;
ssl_stapling_verify on;
resolver 8.8.8.8 8.8.4.4 valid=300s;
resolver_timeout 5s;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
add_header X-Frame-Options SAMEORIGIN;
add_header X-Content-Type-Options nosniff;
ssl_session_cache shared:SSL:10m; 和ssl_session_timeout 1h; 用于缓存 SSL 会话,提升性能ssl_stapling on; 和ssl_stapling_verify on;用于启用 OCSP Stapling,提升安全性resolver 8.8.8.8 8.8.4.4 valid=300s;和resolver_timeout 5s;用于配置 DNS 解析器,提升解析速度add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;用于启用 HSTS,提升安全性add_header X-Frame-Options SAMEORIGIN;用于防止点击劫持攻击add_header X-Content-Type-Options nosniff;用于防止 XSS 攻击
这些配置可以根据实际情况进行调整,但建议尽量启用。