Nginx配置文件,这样优化后,性能提升好几倍

1 Nginx简介

Nginx是一个轻量级的高性能HTTP反向代理服务器,同时它也是一个通用类型的代理服务器,支持绝大部分协议,如TCP、UDP、SMTP、HTTPS等。

用Nginx代理后,客户端的请求由其进行分发到服务器处理,服务器处理完后再返回Nginx,由Nginx结果返回给客户端。

2 Linux中Nginx安装

服务器创建Nginx目录并进入:

[root@localhost]# mkdir /soft && mkdir /soft/nginx/  
[root@localhost]# cd /soft/nginx/  

❷下载Nginx安装包

可以服务器远程工具上传已经下载好的压缩包,也用wget命令服务器在线下载压缩包:

[root@localhost]# wget https://nginx.org/download/nginx-1.21.6.tar.gz  

wget命令的可通过yum命令安装;

❸命令解压Nginx压缩包:

[root@localhost]# tar -xvzf nginx-1.21.6.tar.gz  

❹下载并安装Nginx所需的依赖库和包:

[root@localhost]# yum install --downloadonly --downloaddir=/soft/nginx/ gcc-c++  
[root@localhost]# yum install --downloadonly --downloaddir=/soft/nginx/ pcre pcre-devel4  
[root@localhost]# yum install --downloadonly --downloaddir=/soft/nginx/ zlib zlib-devel  
[root@localhost]# yum install --downloadonly --downloaddir=/soft/nginx/ openssl openssl-devel 

完成后ls查看,所有需要依赖都在里面:

然后用rpm命令依次构建每个依赖包;或用以下下指令一键安装全部依赖包:

[root@localhost]# rpm -ivh --nodeps *.rpm  

❺cd到nginx目录,执行Nginx配置脚本,提前配置好环境便于后面安装,默认位于/usr/local/nginx/目录:

[root@localhost]# cd nginx-1.21.6  
[root@localhost]# ./configure --prefix=/soft/nginx/  

❻执行命令编译并安装Nginx:

[root@localhost]# make && make install  

❼回到/soft/nginx/目录,用ls可看到安装nginx后生成的文件。

❽修改安装后conf目录下的nginx.conf:

[root@localhost]# vi conf/nginx.conf  
修改端口号:listen    80;  
修改IP地址:server_name  你当前机器的本地IP(线上配置域名);  

❾制定Nginx配置文件并启动:

[root@localhost]# sbin/nginx -c conf/nginx.conf  
[root@localhost]# ps aux | grep nginx  

❿放开80端口,刷新服务器防火墙:

[root@localhost]# firewall-cmd --zone=public --add-port=80/tcp --permanent  
[root@localhost]# firewall-cmd --reload  
[root@localhost]# firewall-cmd --zone=public --list-ports  

⓫浏览器输入Nginx配的IP或域名访问,如果你看到了Nginx欢迎界面,那么恭喜你安装成功。

3 Nginx配置详解及优化

1、Nginx用户及组。

user nginx nginx ;

2、工作进程数量,按实际生产机器调整,通常等于CPU数量或者2倍于CPU。

worker_processes 8;

3、错误日志存放路径配置。

error_log logs/error.log; 
error_log logs/error.log notice; 
error_log logs/error.log info; 

4、pid的存放路径。

pid logs/nginx.pid;

5、指定进程可以打开的数目。

worker_rlimit_nofile 204800;

6、每个进程的最大连接数,实际生产中按照机器配置调整,原则上sh设置大一点,但不超过CPU100%。

理论上单台nginx允许的最大连接数为:worker_processes*worker_connections

worker_connections 204800;

7、keepalive超时时间。

keepalive_timeout 60;

8、客户端请求头部的缓冲区大小。该参数根据实际业务需要来设置(例如分页)。

client_header_buffer_size 4k;

9、打开的文件指定缓存,默认不启用;

max为缓存数量(建议和打开文件数量一样);
inactive为文件多久没有请求删除该文件缓存。
open_file_cache max=65535 inactive=60s;

10、缓存有效信息检测时间。

open_file_cache_valid 80s;

11、open_file_cache命令中的inactive参数时间内,设置文件的最少使用次数,超过设置的该数字,文件描述符会在缓存中一直打开。

open_file_cache_min_uses 1;

12、设置http服务器,利用它的反向代理功能提供负载均衡支持

