nginx 调优

nginx 很优秀

实施步骤

按下面内容进行

下载阿里开源镜像的EPEL源

1
2
3
4
5
wget -O /etc/yum.repos.d/epel-7.repo http://mirrors.aliyun.com/repo/epel-7.repo

# 缓存数据
yum clean all
yum makecache

操作系统

1
2
ulimit -n   # 系统打开文件数
ulimit -u # 用户打开文件数

添加base auth

1
2
3
4
5
6
# 创建用户
htpasswd -b ./htpasswd user30 zWQ7GnzL

#在 nginx.conf中添加
auth_basic "Please enter your username and password";
auth_basic_user_file /etc/nginx/htpasswd;

Nginx

连接数 打开文件数 事件

1
2
3
4
5
6
7
8
9
10
user nginx;
worker_processes 4;
worker_cpu_affinity 0001 0010 0100 1000;
worker_rlimit_nofile 65535;

events {
use epoll; #事件模式
worker_connections 204800; # 打开连接数
multi_accept on;
}

其它配置

代理socket

1
2
3
4
5
6
7
8
9
10
11
12
stream {
upstream cloudsocket {
hash $remote_addr consistent;
server xxx.xxx.xxx.xx:3306 weight=5 max_fails=3 fail_timeout=30s;
}
server {
listen 3306;#数据库服务器监听端口
proxy_connect_timeout 10s;
proxy_timeout 300s;#设置客户端和代理服务之间的超时时间,如果5分钟内没操作将自动断开。
proxy_pass cloudsocket;
}
}

代理http

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
server {
listen 80;
server_name xxx.com;
root html;
index index.html index.htm;

location /favicon.ico {
root /usr/share/nginx/html/;
}

location /api {
proxy_redirect off;
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_pass http://127.0.0.1:8080;
}

location / {
root /dist;
index index.html index.htm;
try_files $uri $uri/ @rewrites;
}

location @rewrites{
rewrite ^(.+)$ /index.html last;
}

# 匹配/node /info /search
location ~* ^/(node|info|search)/ {
proxy_pass http://localhost:8080;
}
}

## 代理多个
location ~* ^/(sys|app|api|oss|login|getInfo|logout) {
proxy_redirect off;
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_cache_valid 200 60m;
proxy_cache_min_uses 2;
proxy_cache_methods GET;
proxy_pass http://127.0.0.1:9090;
}

## 添加层级目录
location ~^/ddchain/ {
proxy_redirect off;
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_connect_timeout 600s;
proxy_send_timeout 600s;
proxy_read_timeout 600s;
proxy_pass http://127.0.0.1:8089;
rewrite "^/ddchain(.*)$" $1 break;
}

隐藏版本号

1
2
# 在http中
server_tokens off;