nginx 反向代理配置
反向代理服务
反向代理模式 | Nginx配置模块 |
---|---|
http、websocket、https | ngx_http_proxy_module |
fastcgi | ngx_http_fastcgi_module |
uwsgi | ngx_http_uwsgi_module |
grpc | ngx_http_v2_module |
缓存服务
配置语法:proxy_cache
语法:proxy_cache zone | off;
默认:off;
该语法在http、server、location下配置
缓存过期周期配置:
语法:proxy_cache_valid [code ...] time;
该语法在http、server、location下配置
缓存维度配置:
语法: proxy_cache_key string;
默认:proxy_cache_key $scheme$proxy_host$request_uri;
该语法在http、server、location下配置
让部分页面不缓存:
语法:proxy_no_cache string ...;
该语法在http、server、location下配置
示例
vim /etc/nginx/conf.d/cache_test.conf
upstream imooc {
server 116.62.103.228:8001;
server 116.62.103.228:8002;
server 116.62.103.228:8003;
}
# 代理缓存目录配置,缓存路径、目录分两级(一般这样配置)、zone名字和大小(1m大概8000key)、目录最大大小(满了后触发淘汰规则)、不活跃的为60分钟没有访问就删除、关闭零时目录(增加性能)
proxy_cache_path /opt/app/cache levels=1:2 keys_zone=imooc_cache:10m max_size=10g inactive=60m use_temp_path=off;
server {
listen 80;
server_name localhost jeson.t.imooc.io;
if ($request_uri ~ ^/(url3|login|register|password\/reset)) {
set $cokkie_nocache 1;
}
location / {
proxy_pass http://imooc;
proxy_cache imooc_cache; # 开启缓存
proxy_cache_valid 200 304 12h; # 对200和304返回是12小时过期
proxy_cache_valid any 10m; # 其他的都是10分钟过期
proxy_cache_key $host$uri$is_args$args; # 缓存key
proxy_no_cache $cookie_nocache $arg_nocache $arg_comment;
proxy_no_cache $http_pragma $http_authorization;
# 调试缓存命中分析,HIT值代表缓存命中
add_header Nginx-Cache "$upstream_cache_status";
# 如果有下列问题就跳到下一台服务器
proxy_next_upstream error timeout invalid_header http_500 502 http_503 http_504;
}
}
清空缓存
清空所有缓存:
rm -rf 缓存目录内容
对指定url清空:
第三方扩展模块 ngx_cache_purge
缓存命中分析
vim /etc/nginx/conf.d/cache_test.conf
http {
log_format main ... ' "$upstream_cache_status"';
}
upstream imooc {
server 116.62.103.228:8001;
server 116.62.103.228:8002;
server 116.62.103.228:8003;
}
# 代理缓存目录配置,缓存路径、目录分两级(一般这样配置)、zone名字和大小(1m大概8000key)、目录最大大小(满了后触发淘汰规则)、不活跃的为60分钟没有访问就删除、关闭零时目录(增加性能)
proxy_cache_path /opt/app/cache levels=1:2 keys_zone=imooc_cache:10m max_size=10g inactive=60m use_temp_path=off;
server {
listen 80;
server_name localhost jeson.t.imooc.io;
access_log /var/log/nginx/test_proxy.access.log main;
location / {
proxy_pass http://imooc;
proxy_cache imooc_cache; # 开启缓存
proxy_cache_valid 200 304 12h; # 对200和304返回是12小时过期
proxy_cache_valid any 10m; # 其他的都是10分钟过期
proxy_cache_key $host$uri$is_args$args; # 缓存key
# 调试缓存命中分析,HIT值代表缓存命中
add_header Nginx-Cache "$upstream_cache_status";
# 如果有下列问题就跳到下一台服务器
proxy_next_upstream error timeout invalid_header http_500 502 http_503 http_504;
}
}
分析:
awk '{if ($NF=="\"HIT\""){hit++}}END{printf "%.2f",hit/NR}' /var/log/nginx/test_proxy.access.log
大文件分片请求
http_slice_module
作用:每个子请求收到的数据都会形成一个独立文件,一个请求段了,其他请求不受影响
webScoket反向代理
vim /etc/nginx/conf.d/websocket_proxy.conf
# 映射赋值,客户端发送过来的变量http_upgrade如果有值就赋值,没值就给默认值赋值,空值就赋close
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
upstream websocket {
server 127.0.0.1:8010; # node.js 后端端口
}
server {
listen 8020;
access_log /var/log/nginx/test_proxy.access.log main;
location / {
proxy_pass http://websocket;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
}
}
Fastcgi反向代理
语法:fastcgi_pass address # 地址
该语法在location、if in location下配置
语法:fastcgi_index name;
作用:默认首页设置
该语法在http、server、 location下配置
语法:fastcgi_param parameter value
作用:参数配置
该语法在http、server、 location下配置
vim /etc/nginx/conf.d/websocket_proxy.conf
# 映射赋值,客户端发送过来的变量http_upgrade如果有值就赋值,没值就给默认值赋值,空值就赋close
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
upstream websocket {
server 127.0.0.1:8010; # node.js 后端端口
}
server {
listen 8020;
access_log /var/log/nginx/test_proxy.access.log main;
location / {
proxy_pass http://websocket;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
}
}
示例
安装包
# 安装数据库
yum install mariadb mariadb-server -y
# 启动数据库
systemctl start mariadb
# 登陆数据库测试
mysql -uroot -p
# 新建数据库
create database wds;
# 新建数据库用户
grant all privileges on wds.* to user1@'localhost' identified by 'passwd1';
# 刷新数据库权限表
flush privileges;
# 安装php7
yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
# 安装yum工具包
yum install yum-utils
# 开启php
yum-config-manager --enable remi-php72
# 查看是否配置好了yum源
yum search php72
# 安装php环境
yum install php72-php-fpm php72-php-common php72-php-mbstring php72-php-xmlrpc php72-php-soap php72-php-gd php72-php-xml php72-php-intl php72-php-mysql php72-php-cli php72-php-zip php72-php-curl
# 配置php-fpm端口
vim /etc/opt/remi/php72/php-fpm.d/www.conf
# 启动php-fpm
systemctl start php72-php-fpm.service
配置nginx
#>>> vim /etc/nginx/conf.d/wds_server.conf
server {
listen 8090;
server_name localhost imoocc.com;
access_log /var/log/nginx/wds_server.access.log main;
root /opt/app/wdcode/wordpress;
location / {
index index.php;
# wordpress框架支持的请求方式
# 如果访问的不是根路径,改写URL
try_files $uri $uri/ /index.php?args;
}
# 开启php支持
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000; # 请求转发
fastcgi_index index.php; # 首页
include fastcgi_params; # 细节性的配置文件
# 解析php的文件路径
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
# nginx -s reload -c /etc/nginx/nginx.conf
缓存
语法:fastcgi_cache_path path [levels=levels] keys_zone=name:size [inactive=time]
[max_size=size] ...;
该语法在http下配置
语法:fastcgi_cache_key string;
作用:定义缓存的维度,定义的越细缓存就越大越准确
该语法在http、server、location下配置
语法:fastcgi_cache zone | off;
作用:缓存开关
该语法在http、server、location下配置
语法:fastcgi_cache_valid [code ...] time;
作用:根据返回的状态码定义缓存的时间
该语法在http、server、location下配置
#>>> vim /etc/nginx/conf.d/fastcgicache_test.conf
# 缓存目录、目录层级、空间名字和大小、...
fastcgi_cache_path /opt/app/cache levels=1:2 keys_zone=imoocc:100m max_size=1g inactive=60m;
server {
listen 8090;
server_name localhost imoocc.com;
access_log /var/log/nginx/wds_server.access.log main;
root /opt/app/wdcode/wordpress;
index index.php index.html index.htm
location / {
index index.php;
# wordpress框架支持的请求方式
# 如果访问的不是根路径,改写URL
try_files $uri $uri/ /index.php?args;
if ( $request_uri ~ "/admin/") {
set $is_nocache yes;
}
}
# 开启php支持
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000; # 请求转发
fastcgi_index index.php; # 首页
include fastcgi_params; # 细节性的配置文件
# 解析php的文件路径
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_cache imoocc;
fastcgi_cache_key $scheme$request-method$host$request_uri$is_args$args;
fastcgi_cache_valid 200 60m;
# 方便前端缓存调试
add_header X-Cache-Source $upstream_cache_status;
# fastcgi_cache_use_stale error timeout invalid_header http_500;
# 忽略头信息
# fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
# fastcgi_cache_bypass $is_nocache;
# fastcgi_no_cache $is_nocache;
}
}
后端服务添加no-cache头对nginx代理缓存影响
后端服务添加了no-cache头后,nginx代理缓存默认不生效
如果想强制失效请忽略头信息