nginx 101
一个高性能web服务器
一、价值
访问路由
反向代理
负载均衡
内容缓存
二、重装nginx
apt remove --purge nginx nginx-full nginx-common
apt install nginx
三、命令
查看版本:nginx -v
配置文件语法检查:nginx -t
重新加载配置文件:nginx -s reload
停止:nginx -s stop
启动:nginx -s start
四、语法规则
配置文件由指令和指令块组成
每条指令以分号结尾,指令与参数间以空格符号分隔
指令块以{}大括号将多条指令组织在一起
以#注释
以$使用变量
五、内置变量
$host:请求信息中的host
$http_cookie:cookie信息
$http_referer:来源地址
$http_user_agent:客户端代理信息
$remote_addr:客户端IP地址
$remote_port:客户端端口号
$server_addr:服务端地址
$server_name:服务器名
$server_port:服务器端口号
$uri:请求的uri
六、配置块
main(nginx的全局配置,对全局生效)
user user [group]:指定运行nginx的用户[组]
worker_process:工作进程数量,默认是1个;auto等同于服务器的CPU个数
error_log:记录错误信息的日志
pid:nginx进程PID存放路径
daemon:nginx的运行方式,on:默认,前台,用于调试;off:后台,用于生产
events(影响nginx服务器与用户的网络连接)
worker_connections:工作进程最大连接数
use:网络IO模型
select
poll
kqueue
epoll
/dev/poll
eventport
http(可以嵌套多个server,配置代理、缓存、日志)
include mime.types:文件扩展名与文件类型映射
default_type:默认响应类型
log_format:自定义服务日志
access_log:指定日志输出目录
sendfile:开启高效文件传输模式
keepalive_timeout:客户端与服务端连接超时时间
gzip:gzip压缩
server(定义虚拟主机)
listen:ip:port
server_name:主机名称
location
用于请求配置的路由
~:URI字母大小写敏感
~*:忽略大小写
^~:匹配前半部分
文件路径
root:root + location
alias:allias路径 替换location路径
index:定向到首页
error_page:error_page code path
try_files:try_files path1 path2 $uri,如果匹配不到前面的path,则重定向到最后的uri上
autoindex:可以快速搭建静态资源下载网站
反向代理
proxy_pass:将请求反向代理到指定服务器上,proxy_pass URL
proxy_set_header:转发host头部,proxy_set_header key value
proxy_method:转发请求方法proxy_method method
upstream(负载均衡)
尽量把请求平均的分布到每一台上游服务器上
策略
轮询:按时间顺序逐一分配到不同的后端服务器
加权轮询:根据weight分配权重,值越大权重越大
ip_hash:每个请求按访问ip的hash结果分配
七、默认nginx.conf
#指定运用nginx的用户[组]
user nginx;
#工作进程数量,默认是1,auto等同于服务器的cpu个数
worker_processes auto;
#记录错误信息日志
error_log /var/log/nginx/error.log notice;
#nginx进程pid存放路径
pid /var/run/nginx.pid;
events {
#工作进程最大连接数
worker_connections 1024;
}
http {
#文件扩展名与文件类型映射
include /etc/nginx/mime.types;
#默认响应类型
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"';
#指定日志输出目录
access_log /var/log/nginx/access.log main;
#开启高效文件传输模式
sendfile on;
#减少网络报文段段数量
#tcp_nopush on;
#客户端与服务端连接超时时间
keepalive_timeout 65;
#开启gzip压缩
#gzip on;
#虚拟主机配置目录
include /etc/nginx/conf.d/*.conf;
}
八、设置过期时间
#对html文件缓存
location ~ .*\.(htm|html)$ {
expires 24h;
root /var/www/html
}
#对图片缓存
location ~ .*\.(gif|jpg|jpeg|png|bmp|ico)$ {
expires 30d;
root /var/www/img/
}
九、设置跨域
反向代理解决跨域
#通常前端资源与后端接口配置在两个域名下,容易造成跨域的问题
#比如 www.fe.com及api.com
#可以通过poxy_pass转发,使得请求都到www.fe.com下,从而解决跨域的问题
server {
listen 80;
server_name www.fe.com
location ^~/api/ {
rewrite ^/api/(.*) $1 break;
poxy_pass api.com
#两个域名之间传递cookie
proxy_cookie_domain www.fe.com api.com
}
}
添加header解决跨域
server {
listen 80;
server_name api.com;
# 全局变量获得当前请求 origin,带 Cookie 的请求不支持 *(通配符)
add_header 'Access-Control-Allow-Origin' $http_origin;
# 为 true 可带上 cookie
add_header 'Access-Control-Allow-Credentials' 'true';
# 允许请求方法
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
# 允许请求的 header,可以为 *(通配符)
add_header 'Access-Control-Allow-Headers' $http_access_control_request_headers;
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
if ($request_method = 'OPTIONS') {
# OPTIONS 请求的有效期,在有效期内不用发出另一条预检请求
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain; charset=utf-8';
add_header 'Content-Length' 0;
return 204; # 200 也可以
}
location / {
root /usr/share/nginx/html/be;
index index.html;
}
}
十、防盗链
防止服务器上的资源被其他用户通过其他手段下载
location ~ .*\.(gif|jpg|jpeg|png|bmp|ico)$ {
# 只允许 ip 请求资源
# none 表示没带 refer
# blocked 表示不是标准 HTTP 请求
valid_referers none blocked ip;
if ($invalid_referer) {
rewrite ^/ http://$host/logo.png;
}
}
十一、负载均衡
http {
upstream api.com {
# 采用 ip_hash 负载均衡策略
ip_hash;
# 采用 fair 负载均衡策略
# fair;
# 负载均衡目的服务地址
server 127.0.0.1:80;
server 127.0.0.1:81;
server 127.0.0.1:82 weight=10; # weight 方式,不写默认为 1
}
server {
location / {
proxy_pass http://api.com;
proxy_connect_timeout 10;
}
}
}
十二、重定向
根据客户端的userAgent,重定向到h5还是pc
location / {
# 当 userAgent 中检测到移动端设备
if ($http_user_agent ~* '(Android|webOS|iPhone|iPod|BlackBerry)') {
set $mobile_request '1';
}
# 则重定向至
if ($mobile_request = '1') {
rewrite ^.+ http://h5.example.com;
}
}
http从定向到https
server {
listen 80;
location / {
return 301 https://$host$request_uri;
}
}
server {
listen 443 ssl;
}
无www重定向到www
server {
listen 80;
server_name example.com;
return 301 http://www.example.com$request_uri;
}
server {
listen 80;
server_name www.example.com;
location / {
proxy_pass http://localhost:8080;
proxy_set_header X-Forwared-Proto $scheme;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}