Appearance
nginx
nginx 是一个高性能的开源 Web 服务器软件,它也可以作为反向代理服务器、负载均衡器和 HTTP 缓存器使用。它已经成为互联网上最流行的 Web 服务器软件之一,被广泛用于各种网站和 Web 应用程序中。
配置
nginx 的主配置文件通常位于 /etc/nginx/nginx.conf,用户新增配置一般放在 /etc/nginx/conf.d/ 目录下。
nginx
# 定义工作进程数
worker_processes auto;
# 定义事件处理机制
events {
worker_connections 1024;
}
# 定义http服务
http {
# 定义日志格式
log_format main '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent"';
# 定义访问日志文件路径
access_log /var/log/nginx/access.log main;
# 定义错误日志文件路径
error_log /var/log/nginx/error.log;
# 定义虚拟主机
server {
# 定义监听端口
listen 80;
# 定义域名
server_name example.com;
# 定义根目录
root /var/www/example.com;
# 定义默认文档
index index.html;
# 定义访问限制
location /admin/ {
allow 192.168.0.0/16;
deny all;
}
# 定义反向代理
location /api/ {
proxy_pass http://localhost:8080/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}
其中 server
块表示一个虚拟主机,而 http
块中可以配置多个 server
。
常用指令
- listen: 指定服务器监听的端口号和协议,如 listen 80; 或 listen 443 ssl;
- server_name: 指定此服务器的域名或 IP 地址,如 server_name example.com;
- root: 指定此虚拟主机的根目录,如 root /var/www/example.com;
- index: 指定此虚拟主机默认的索引文件,如 index index.html;
- error_log: 指定此虚拟主机的错误日志文件路径,如 error_log /var/log/nginx/example.com-error.log;
- access_log: 指定此虚拟主机的访问日志文件路径,如 access_log /var/log/nginx/example.com-access.log;
- ssl_certificate: 指定此虚拟主机的 SSL 证书路径,如 ssl_certificate /etc/nginx/ssl/example.com.crt;
- ssl_certificate_key: 指定此虚拟主机的 SSL 证书私钥路径,如 sl_certificate_key /etc/nginx/ssl/example.com.key;
常用变量
- $scheme: 当前请求的协议(http 或 https)。
- $host: 请求的主机名。
- $request_uri: 包含整个请求的 URI,包括查询字符串。
- $uri: 不包括查询字符串的 URI。
- $args: 查询字符串部分,即 URI 中 ? 后面的部分。
- $remote_addr: 客户端的 IP 地址。
- $server_name: 匹配到当前请求的服务器名。
- $request_method: 请求方法(GET、POST 等)。
- $http_user_agent: 用户代理,即客户端浏览器的标识。
- $http_referer: 来源 URL,即链接到当前页面的 URL。
location
location 指令用于匹配客户端请求的 URL 路径,并指定如何处理这些请求。当有多个 location 指令匹配同一个 URL 路径时,nginx 会按照配置文件中 location 指令出现的顺序依次匹配请求的 URL 路径,匹配成功后会停止继续匹配。因此,当有多个 location 指令匹配同一个 URL 路径时,应该将更具体的匹配规则放在前面,以确保正确匹配到请求。
常用的匹配模式包括以下几种:
- 通用匹配:使用 / 符号作为 location 指令的参数时,表示通用匹配,可以匹配任何请求的 URL 路径。
nginx
location / {
}
- 精确匹配:使用 = 符号开头的 location 指令表示精确匹配,只有当请求的 URL 路径与指定的路径完全相同时才会匹配成功。
nginx
location = /index.html {
}
- 前缀匹配:使用普通的字符串作为 location 指令的参数时,表示前缀匹配。
nginx
location /images {
}
- 正则表达式匹配:使用正则表达式作为 location 指令的参数,可以实现更灵活的匹配。
~*
符号表示使用正则表达式匹配,并忽略大小写。
nginx
location ~* \.(gif|jpg|png)$ {
}
proxy_pass
proxy_pass 指令可用于将 HTTP 请求代理到一个其它的服务器上,也就是“反向代理” (reverse proxying)。
- 代理到本地服务
nginx
location / {
proxy_pass http://127.0.0.1:8080;
}
return
用于返回指定的 HTTP 状态码和响应头,并可以进行重定向。
- 重定向:可以使用 return 指令将客户端请求重定向到其他 URL;
nginx
location /old {
return 301 http://example.com/new;
}
- 返回错误页面:可以使用 return 指令返回特定的错误页面;
nginx
error_page 404 /404.html;
location / {
try_files $uri $uri/ =404;
}
location = /404.html {
internal;
return 200 'Page not found';
}
- 返回特定的 HTTP 状态码:可以使用 return 指令返回特定的 HTTP 状态码;
nginx
location /health_check {
return 200 'OK';
}
- 过滤请求:可以使用 return 指令拦截某些请求;
nginx
location ~* \.(php|jsp|asp)$ {
return 403;
}
rewrite
用于重写 URL。
- break:表示重写后停止执行当前 location 中的其他指令;
- last:表示重写后重新匹配 location;
- redirect:表示重定向;
常见配置:
- URL 重写:部重定向服务器内部,客户端无感知。
nginx
location /foo {
rewrite ^/foo(.*)$ /bar$1;
}
注意
内部重定向时,重定向的 URL 必须指向同一个服务器。如果要将请求重定向到另一个服务器,应该使用 proxy_pass 指令代理请求,而不是进行内部重定向。
- 重定向:将客户端请求重定向到其他 URL;
nginx
location /abc {
rewrite ^/abc$ http://example.com/xyz redirect;
}