1、nginx反向代理屏蔽,所有连接请求中断,返回444

 location /admin/ {#如果后端的服务器返回502、504、执行超时等错误,自动将请求转发到upstream负载均衡池中的另一台服务器,实现故障转移proxy_next_upstream http_502 http_504 http_404 error timeout invalid_header;proxy_pass http://admin;#把原http请求的Header中的Host字段也放到转发的请求里,代理的后端服务器可以通过Host头得知用户访问的真正的域名,如不设置,则得到是代理服务(nginx)的IP,这样对于动态拼接的url,后端服务器能在页面里返回正确的urlproxy_set_header HOST $host;#透传客户端真实IP地址proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;#通过nginx设置HttpOnly Secure SameSite参数解决Cookie跨域丢失proxy_cookie_path / "/; httponly; secure; SameSite=Lax";#deny all;#过滤其他域名的请求,返回444状态码return 444;     }

2、nginx log文件 json格式配置

 log_format main  '{"DateTime":"$time_iso8601",' '"client":"$remote_addr",'               客户端的ip地址'"uri":"$uri",''"status":"$status",''"domain":"$host",''"host":"$server_addr",''"size":"$body_bytes_sent",'             发送给客户端文件主体内容大小'"rt":"$request_time",''"urt":"$upstream_response_time",''"uct":"$upstream_connect_time",''"uht":"$upstream_header_time",''"useragent":"$http_user_agent",'        客户端浏览器的相关信息'"upstreampstatus":"$upstream_status",''"upstreamaddr":"$upstream_addr",''"AC-LogTypeID":10,''"AC-TraceId":"$upstream_http_x_b3_traceid",''"AC-LogType":"proxy-log-type"''}';'"request": "$request",'                请求的url与http协议'"request_method": "$request_method",''"uri": "$uri", ''"http_referrer": "$http_referer",'      从那个页面链接访问过来的'"http_x_forwarded_for": "$http_x_forwarded_for",'  客户端真实ip地址access_log  /apps/usr/nginx/logs/access_cronlog.log  main;#什么是remote_addr
remote_addr 是服务端根据请求TCP包的ip指定的。假设从client到server中间没有任何代理,那么web服务器(nginx,apache等)就会把client的ip设为IPremote_addr;如果存在代理转发http请求,web服务器会把最后一次代理服务器的IP设置为remote_addr;#什么是x_forwarded_for
当使用代理时,web服务器无法通过TCP数据包来源获得发起请求的client的真实IP,因此代理服务器通常会在http请求头增加一个叫做x_forwarded_for的字段,用来记录请求发起者的真实IP;

3、nginx安装需要指定的模块

--user=mms --group=mms --prefix=/apps/usr/nginx --error-log-path=/apps/var/nginx/error.log --http-log-path=/apps/var/nginx/access.log --with-http_stub_status_module --with-http_ssl_module --with-http_sub_module --with-http_gzip_static_module --with-http_realip_module--with-http_stub_status_module     #nginx的客户端状态
--with-http_ssl_module           #ssl加密
--with-http_sub_module           #web站点内容过滤功能模块
--with-http_gzip_static_module   #预读gzip功能
--with-http_realip_module        #在nginx访问日志中去除代理IP,显示客户的真实IP

4、NGINX跨域

location /datang-h5/ {add_header Access-Control-Allow-Origin *;add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';add_header 'Access-Control-Allow-Headers' 'DNT,Authorization,Accept,Origin,Keep-Alive,User-Agent,X-Mx-ReqToken,X-Data-Type,X-Auth-Token,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';proxy_next_upstream http_502 http_504 http_404 error timeout invalid_header;proxy_pass http://datang-h5/;proxy_set_header HOST $host:18091;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;if ($request_method = 'OPTIONS') {add_header 'Access-Control-Max-Age' 1728000;add_header 'Content-Type' 'text/plain; charset=utf-8';add_header 'Content-Length' 0;return 204;}}

5、Nginx支持ipv6配置

#需要–with-ipv6模块
#https ipv6地址需要解析
server {listen [::]:80 ipv6only=on;}server {listen [::]:443 ssl ipv6only=on;}访问格式:
https://[fe80::fc44:6e5e:eeda:ebac]:443

6、Nginx IP鉴权

#通过allow location /api/v1 {proxy_next_upstream http_502 http_504 http_404 error timeout invalid_header;proxy_pass http://api-zuul;proxy_set_header HOST $host;#proxy_set_header X-Real-IP $remote_addr;#proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;#proxy_set_header X-Forwarded-For $http_x_forwarded_for;#proxy_set_header X-Forwarded-For $remote_addr;set_real_ip_from  0.0.0.0/0;real_ip_header  X-Cluster-Client-IP;allow 43.225.211.91;allow 112.33.12.149;allow 218.205.209.238;allow 203.130.42.149;allow 223.104.17.213;allow 112.35.69.29;allow 112.35.69.101;allow 124.64.17.188;allow 113.46.147.67;allow 120.244.174.135;#deny all;
nginx 获取真实ip,将 release 环境 nginx 相关配置修改为(其中100.125.0.0/16为SLB所在网段)set_real_ip_from 0.0.0.0/0;set_real_ip_from 100.125.0.0/16;real_ip_header X-Forwarded-For;allow 223.72.211.85;#allow 10.128.12.0/24;allow 218.205.209.238;allow 221.179.195.93;deny all;4、七层SLB + http_realip_module 模块测试
4.1、将 release 环境 nginx 相关配置修改为(其中100.97.0.0/16为SLB所在网段):
set_real_ip_from 100.97.0.0/16;
real_ip_header X-Forwarded-For;
real_ip_recursive on;

7、nginx日志分割

[3::root@zmms-2::/apps/opt]# >>>cat nginx_log.sh
#!/bin/bash
#设置日志文件存放目录
LOG_HOME="/apps/usr/nginx1196/logs"
#备分文件名称
LOG_PATH_BAK="$(date -d yesterday +%Y%m%d%H%M)".access.log
#重命名日志文件
mv ${LOG_HOME}/access.log ${LOG_HOME}/${LOG_PATH_BAK}
#向nginx主进程发信号重新打开日志
kill -USR1 `cat /apps/usr/nginx1196/logs/nginx.pid`#计划时钟
crontab -e
00 02 * * * (cd /apps/opt ; sh -x nginx_log.sh      > /apps/opt/TempFile/Crony_nginx_log.logzip 2>&1)
30 02 * * * (cd /apps/opt ; sh -x backup_nginx.sh   > /apps/opt/TempFile/Crony_nginx1196.logzip 2>&1)

8、nginx备份脚本

[4::root@zmms-2::/apps/opt]# >>>cat backup_nginx.sh
#!/bin/bashTempFilePath=${ProcessPath}/TempFile
ip_str=`ifconfig |grep '10.56.0' |awk '{print $2}'`
logpath=/baklog/10.56.0.2/
LogPathAll="/apps/usr/nginx1196/logs/"mkdir -m 755 -p /baklog/10.56.0.2/nginx1196/for LogPath in ${LogPathAll}
do
cd ${LogPath}
#find ./ -maxdepth 1 -type f -name "*.20??[-_]??[-_]?*[0-9,g]"  | xargs gzip
#find ./ -maxdepth 1 -type f -name "*-20??[-_]??[-_]?*[0-9,g]"  | xargs gzip
#find ./ -maxdepth 1 -type f -name "*_20??[-_]??[-_]?*[0-9,g]"  | xargs gzip
find /apps/usr/nginx1196/logs/ -maxdepth 1 -type f -name "*20*"  | xargs gzip
donechown mms:mms /apps/usr/nginx1196/logs/20*
mv $LogPathAll*.gz /baklog/10.56.0.2/nginx1196/find $logpath -type f -mtime +365 |xargs rm -rf

9、防盗链

放盗链:   location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip)$ {valid_referers none blocked www.baidu.com;if ($invalid_referer) {rewrite ^/ [img]http://yzil.cn[/img];return 403;}}  

Nginx优化解决问题相关推荐

  1. 简单介绍六点nginx优化的方法

    这篇文章主要介绍了nginx优化的六点方法,有对nginx优化不太熟悉的同学可以参考下 一.优化Nginx并发量 [root@proxy ~]# ab -n 2000 -c 2000 http://1 ...

  2. nginx优化 突破十万并发

    nginx优化 突破十万并发一.一般来说nginx 配置文件中对优化比较有作用的为以下几项:1. worker_processes 8;nginx 进程数,建议按照cpu 数目来指定,一般为它的倍数 ...

  3. 【转帖】Nginx优化use参数epoll,kqueue,rtsig,eventport,poll

    下图对比了poll select epoll和kqueue的性能.select和poll是一个级别的,epoll和kqueue是一个级别的,相差不多.epoll用在linux上,kqueue用在bsd ...

  4. 一场由nginx优化引起的tcp/ip及tcpdump研究

    一场由nginx优化引起的tcp/ip及tcpdump研究 在这里不得不再吐槽下国内整个IT粗糙浮躁,度娘下来的中文文档几尽抄袭~google下来的文档英文文档质量远高于国内中文文档. 用ie或没有安 ...

  5. nginx优化配置选项

    Nginx以事件驱动(epoll)的方式编写,所以有非常好的性能,同时也是一个非常高效的反向代理.负载平衡.但是Nginx并不支持cgi方式运行,原因是可以减少因此带来的一些程序上的漏洞.所以必须使用 ...

  6. nginx优化——包括https、keepalive等

    nginx优化--包括https.keepalive等 一.nginx之tcp_nopush.tcp_nodelay.sendfile 1.TCP_NODELAY 你怎么可以强制 socket 在它的 ...

  7. nginx优化_安全方面的优化

    1.1 Nginx优化分类 安全优化(提升网站安全性配置) 性能优化(提升用户访问网站效率) 1.2 Nginx安全优化 1.2.1 隐藏nginx版本信息优化 官方配置参数说明:http://ngi ...

  8. nginx 优化,突破十万并发

    nginx优化 突破十万并发 一.一般来说nginx 配置文件中对优化比较有作用的为以下几项: worker_processes 8; nginx 进程数,建议按照cpu 数目来指定,一般为它的倍数 ...

  9. nginx 优化(收藏)

    nginx优化 充分发挥Nginx的高效性和稳定性,对于Nginx优化非常重要.下面主要是从编译安装.第三方插件.系统内核等三方面介绍. 编译安装过程优化 1.减小Nginx编译后的文件大小 在编译N ...

最新文章

  1. 2021年广东省高考英语听说成绩查询,广东2021年高考准考证今起自行打印 成绩及分数线6月24日左右公布...
  2. Vue:利用Vue生成的网页,在浏览器中的标签页中的图标与标题怎么修改为自己的?
  3. HTTP和HTTPS详解
  4. 沈阳药科大学计算机二级好考吗,沈阳药科大学考研难吗?一般要什么水平才可以进入?...
  5. linux-shell入门-shell两种使用方式-shell的基本特性
  6. (原創) 如何使用Operator Overloading? (C/C++)
  7. 计算机网络实训简介,计算机网络实验报告介绍.doc
  8. 你身边有创业失败导致负债累累的案例吗
  9. try-catch lasterr
  10. 你能相信这些逼真的油画是前端小姐姐只用HTML+CSS画出来的吗?精细到毛发,让美术设计也惊叹丨GitHub热榜...
  11. sublime text 前端开发插件安装和配置
  12. 【Python剧情版游戏】优美精致的画风甜甜的剧情、很难不让人上头啊?你get到了嘛
  13. java学习练习预埋件配筋计算
  14. Logstash 时间转换(YYYY-MM-dd HH:mm:ss转Unix时间)
  15. OpenGL——顶点属性
  16. 《3D Point Cloud Registration for Localization using a Deep Neural Network Auto-Encoder》翻译
  17. 利用watir自动化测试并截图,截图后保存
  18. 使用itext把图片转成pdf文件,图片来自本地路径或者文件上传,输出pdf存在本地或者远程minio
  19. linux上使用drive从google drive 下载文件和文件夹
  20. 博客实现浏览量统计次数

热门文章

  1. 青岛港:服务器虚拟化和动环的一体化运维管理
  2. 14种营销软文标题的写法,你也可以写出阅读10W+的爆文!
  3. 啃完阿里这份高并发编程核心笔记,反手涨了 5K
  4. 现代计算机的原型英语,冯诺依曼通过____实现了现代计算机的原型
  5. centos下sh脚本监控文件内容告警
  6. 【Rust开荒】数组避坑分享
  7. docker安装GitLib及基本使用
  8. 计算机应用技术与信息安全与管理,广安职业技术学院2020年单独招生技能测试大纲(计算机应用技术、软件技术、信息安全与管理)...
  9. 【雷达通信】 基于卡尔曼滤波实现GPS和INS联合导航含Matlab源码
  10. Go开源库Excelize介绍,电子Excel表格操作强大的库