http
{
	include mime.types;
}

mime类型设置,以及类型由mime.type文件定义

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"';
log_format log404 '$status [$time_local] $remote_addr $host$request_uri $sent_http_location';

日志格式设置

$remote_addr与$http_x_forwarded_for用以记录客户端的ip地址;
$remote_user:用来记录客户端用户名称;
$time_local: 用来记录访问时间与时区;
$request: 用来记录请求的url与http协议;
$status: 用来记录请求状态;成功是200,
$body_bytes_sent :记录发送给客户端文件主体内容大小;
$http_referer:用来记录从那个页面链接访问过来的;
$http_user_agent:记录客户浏览器的相关信息;

log_format命令设置log格式后,需用access_log命令设置log存放路径;

access_log logs/host.access.log main;
access_log logs/host.access.404.log log404;

13、
server_names_hash_max_size 和server_names_hash_bucket_size用来控制服务器名字hash表的保存

server_names_hash_bucket_size 128;

hash bucket size=hash表大小,且是一路处理器缓存大小的倍数。

如果hash bucket size等于一路处理器缓存的大小,那么在查找键的时候,最坏的情况下在内存中查找的次数为2。

14、如果要增大13点中两个参数的提示,首先要增大client_header_buffer_size参数值

client_header_buffer_size 4k;

15、客户端请求头部的缓冲区大小。分页大小可以用命令getconf PAGESIZE取得。

large_client_header_buffers 8 128k;

nginx默认会用client_header_buffer_size这个buffer来读header值,当header过大,则用


large_client_header_buffers来读header值。

16、设置通过nginx上传文件的大小

open_file_cache max=102400 inactive=20s;

17、sendfile指令指定 nginx 是否调用sendfile 函数(zero copy 方式)来输出文件,对于普通应用,必须设为on。如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络IO处理速度,降低系统uptime。

sendfile on;

18、允许或禁止socket的TCP_CORK的选项,只有使用了sendfile时才用

tcp_nopush on;

19、后端服务器连接的超时时间

proxy_connect_timeout 90; 

20、后端服务器处理请求的时间

proxy_read_timeout 180;

21、后端服务器数据响应时间

proxy_send_timeout 180;

22、设置从被代理服务器读取的第一部分应答的缓冲区大小

proxy_buffer_size 256k;

23、设置用于读取应答的缓冲区数目和大小,默认情况也为分页大小。

proxy_buffers 4 256k;

24、设置在写入proxy_temp_path时数据的大小,预防一个工作进程在传递文件时阻塞太长

proxy_busy_buffers_size 256k;
 
proxy_temp_file_write_size 256k;

25、proxy_temp_path和proxy_cache_path指定的路径必须在同一分区

proxy_temp_path /data0/proxy_temp_dir;

26、设置内存缓存空间大小为300MB,如果内容2天未被访问则自动清除缓存,硬盘缓存空间大小为30GB。

proxy_cache_path /data0/proxy_cache_dir levels=1:2 keys_zone=cache_one:300m inactive=2d max_size=30g;

27、keepalive超时时间。

keepalive_timeout 120;

28、nginx的upstream配置

a、轮询(默认)

全部请求都按时间顺序分配到后端服务器。

b、weight(权重)

很好理解,就是指定轮询概率,权重大小和访问概率成正比,用于后端服务器性能不均的情况。

例如:

upstream bakend {
server 192.168.110.230 weight=10;
server 192.168.110.235 weight=5;
}

//此时的设置测192.168.110.230的访问概率是192.168.110.235的两倍

c、ip_hash

所有请求都按访问ip的hash结果分配,如此以来每个用户可以访问固定的后端服务器。

可以用来解决session共享的问题,但当前环境下大多用redis等解决session共享了。

例如:

upstream bakend {
ip_hash;
server 192.168.110.230:88;
server 192.168.110.235:80;
}

d、fair

fair是第三方提供的不是nginx官方提供的,即按后端服务器的响应时间来分配请求,响应时间短的优先分配。

例如:

upstream backend {
server server1;
server server2;
fair;
}

e、url_hash

url_hash也是第三方提供的不是nginx官方提供的,即按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。

例如:

upstream backend {
server squid1:3128;
server squid2:3128;
hash $request_uri;
hash_method crc32;
}

29、nginx请求限流

