文档:http://nginx.org/en/docs/
ngin_status: 并发统计
Ngxtop : 请求统计
1 并发优化
sudo vim /usr/local/nginx/conf/nginx.conf
user gigabyte; #用户
worker_processes 4; #cpu
error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
pid logs/nginx.pid;
# 并发相关的参数
worker_rlimit_nofile 20480; # 每个进程打开的最大的文件数,受限于操作系统
events {
use epoll;
multi_accept on; # 可以一次建立多个连接
worker_connections 10240; # 单个工作进程最大并发连接数
}
2 KeepAlive长连接
# 长连接
keepalive_timeout 65;# 长连接的超时时间
keepalive_requests 500;# 500个请求之后就关闭连接,可以调大
keepalive_disable msie6; # ie6 禁用
3 压缩优化
gzip on;
gzip_http_version 1.1;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
gzip_proxied any;
gzip_types text/plain text/css application/javascript application/x-javascript application/json application/xml application/vnd.ms-fontobject application/x-font-ttf application/svg+xml application/x-icon;
gzip_vary on; #Vary: Accept-Encoding
gzip_static on; # 如果有压缩好的,直接使用
如果遇到报错不要慌,根据提示去进行就好,这里提示使用--without-http_gzip_module,那就用呗,解决~
4 配置缓存
1 安装
- 将上面几个安装包放到/usr/local/src目录下然后解压
cd nginx-1.15.9
sudo ./configure --prefix=/usr/local/nginx --with-pcre=/usr/local/src/pcre-8.38 --with-http_stub_status_module --with-http_gzip_static_module --add-module=/usr/local/src/ngx_cache_purge-2.3
sudo make
sudo make install
# 启动nginx
cd /usr/local/nginx/sbin
sudo ./nginx
sudo ./nginx -s stop
sudo ./nginx -s reload
2 配置
sudo vim /usr/local/nginx/conf/nginx.conf
#1 并发优化
user gigabyte; #用户
worker_processes 4; #cpu
error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
pid logs/nginx.pid;
# 并发相关的参数
worker_rlimit_nofile 20480; # 每个进程打开的最大的文件数,受限于操作系统
events {
use epoll;
multi_accept on; # 可以一次建立多个连接
worker_connections 10240; # 单个工作进程最大并发连接数
}
http {
include mime.types;
default_type application/octet-stream;
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 0;
#keepalive_timeout 65;
# 2 长连接
keepalive_timeout 65;# 长连接的超时时间
keepalive_requests 500;# 500个请求之后就关闭连接,可以调大
keepalive_disable msie6; # ie6 禁用
#gzip on;
# 3 压缩相关
gzip on;
gzip_http_version 1.1;
gzip_disable "MSIE [1-6]\.(?!.*SV1)"; # 在IE 1-6禁用
gzip_proxied any; # 无论在那个代理过来的都启用
gzip_types text/plain text/css application/javascript application/x-javascript application/json application/xml application/vnd.ms-fontobject application/x-font-ttf application/svg+xml application/x-icon;
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响应超时
# 反向代理服务器集群
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;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
# root html;
# index index.html index.htm;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade; # 与http 2.0 相关
proxy_set_header Connection "upgrade"; # 与http 2.0 相关
#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 10.23.23.25;
allow 10.23.23.33;
deny all;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
- NOTE1 : worker_rlimit_nofile 204800
- 如果设置了worker_connections=204800, 那么需要同时修改/etc/security/limits.conf
vi /etc/security/limits.conf - hard nofile 204800
- soft nofile 204800
- soft core unlimited
- soft stack 204800
修改完之后重启
limit -n
- NOTE2: 如果开启了反向代理,那么就
开启了 ----> 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; # 最大的空闲的长连接数
}
那么要在location中设置
location / {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
} - NOTE: 配置文件修改完之后,检查语法是否正确
./sbin/nginx -t
然后重启
cd /usr/local/nginx
sudo ./sbin/nginx -s reload
3 根据端口号查看进程
sudo netstat -nap | grep 808
如果你想用nginx管理两个tomcat ,那么你需要有两个tomcat才行哦
目前只成功了一个。