mirror of
https://github.com/qiurunze123/miaosha.git
synced 2023-11-19 22:41:03 +08:00
push nginx 优化
This commit is contained in:
parent
109ec1bc20
commit
ab0cdc9182
31
README.md
31
README.md
|
@ -111,24 +111,25 @@
|
||||||
9.所有秒杀相关的接口比如:秒杀,获取秒杀地址,获取秒杀结果,获取秒杀验证码都需要加上<br>
|
9.所有秒杀相关的接口比如:秒杀,获取秒杀地址,获取秒杀结果,获取秒杀验证码都需要加上<br>
|
||||||
秒杀是否开始结束的判断
|
秒杀是否开始结束的判断
|
||||||
#### [tomcat配置和优化](/docs/tomcat-good.md)
|
#### [tomcat配置和优化](/docs/tomcat-good.md)
|
||||||
#### [Nginx优化(前端缓存)]()
|
#### [Nginx优化(前端缓存)](/docs/ngnix-good.md))
|
||||||
|
1.并发优化 2.Keepalive长连接 3.压缩优化 4.配置缓存5.监控工具
|
||||||
![整体流程](https://raw.githubusercontent.com/qiurunze123/imageall/master/miaosha2.png)
|
![整体流程](https://raw.githubusercontent.com/qiurunze123/imageall/master/miaosha2.png)
|
||||||
### [缓存](/docs/high-concurrency/why-cache.md)
|
### [缓存](/docs/why-cache.md)
|
||||||
- [在项目中缓存是如何使用的?缓存如果使用不当会造成什么后果?](/docs/high-concurrency/why-cache.md)
|
- [在项目中缓存是如何使用的?缓存如果使用不当会造成什么后果?](/docs/why-cache.md)
|
||||||
- [Redis 和 Memcached 有什么区别?Redis 的线程模型是什么?为什么单线程的 Redis 比多线程的 Memcached 效率要高得多?](/docs/high-concurrency/redis-single-thread-model.md)
|
- [Redis 和 Memcached 有什么区别?Redis 的线程模型是什么?为什么单线程的 Redis 比多线程的 Memcached 效率要高得多?](/docs/redis-single-thread-model.md)
|
||||||
- [Redis 都有哪些数据类型?分别在哪些场景下使用比较合适?](/docs/high-concurrency/redis-data-types.md)
|
- [Redis 都有哪些数据类型?分别在哪些场景下使用比较合适?](/docs/redis-data-types.md)
|
||||||
- [Redis 的过期策略都有哪些?手写一下 LRU 代码实现?](/docs/high-concurrency/redis-expiration-policies-and-lru.md)
|
- [Redis 的过期策略都有哪些?手写一下 LRU 代码实现?](/docs/redis-expiration-policies-and-lru.md)
|
||||||
- [如何保证 Redis 高并发、高可用?Redis 的主从复制原理能介绍一下么?Redis 的哨兵原理能介绍一下么?](/docs/high-concurrency/how-to-ensure-high-concurrency-and-high-availability-of-redis.md)
|
- [如何保证 Redis 高并发、高可用?Redis 的主从复制原理能介绍一下么?Redis 的哨兵原理能介绍一下么?](/docs/how-to-ensure-high-concurrency-and-high-availability-of-redis.md)
|
||||||
- [Redis 的持久化有哪几种方式?不同的持久化机制都有什么优缺点?持久化机制具体底层是如何实现的?](/docs/high-concurrency/redis-persistence.md)
|
- [Redis 的持久化有哪几种方式?不同的持久化机制都有什么优缺点?持久化机制具体底层是如何实现的?](/docs/redis-persistence.md)
|
||||||
- [Redis 集群模式的工作原理能说一下么?在集群模式下,Redis 的 key 是如何寻址的?分布式寻址都有哪些算法?了解一致性 hash 算法吗?如何动态增加和删除一个节点?](/docs/high-concurrency/redis-cluster.md)
|
- [Redis 集群模式的工作原理能说一下么?在集群模式下,Redis 的 key 是如何寻址的?分布式寻址都有哪些算法?了解一致性 hash 算法吗?如何动态增加和删除一个节点?](/docs/redis-cluster.md)
|
||||||
- [了解什么是 Redis 的雪崩和穿透?Redis 崩溃之后会怎么样?系统该如何应对这种情况?如何处理 Redis 的穿透?](/docs/high-concurrency/redis-caching-avalanche-and-caching-penetration.md)
|
- [了解什么是 Redis 的雪崩和穿透?Redis 崩溃之后会怎么样?系统该如何应对这种情况?如何处理 Redis 的穿透?](/docs/redis-caching-avalanche-and-caching-penetration.md)
|
||||||
- [如何保证缓存与数据库的双写一致性?](/docs/high-concurrency/redis-consistence.md)
|
- [如何保证缓存与数据库的双写一致性?](/docs/redis-consistence.md)
|
||||||
- [Redis 的并发竞争问题是什么?如何解决这个问题?了解 Redis 事务的 CAS 方案吗?](/docs/high-concurrency/redis-cas.md)
|
- [Redis 的并发竞争问题是什么?如何解决这个问题?了解 Redis 事务的 CAS 方案吗?](/docs/redis-cas.md)
|
||||||
- [生产环境中的 Redis 是怎么部署的?](/docs/high-concurrency/redis-production-environment.md)
|
- [生产环境中的 Redis 是怎么部署的?](/docs/redis-production-environment.md)
|
||||||
|
|
||||||
## 高可用架构
|
## 高可用架构
|
||||||
- [Hystrix 介绍](/docs/high-availability/hystrix-introduction.md)
|
- [Hystrix 介绍](/docs/hystrix-introduction.md)
|
||||||
- [电商网站详情页系统架构](/docs/high-availability/e-commerce-website-detail-page-architecture.md)
|
- [电商网站详情页系统架构](/docs/e-commerce-website-detail-page-architecture.md)
|
||||||
|
|
||||||
### 高可用系统
|
### 高可用系统
|
||||||
- 如何设计一个高可用系统?
|
- 如何设计一个高可用系统?
|
||||||
|
|
218
docs/ngnix-good.md
Normal file
218
docs/ngnix-good.md
Normal file
|
@ -0,0 +1,218 @@
|
||||||
|
### 秒杀nginx优化
|
||||||
|
|
||||||
|
有问题或者宝贵意见联系我的QQ,非常希望你的加入!
|
||||||
|
|
||||||
|
##要求:
|
||||||
|
>目标
|
||||||
|
|
||||||
|
1.并发优化
|
||||||
|
2.KeepALive长连接优化
|
||||||
|
3.压缩优化
|
||||||
|
4.配置缓存
|
||||||
|
|
||||||
|
#### [/docs/tools/nginx nginx优化相关包](/docs/tools)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
安装:cd
|
||||||
|
yum install -y gcc gcc-c++
|
||||||
|
./configure --prefix=/usr/local/nginx --with-pcre=/home/qiurunze/下载/pcre-8.38 --with-http_stub_status_module --with-http_gzip_static_module --add-module=/home/qiurunze/下载/ngx_cache_purge-2.3
|
||||||
|
make
|
||||||
|
make install
|
||||||
|
|
||||||
|
ps -ef | grep nginx
|
||||||
|
|
||||||
|
./sbin/nginx -s reload
|
||||||
|
|
||||||
|
http://nginx.org/en/docs/
|
||||||
|
|
||||||
|
1.工作线程数和并发连接数
|
||||||
|
worker_processes 4; #cpu,如果nginx单独在一台机器上
|
||||||
|
worker_processes auto;
|
||||||
|
events {
|
||||||
|
worker_connections 4096;#每一个进程打开的最大连接数,超出了log中会有记录
|
||||||
|
multi_accept on; #可以一次建立多个连接
|
||||||
|
use epoll;
|
||||||
|
}
|
||||||
|
worker_rlimit_nofile 10240;每个进程打开的最大的文件数,受限于操作系统:
|
||||||
|
vi /etc/security/limits.conf
|
||||||
|
* hard nofile 102400
|
||||||
|
* soft nofile 102400
|
||||||
|
* soft core unlimited
|
||||||
|
* soft stack 10240
|
||||||
|
|
||||||
|
2.操作系统优化
|
||||||
|
配置文件/etc/sysctl.conf
|
||||||
|
sysctl -w net.ipv4.tcp_syncookies=1#防止一个套接字在有过多试图连接到达时引起过载
|
||||||
|
sysctl-w net.core.somaxconn=1024#默认128,连接队列
|
||||||
|
sysctl-w net.ipv4.tcp_fin_timeout=10 # timewait的超时时间
|
||||||
|
sysctl -w net.ipv4.tcp_tw_reuse=1 #os直接使用timewait的连接
|
||||||
|
sysctl -w net.ipv4.tcp_tw_recycle = 0 #回收禁用
|
||||||
|
|
||||||
|
3.Keepalive长连接
|
||||||
|
Nginx与upstream server:
|
||||||
|
upstream server_pool{
|
||||||
|
server localhost:8080 weight=1 max_fails=2 fail_timeout=30s;
|
||||||
|
keepalive 300; #300个长连接
|
||||||
|
}
|
||||||
|
同时要在location中设置:
|
||||||
|
location / {
|
||||||
|
proxy_http_version 1.1;
|
||||||
|
proxy_set_header Upgrade $http_upgrade;
|
||||||
|
proxy_set_header Connection "upgrade";
|
||||||
|
}
|
||||||
|
客户端与nginx(默认是打开的):
|
||||||
|
|
||||||
|
|
||||||
|
4.keepalive_timeout 60s; #长连接的超时时间
|
||||||
|
keepalive_requests 100; #100个请求之后就关闭连接,可以调大
|
||||||
|
keepalive_disable msie6; #ie6禁用启用压缩
|
||||||
|
gzip on;
|
||||||
|
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
|
||||||
|
gzip_proxied any;
|
||||||
|
gzip_types text/html text/plain application/x-javascript application/javascript text/css application/xml
|
||||||
|
gzip_vary on; #Vary: Accept-Encoding
|
||||||
|
gzip_static on; #如果有压缩好的 直接使用
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
5.状态监控
|
||||||
|
location = /nginx_status {
|
||||||
|
stub_status on;
|
||||||
|
access_log off;
|
||||||
|
allow <YOURIPADDRESS>;
|
||||||
|
deny all;
|
||||||
|
}
|
||||||
|
输出结果:
|
||||||
|
Active connections: 1
|
||||||
|
server accepts handled requests
|
||||||
|
17122 17122 34873
|
||||||
|
Reading: 0 Writing: 1 Waiting: 0
|
||||||
|
Active connections:当前实时的并发连接数
|
||||||
|
accepts:收到的总连接数,
|
||||||
|
handled:处理的总连接数
|
||||||
|
requests:处理的总请求数
|
||||||
|
Reading:当前有都少个读,读取客户端的请求
|
||||||
|
Writing:当前有多少个写,向客户端输出
|
||||||
|
Waiting:当前有多少个长连接(reading + writing)
|
||||||
|
reading – nginx reads request header
|
||||||
|
writing – nginx reads request body, processes request, or writes response to a client
|
||||||
|
waiting – keep-alive connections, actually it is active - (reading + writing)
|
||||||
|
|
||||||
|
6.实时请求信息统计ngxtop
|
||||||
|
https://github.com/lebinh/ngxtop
|
||||||
|
(1)安装python-pip
|
||||||
|
yum install epel-release
|
||||||
|
yum install python-pip
|
||||||
|
(2)安装ngxtop
|
||||||
|
pip install ngxtop
|
||||||
|
(3)使用
|
||||||
|
指定配置文件: ngxtop -c ./conf/nginx.conf
|
||||||
|
查询状态是200: ngxtop -c ./conf/nginx.conf --filter 'status == 200'
|
||||||
|
查询那个ip访问最多: ngxtop -c ./conf/nginx.conf --group-by remote_addr
|
||||||
|
|
||||||
|
-----------------------------------------------------------------------------
|
||||||
|
nginx.conf配置文件
|
||||||
|
user www;
|
||||||
|
worker_processes 4;#取决于cpu
|
||||||
|
|
||||||
|
error_log logs/error.log;
|
||||||
|
|
||||||
|
pid logs/nginx.pid;
|
||||||
|
|
||||||
|
worker_rlimit_nofile 10240; #每个进程打开的最大的文件数,受限于操作系统/etc/security/limits.conf
|
||||||
|
|
||||||
|
events {
|
||||||
|
worker_connections 10240;#每一个进程打开的最大连接数,超出了log中会有记录
|
||||||
|
multi_accept on; #可以一次建立多个连接
|
||||||
|
use epoll;
|
||||||
|
}
|
||||||
|
|
||||||
|
http {
|
||||||
|
include mime.types;
|
||||||
|
default_type application/octet-stream;
|
||||||
|
server_tokens off; #隐藏版本号
|
||||||
|
client_max_body_size 10m; #文件上传需要调大
|
||||||
|
|
||||||
|
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 logs/access.log main;
|
||||||
|
#默认写日志:打开文件写入关闭,max:缓存的文件描述符数量,inactive缓存时间,valid:检查时间间隔,min_uses:在inactive时间段内使用了多少次加入缓存
|
||||||
|
open_log_file_cache max=200 inactive=20s valid=1m min_uses=2;
|
||||||
|
|
||||||
|
sendfile on;
|
||||||
|
tcp_nopush on;
|
||||||
|
#与浏览器的长连接
|
||||||
|
keepalive_timeout 65;#长连接超时时间
|
||||||
|
keepalive_requests 500;#500个请求以后,关闭长连接
|
||||||
|
keepalive_disable msie6;
|
||||||
|
# 启用压缩
|
||||||
|
gzip on;
|
||||||
|
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
|
||||||
|
gzip_proxied any;
|
||||||
|
gzip_types text/plain application/x-javascript application/javascript text/css application/xml;
|
||||||
|
gzip_vary on; #Vary: Accept-Encoding
|
||||||
|
gzip_static on; #如果有压缩好的 直接使用
|
||||||
|
#超时时间
|
||||||
|
proxy_connect_timeout 5; #连接proxy超时
|
||||||
|
proxy_send_timeout 5; # proxy连接nginx超时
|
||||||
|
proxy_read_timeout 60;# proxy响应超时
|
||||||
|
# 开启缓存,2级目录
|
||||||
|
proxy_cache_path /usr/local/nginx/proxy_cache levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=20g;
|
||||||
|
proxy_ignore_headers X-Accel-Expires Expires Cache-Control;
|
||||||
|
proxy_hide_header Cache-Control;
|
||||||
|
proxy_hide_header Pragma;
|
||||||
|
|
||||||
|
#反向代理服务器集群
|
||||||
|
upstream server_pool{
|
||||||
|
server localhost:8080 weight=1 max_fails=2 fail_timeout=30s;
|
||||||
|
server localhost:8081 weight=1 max_fails=2 fail_timeout=30s;
|
||||||
|
keepalive 200; # 最大的空闲的长连接数
|
||||||
|
}
|
||||||
|
|
||||||
|
server {
|
||||||
|
listen 80;
|
||||||
|
server_name localhost 192.168.220.133;
|
||||||
|
|
||||||
|
location / {
|
||||||
|
#长连接
|
||||||
|
proxy_http_version 1.1;
|
||||||
|
proxy_set_header Upgrade $http_upgrade;
|
||||||
|
proxy_set_header Connection "upgrade";
|
||||||
|
#Tomcat获取真实用户ip
|
||||||
|
proxy_set_header Host $http_host;
|
||||||
|
proxy_set_header X-Real-IP $remote_addr;
|
||||||
|
proxy_set_header X-Forwarded-For $remote_addr;
|
||||||
|
proxy_set_header X-Forwarded-Proto $scheme;
|
||||||
|
proxy_pass http://server_pool;
|
||||||
|
}
|
||||||
|
# 状态监控
|
||||||
|
location /nginx_status {
|
||||||
|
stub_status on;
|
||||||
|
access_log off;
|
||||||
|
allow 127.0.0.1;
|
||||||
|
allow 192.168.220.133;
|
||||||
|
deny all;
|
||||||
|
}
|
||||||
|
#用于清除缓存
|
||||||
|
location ~ /purge(/.*)
|
||||||
|
{
|
||||||
|
allow 127.0.0.1;
|
||||||
|
allow 192.168.220.133;
|
||||||
|
deny all;
|
||||||
|
proxy_cache_purge cache_one $host$1$is_args$args;
|
||||||
|
}
|
||||||
|
# 静态文件加缓存
|
||||||
|
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css|html|htm)?$
|
||||||
|
{
|
||||||
|
expires 1d;
|
||||||
|
proxy_cache cache_one;
|
||||||
|
proxy_cache_valid 200 304 1d;
|
||||||
|
proxy_cache_valid any 1m;
|
||||||
|
proxy_cache_key $host$uri$is_args$args;
|
||||||
|
proxy_pass http://server_pool;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user