Nginx 面试高频问题
什么是 Nginx?
Nginx 是一个高性能的:
- Web服务器
- 反向代理服务器
- 负载均衡服务器
特点:
- 高并发
- 低内存占用
- 高性能
- 支持反向代理
正向代理和反向代理
正向代理
客户端主动访问代理服务器。
客户端
↓
代理服务器
↓
目标网站
例如:
- VPN
- 科学上网
反向代理
客户端不知道真实服务器。
用户
↓
Nginx
↓
应用服务器
应用场景:
- 负载均衡
- 隐藏真实IP
- SSL终止
Nginx 支持哪些负载均衡策略?
负载均衡(Load Balance)是指:将客户端的请求合理地分配到多台服务器上,从而避免单台服务器压力过大,提高系统性能和可用性。
轮询(默认)
upstream backend {
server 192.168.1.1;
server 192.168.1.2;
}
Weight
权重分配
server 192.168.1.1 weight=5;
server 192.168.1.2 weight=1;
IP Hash
ip_hash;
同一IP访问同一台服务器。
Least Connections
least_conn;
连接数最少优先。
什么是 Master 和 Worker?
Master
负责:
- 读取配置
- 管理Worker
Worker
负责:
- 接收请求
- 返回响应
流程:
Master
↓
Worker
↓
处理请求
Worker Process 设置多少合适?
通常:
worker_processes auto;
或者:
CPU核心数
例如:
8核CPU
worker_processes 8
FastCGI、PHP-FPM、Nginx
Nginx 本身不能执行 PHP,它只能接收 HTTP 请求。
当访问 PHP 文件时,Nginx 会通过 FastCGI 协议将请求转发给 PHP-FPM。
FastCGI 是一种通信协议,负责 Nginx 与 PHP 程序之间的数据传输。
PHP-FPM 是 PHP 的 FastCGI 进程管理器,负责维护 PHP Worker 进程池并执行 PHP 脚本。
整个请求链路为:
浏览器→ Nginx→ FastCGI→ PHP-FPM→ PHP→ MySQL→ PHP-FPM→ Nginx→ 浏览器
通过 FastCGI 和 PHP-FPM 的进程池机制,避免了 CGI 模式频繁创建和销毁进程的问题,大幅提高了 PHP 的并发处理能力。
location
location基本语法:
location [ = | ~ | ~* | ^~ ] uri {
...
}
按照优先级排序:
# 精准匹配,完全相等
location = uri {
...
}
# 前缀匹配,匹配后停止正则搜索
location ^~ uri {
...
}
# 正则匹配,区分大小写
location ~ \.php$ {
...
}
# 正则匹配,不区分大小写
location ~* \.(gif|jpg|jpeg)$ {
...
}
# 普通前缀匹配,按最长前缀匹配原则
location uri {
...
}
root 和 alias 区别?
root:
root /data;
访问:
/img/a.jpg
↓
/data/img/a.jpg
alias:
alias /data/images;
访问:
/img/a.jpg
↓
/data/images/a.jpg
try_files 的作用?
try_files基本语法
try_files file ... uri;
try_files file ... =code;
按指定顺序检查文件是否存在,并使用找到的第一个文件进行请求处理。可以理解为:
if (文件存在($uri)) {
返回该文件;
}
else if (目录存在($uri/)) {
返回该目录;
}
else {
内部重定向到 /index.php?$query_string;
}
什么是伪静态?
动态URL转换。
例如:
/index.php?id=1
转换:
/article/1.html
提高SEO效果。
如何配置 HTTPS?
申请SSL证书。
server {
listen 443 ssl;
ssl_certificate cert.pem;
ssl_certificate_key cert.key;
}
如何实现 HTTP 跳 HTTPS?
server {
listen 80;
return 301 https://$host$request_uri;
}
什么是 502 Bad Gateway?
表示:
Nginx无法连接PHP-FPM
常见原因:
- PHP-FPM挂掉
- 端口错误
- 进程不足
检查:
systemctl status php-fpm
什么是 504 Gateway Timeout?
表示:
后端执行超时
例如:
SQL太慢
接口超时
调整:
fastcgi_read_timeout
Nginx 如何实现静态资源缓存?
location ~* \.(js|css|jpg|png)$ {
expires 30d;
}
效果:
减少服务器压力
如何禁止访问某些文件?
例如:
location ~ /\. {
deny all;
}
禁止访问:
.env
.git
Nginx 日志有哪些?
访问日志:
access.log
错误日志:
error.log
查看:
tail -f access.log
tail -f error.log
如何查看 Nginx 配置是否正确?
检查配置:
nginx -t
重新加载:
nginx -s reload
nginx.conf
server {
listen 80;
server_name example.com www.example.com;
# 网站根目录
root /var/www/project/public;
index index.php index.html index.htm;
# 访问日志
access_log /var/log/nginx/project_access.log;
error_log /var/log/nginx/project_error.log;
#################################################
# URL Rewrite(ThinkPHP/Laravel)
#################################################
location / {
try_files $uri $uri/ /index.php?$query_string;
}
#################################################
# PHP-FPM
#################################################
location ~ \.php$ {
try_files $uri =404;
# 把PHP请求转发给PHP-FPM处理。
fastcgi_pass unix:/run/php/php8.2-fpm.sock;
# 或者:
# fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
fastcgi_connect_timeout 60;
fastcgi_send_timeout 60;
fastcgi_read_timeout 60;
fastcgi_buffer_size 32k;
fastcgi_buffers 4 32k;
}
#################################################
# 静态资源缓存
#################################################
location ~* \.(jpg|jpeg|png|gif|ico|svg|webp)$ {
expires 30d;
access_log off;
}
location ~* \.(css|js)$ {
expires 7d;
access_log off;
}
location ~* \.(woff|woff2|ttf|eot)$ {
expires 30d;
access_log off;
}
#################################################
# Gzip压缩
#################################################
gzip on;
gzip_comp_level 6;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_types
text/plain
text/css
application/json
application/javascript
application/xml
application/xml+rss
image/svg+xml;
#################################################
# 安全配置
#################################################
# 禁止访问隐藏文件
location ~ /\. {
deny all;
}
# 禁止访问git目录
location ~ /\.git {
deny all;
}
# 禁止访问env文件
location ~ /\.env {
deny all;
}
# 禁止上传目录执行PHP
location ~* ^/uploads/.*\.php$ {
deny all;
}
#################################################
# favicon
#################################################
location = /favicon.ico {
log_not_found off;
access_log off;
}
#################################################
# robots
#################################################
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
#################################################
# 文件上传限制
#################################################
client_max_body_size 50M;
}