Nginx的主要作用和内部处理流程
1.负载均衡;
2.http请求的反向代理;
3.隐藏底层应用的地址;
4.解决跨域问题;
5.请求的额外处理; 等
关键名词解释
请求转发和重定向
转发:proxy_pass
重定向:rewrite (301和302重定向)
conf配置文件结构:
全局nginx配置,如user、worker_processes、pid等等......
http
{
http相关配置......
upstream g_mobile {
server 10.1.xx.xx:xxxx weight=10 max_fails=2 fail_timeout=30s;
server 10.1.xx.xx:xxxx weight=10 max_fails=2 fail_timeout=30s;
}
server
{
listen 端口;
server_name xxx;
rewrite ^/(.*) http://www.baidu.com/ permanent;
location /匹配路径1/ {
proxy_pass http://g_mobile/xxx;
}
location /匹配路径2/ {
proxy_pass http://xxx/xxx;
}
}
server
{
......
}
}
实际应用:
实例1:
应用服务器与外部接口http://p2p.cn/api/check网络不通,但是 打卡的nginx服务器(或者说审批日志的nginx服务器)跟p2p.cn网路是通的。
解决方案:
Tips:跳板机
实例2:
原来应用的链接是http://hfx.net/#urttr,但领导觉得链接不专业,需要去掉#号,打算后续链接改为http://hfx.net/i/urttr,而且历史链接需要兼容打开
解决方案:
Tips:重定向
#原来的nginx配置:
location / {
add_header Strict-Transport-Security “max-age=0; includeSubDomains” always;
alias /opt/appsys/iframe/;
index index.html index.htm;
}
实例3:
每次部署新的前端应用都要配置nginx的路径,比较麻烦,是否后续免配置或者动态配置
解决方案:
Tips:反代总目录
实例4:
原来应用的地址是https://xxx/a2p-msg-admin/#/a2padmin,但是产品觉得地址太长了,想要一个短链接,例如https://xxx/a2padmin
解决方案:
Tips:重定向
实例5:
原对外的接口是一个后端接口
http://xxx/TakeMobileNumberController/toTakeNumberPage?mytoken=xxxxxx,后来应用重构后前后端分离了,对外接口由后端接口改成前端接口了,格式如https://xxx/s/a2p-mobile/takeNumber.html?token=xxxx。但是为了兼容历史对接方,需要兼容这种历史调用
解决方案:
Tips:重定向、修改参数名
实例6:
两个域名都反代到我们的前端应用,也就是http://xxx/zd/和http://xxx/zd/都能访问我们前端应用。用户实际访问的地址是http://xxx/zd/hlj和http://xxx/zd/hlj,对于浏览器来说/hlj只是一个文件路径,但前端应用是没有这个路径的,这其实是动态的参数代表地区。另外对于http://xxx/zd/hlj和http://xxx/zd/hlj访问都是一个前端应用,但是前端需要对不同域名访问做不同的逻辑处理。
解决方案:
Tips:把路径处理成参数、区分出访问的域名
Location语法优先级排列
匹配符 匹配规则 优先级
= 精确匹配 1
^~ 以某个字符串开头 2
~ 区分大小写的正则匹配 3
~* 不区分大小写的正则匹配 4
!~ 区分大小写不匹配的正则 5
!~* 不区分大小写不匹配的正则 6
/ 通用匹配,任何请求都会匹配到 7
Nginx 配置实例:
#定义Nginx运行的用户和用户组
user www www;
#nginx进程数,建议设置为等于CPU总核心数。
worker_processes 16;
#全局错误日志定义类型,[ debug | info | notice | warn | error | crit ]
error_log /opt/appsys/logs/nginx/error.log error;
#进程文件
pid logs/nginx.pid;
events {
#单个进程最大连接数(最大连接数=连接数*进程数)
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
server_tokens off;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" '
'$request_time $upstream_response_time';
access_log /opt/appsys/logs/nginx/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#开启gzip压缩
gzip on; #打开gzip压缩功能
gzip_min_length 1k; #压缩阈值
gzip_buffers 4 16k; #buffer 不用修改
gzip_comp_level 2; #压缩级别:1-10,数字越大压缩的越好,时间也越长
gzip_types text/plain application/x-javascript application/javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png; # 压缩文件类型
gzip_vary off; #跟Squid等缓存服务有关,on的话会在Header里增加 "Vary: Accept-Encoding"
gzip_disable "MSIE [1-6]\."; #IE1-6版本不支持gzip压缩
#缓存路径
proxy_cache_path /opt/appsys/logs/nginx/nginx_cache levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=10g;
#xx系统负载均衡
upstream g_mobile {
server 10.1.xx.xx:xxxx weight=10 max_fails=2 fail_timeout=30s;
server 10.1.xx.xx:xxxx weight=10 max_fails=2 fail_timeout=30s;
}
server {
listen 80 default_server;
server_name localhost hfx.net www.hfx.net;
#小程序新链接
rewrite "^/i/([a-zA-Z0-9]{4,})$" /#$1 permanent;
#前端短域名公网映射:
location / {
add_header Strict-Transport-Security "max-age=0; includeSubDomains" always;
alias /opt/appsys/iframe/;
index index.html index.htm;
}
#http映射
location /cpmobile/ {
proxy_pass http://g_cpmobile/;
proxy_set_header Host $host;
proxy_set_header x-forward-for $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 16m;
proxy_send_timeout 10s;
proxy_read_timeout 30s;
proxy_cache off;
}
}
}
参考链接:
Nginx工作原理
https://www.jianshu.com/p/6215e5d24553
$1/$2的含义
https://blog.csdn.net/yxl0011/article/details/72818409?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-4.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-4.nonecase
Rewirte配置
https://blog.csdn.net/qq_41475058/article/details/89516051
内置变量
https://www.cnblogs.com/luyucheng/p/6148242.html