限制哪些恶意攻击请求网站的请求。

#限制用户连接数来预防DOS攻击
limit_conn_zone $binary_remote_addr zone=perip:10m;
limit_conn_zone $server_name zone=perserver:10m;
#限制同一客户端ip最大并发连接数
limit_conn perip 2;
#限制同一server最大并发连接数
limit_conn perserver 20;
#限制下载速度,根据自身服务器带宽配置
limit_rate 300k;

30、nginx负载均衡

upstream ygoapi{
server ip:port fail_timeout=5 max_fails=3;
server ip:port fail_timeout=5 max_fails=3;
ip_hash; #负载均衡策略
}

31、nginx中htpps配置

server {
listen 443;
server_name 域名;
ssl on;
root /xxx/xxx/html; // 前台文件存放文件夹,一般使用 Nginx 初始化的文件夹,当然也可以自己修改
index index.html;// 上面配置的文件夹里面的index.html
ssl_certificate /路径/证书名称.pem;
ssl_certificate_key /路径/证书名称.key;
ssl_session_timeout 5m;
# SSL协议配置
ssl_protocols SSLv2 SSLv3 TLSv1.2; #表示使用TLS类型协议
ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP; #表示使用加密套件的类型
ssl_prefer_server_ciphers on;
location / {
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;
}
}
}

server {
listen 80;
server_name your-domain.com;// 你的域名
rewrite ^(.*)$ https://$host:443$1 permanent;// 把http的域名请求转成https且转发到443端口
}

32、nginx全局配置

worker_processes 1;
pid /var/run/nginx.pid;

events {
worker_connections 2048;
multi_accept on;
use epoll;
}

http {
include /etc/nginx/mime.types;
default_type application/octet-stream;

log_format main '{"@timestamp":"$time_iso8601",'
'"host":"$server_addr",'
'"clientip":"$remote_addr",'
'"size":$body_bytes_sent,'
'"responsetime":$request_time,'
'"upstreamtime":"$upstream_response_time",'
'"upstreamhost":"$upstream_addr",'
'"http_host":"$host",'
'"url":"$uri",'
'"xff":"$http_x_forwarded_for",'
'"referer":"$http_referer",'
'"agent":"$http_user_agent",'
'"status":"$status"}';

sendfile on;
tcp_nopush on;
tcp_nodelay on;

server_names_hash_bucket_size 128;
server_names_hash_max_size 512;
keepalive_timeout 65;
client_header_timeout 15s;
client_body_timeout 15s;
send_timeout 60s;

limit_conn_zone $binary_remote_addr zone=perip:10m;
limit_conn_zone $server_name zone=perserver:10m;
limit_conn perip 2;
limit_conn perserver 20;
limit_rate 300k;

proxy_cache_path /data/nginx-cache levels=1:2 keys_zone=nginx-cache:20m max_size=50g inactive=168h;

client_body_buffer_size 512k;
client_header_buffer_size 4k;
client_max_body_size 512k;
large_client_header_buffers 2 8k;
proxy_connect_timeout 5s;
proxy_send_timeout 120s;
proxy_read_timeout 120s;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
proxy_next_upstream http_502 http_504 http_404 error timeout invalid_header;

gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 4;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
gzip_vary on;
gzip_disable "MSIE [1-6].";

include /etc/nginx/conf.d/*.conf;
}

33、nginx内核参数配置

#如果想把timewait降下了就要把tcp_max_tw_buckets值减小,默认是180000
net.ipv4.tcp_max_tw_buckets = 5000

#开启重用功能,允许将TIME-WAIT状态的sockets重新用于新的TCP连接
net.ipv4.tcp_tw_reuse = 1

#系统中最多有多少个TCP套接字不被关联到任何一个用户文件句柄上。如果超过这个数字,孤儿连接将即刻被复位并打印出警告信息。这个限制仅仅是为了防止简单的DoS攻击
net.ipv4.tcp_max_orphans = 262144

#当keepalive起用的时候,TCP发送keepalive消息的频度。缺省是2小时。我们可以调短时间跨度
net.ipv4.tcp_keepalive_time = 30

34、server配置

#隐藏版本信息
server_tokens off;
server {
listen 80;
server_name www.ygoclub.com;
charset utf-8;

#重定向HTTP请求到HTTPS
return 301 https://$server_name$request_uri;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值