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. C++中const char*, string 与char*的转化
  2. 网站推广期间出现排名异常网站推广专员应如何应对?
  3. win10蓝屏提示重新启动_Windows 10系统出现蓝屏、CPU占用高,你们是如何解决的?...
  4. 2-hadoop-Hadoop以及生态
  5. mysql触发器 多个条件_当条件为真时,如何使用MySQL触发器更新多个表?
  6. 吉吉王国(二分+树形dp)
  7. npm安装typescript
  8. 牛客题霸-SQL篇——10~20题
  9. jQuery-表单验证使用方法
  10. axure rp8 添加动态面板_AxureRP教程–动态面板高级应用
  11. logo免费设计app有哪些?好用的logo设计app分享
  12. 亚马逊查询关键词排名的工具_亚马逊关键词的概念和查找工具
  13. ArcGIS 把字段允许空值设为否
  14. 【转】10种吓跑财神的漏财风水
  15. Excel VBA 中有关使用 UBound + CurrentRegion 提示类型不匹配的问题及解决方案
  16. 关于职场,你需要不断拼搏
  17. java List深拷贝的两种方式
  18. C语言(谭浩强版本,主讲人:小甲鱼)P41-P49
  19. 海康摄像头SDK二次开发错误问题解决方案(一):2. 错误号64:NET_DVR_LOADPLAYERSDKFAILED 64 载入当前目录下 Player Sdk 出错
  20. esp12f ESP8266芯片引脚

热门文章

  1. 贪心算法 openjudge 百练 python
  2. Android 使用Canvas+Paint画虚线
  3. Haroopad安装运行提示缺少libgconf
  4. dns使用tcp/53端口
  5. Clean Code总结
  6. ES可视化工具--ElasticHD--下载、安装、使用
  7. 护照识别ocr识别技术
  8. 23 WebGL的视点和视线
  9. 计算机一级如何用rank函数降序,rank函数降序排名
  10. 逐次逼近寄存器 ADC 的性能驱动单元电容器布局 2015