戳底部名片,一起变现
1. 基础命令
启动、停止和重启
-
启动 Nginx
sudo systemctl start nginx # 对于基于 Systemd 的系统 sudo service nginx start # 对于非 Systemd 系统 -
停止 Nginx
sudo systemctl stop nginx # 对于基于 Systemd 的系统 sudo service nginx stop # 对于非 Systemd 系统 -
重启 Nginx
sudo systemctl restart nginx # 对于基于 Systemd 的系统 sudo service nginx restart # 对于非 Systemd 系统 -
重新加载配置
当修改了配置文件后,可以使用此命令让 Nginx 重新加载而无需完全重启:
sudo systemctl reload nginx # 对于基于 Systemd 的系统 sudo service nginx reload # 对于非 Systemd 系统
检查配置语法
在重新加载或重启之前,最好先检查配置文件是否有语法错误:
sudo nginx -t
如果配置正确,你会看到类似于 syntax is ok 和 test is su***essful 的消息。
2. 配置文件结构
Nginx 的主配置文件通常位于 /etc/nginx/nginx.conf。此外,站点的具体配置一般存放在 /etc/nginx/sites-available/ 目录下,并通过符号链接激活(放置在 /etc/nginx/sites-enabled/ 中)。
全局设置
这部分定义了影响整个 Nginx 实例的行为:
user www-data; # 指定运行 Nginx 的用户
worker_processes auto; # 自动调整工作进程数
pid /run/nginx.pid; # PID 文件位置
事件块
这里配置了连接处理相关的参数:
events {
worker_connections 1024; # 每个工作进程的最大并发连接数
}
HTTP 块
这是最常用的配置部分,包含所有 HTTP 服务的设置:
http {
include /etc/nginx/mime.types; # 包含 MIME 类型定义
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"';
a***ess_log /var/log/nginx/a***ess.log main;
error_log /var/log/nginx/error.log;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
3. 服务器块(Server Block)
每个 server 块代表一个虚拟主机或网站配置:
server {
listen 80;
server_name example.*** www.example.***;
root /var/www/html;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
error_log /var/log/nginx/example.error.log;
a***ess_log /var/log/nginx/example.a***ess.log;
}
监听端口
指定服务器监听的 IP 地址和端口号。默认情况下,Nginx 监听所有可用接口上的 80 端口。
域名绑定
通过 server_name 指令将特定域名或多个域名绑定到该服务器块上。
根目录与索引文件
root 定义了网站内容所在的文件系统路径;index 则指定了默认的首页文件名。
位置块(Location Block)
用于定义如何处理来自客户端的不同 URL 请求:
location /images/ {
alias /var/www/images/;
}
location /admin/ {
auth_basic "Restricted Area";
auth_basic_user_file /etc/nginx/.htpasswd;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
- 静态资源映射:如上所示,可以为特定路径设置不同的根目录。
- 认证保护:使用基本认证限制访问敏感区域。
- 动态内容处理:例如通过 FastCGI 传递 PHP 请求给 PHP-FPM 处理。
4. 反向代理与负载均衡
Nginx 不仅可以用作静态文件服务器,还可以作为高效的反向代理来分发请求到后端应用服务器。下面是一个简单的反向代理配置示例:
upstream backend {
server 192.168.1.10:8080;
server 192.168.1.11:8080;
}
server {
listen 80;
server_name api.example.***;
location / {
proxy_pass http://backend;
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;
}
}
负载均衡策略
可以通过调整 upstream 块中的参数来实现不同类型的负载均衡算法:
upstream backend {
least_conn; # 最少连接数
ip_hash; # 根据客户端 IP 分配
hash $request_uri consistent; # 基于请求 URI 的一致性哈希
server 192.168.1.10:8080 weight=5 max_fails=2 fail_timeout=30s;
server 192.168.1.11:8080;
}
5. SSL/TLS 支持
为了提供安全连接,建议启用 HTTPS。以下是一个完整的 SSL 配置示例:
server {
listen 443 ssl;
server_name example.*** www.example.***;
ssl_certificate /etc/letsencrypt/live/example.***/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.***/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
location / {
proxy_pass http://backend;
...
}
}
自动证书管理
使用 Let’s Encrypt 和 Certbot 工具可以轻松获取并自动更新 SSL 证书:
sudo apt install certbot python3-certbot-nginx # 对于基于 Debian 的系统
# 或者
sudo yum install certbot python3-certbot-nginx # 对于基于 Red Hat 的系统
sudo certbot --nginx -d example.*** -d www.example.***
6. 日志与监控
定期检查 Nginx 的日志文件,以便及时发现潜在的问题。日志文件通常位于 /var/log/nginx/ 目录下,包括访问日志和错误日志。
自定义日志格式
你可以根据需要创建自定义的日志格式:
log_format custom '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" '
'rt=$request_time upstream_rt=$upstream_response_time';
a***ess_log /var/log/nginx/custom_a***ess.log custom;
集成第三方工具
为了更好地管理和分析日志数据,可以集成第三方工具如 ELK Stack(Elasticsearch, Logstash, Kibana)、Graylog 或者使用云服务提供商的日志管理解决方案。
7. 性能优化
根据你的应用场景和硬件条件,考虑对 Nginx 进行性能调优。这可能涉及到调整 worker 进程数、最大并发连接数、缓存策略等参数。请参考官方文档获取更多信息。
调整 worker 进程数
确保 worker 进程数与 CPU 核心数相匹配:
worker_processes auto;
启用 Gzip 压缩
减少传输的数据量,提高页面加载速度:
gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
缓存静态资源
利用浏览器缓存来减轻服务器负担:
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 30d;
add_header Cache-Control "public, no-transform";
}