12.14Nginx常见问题、Nginx优化

一、server优先级
二、禁止IP访问
三、Nginx try_file路径匹配
1.正常的配置文件
2.使用try_file的配置
3.修改try_file配置
4.一般使用场景
1)配置nginx
2)安装tomcat
3)测试
四、Nginx优雅显示错误页面
1.跳转到网络
2.跳转到本地文件
3.访问php找不到文件时错误页面跳转
一、优化概述
1.需要了解
2.从哪些方面入手
3.影响性能的指标
二、ab测试工具
1.安装ab测试工具
2.工具使用
3.配置nginx网站
4.配置hosts压测nginx访问静态资源
5.压测tomcat访问静态资源
6.压测httpd访问静态资源
7.结论
8.tomcat正经安装方式
三、系统优化
1.查看文件句柄数
2.设置文件句柄数
1)系统全局的设置
2)用户局部设置
3)针对服务设置
4)系统通用优化
四、代理服务优化
1.配置用户访问负载均衡的长连接
2.配置负载均衡代理到web的长连接
3.配置web代理到php保持长连接
4.代理优化配置
五、静态资源优化
1.静态资源
2.静态资源缓存
1)浏览器缓存原理
2)浏览器缓存参数含义

一、server优先级

1.首先选择所有的字符串完全匹配的server_name。(完全匹配)
2.选择通配符在前面的server_name,如.driverzeng.com blog.driverzeng.com
3.选择通配符在后面的server_name,如driverzeng. driverzeng.com driverzeng.cn
4.最后选择使用正则表达式匹配的server_name
5.如果全部都没有匹配到,那么将选择在listen配置项后加入[default_server]的server块
6.如果没写,那么就找到匹配listen端口的第一个Server块的配置文件

二、禁止IP访问

当用户通过访问IP或者未知域名访问你得网站的时候,你希望禁止显示任何有效内容,可以给他返回500,目前国内很多机房都要求网站关闭空主机头,防止未备案的域名指向过来造成麻烦

[root@lb01 conf.d]# cat server4.conf 
server {
    listen 80 default_server;           #默认优先返回;
    server_name _;                      #空主机头或者IP;
    return 500;                         #直接返回500错误;
}

三、Nginx try_file路径匹配

nginx的try_file路径匹配,Nginx会按顺序检查文件及目录是否存在(根据 root 和 alias 指令设置的参数构造完整的文件路径),并用找到的第一个文件提供服务。在元素名后面添加斜杠 / 表示这个是目录。如果文件和目录都不存在,Nginx会执行内部重定向,跳转到命令的最后一个 uri 参数定义的 URI 中。

1.正常的配置文件
[root@web01 conf.d]# vim linux.try.com.conf 
server {
    listen 80;
    server_name linux.try.com;

    location / {
        root /code;
        index index.html;
    }
}

[root@web01 conf.d]# systemctl restart nginx
[root@web01 conf.d]# echo "test try_file" > /code/index.html
2.使用try_file的配置
[root@web01 conf.d]# vim linux.try.com.conf 
server {
    listen 80;
    server_name linux.try.com;

    location / {
        root /code;
        try_files $uri /1.jpg;
    }
}

#访问测试:
1.访问域名时 linux.try.com,返回的结果是 1.jpg
由于请求的是域名,后面没有 uri,那么 $uri 匹配到的就是 "空",匹配不到内容的情况下,返回 1.jpg

2.访问域名 linux.try.com/index.html,返回的结果是 index.html
由于请求的是linux.try.com/index.html,$uri 匹配到的是 index.html,就返回相应内容
3.修改try_file配置
[root@web01 conf.d]# vim linux.try.com.conf 
server {
    listen 80;
    server_name linux.try.com;

    location / {
        root /code;
        try_files $uri $uri/ /1.jpg;
    }
}

#访问测试:
1.访问域名 linux.try.com,返回的结果是 index.html
由于请求的是域名,后面没有 uri,那么 $uri 匹配到的就是 "空"$uri 匹配不到内容的情况下,匹配 $uri/,匹配到的是 "空/"/ 配置的是 /code,那么就回去请求code目录下的 index.html
4.一般使用场景

1)配置nginx

[root@web01 conf.d]# vim linux.try.com.conf
server {
    listen 80;
    server_name linux.try.com;
    root /code;

    location / {
        try_files $uri $uri/ @java;           #当$uri和$uri/都匹配不到时,由后端的java来进行处理,名字可自定义,但一定要加@
    } 

    location @java {
    	proxy_pass http://172.16.1.7:8080;          #配置后端tomcat
    }
}
[18:42:02 root@web01 ~]#systemctl restart nginx

2)安装tomcat

[root@web01 ~]# yum install -y tomcat
[root@web01 ~]# cd /usr/share/tomcat/webapps/
[root@web01 webapps]# mkdir ROOT
[root@web01 webapps]# echo "test try_file @java" > ROOT/index.html
[19:01:32 root@web01 ~]#systemctl restart tomcat
[18:59:39 root@web01 ~]#netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name          
tcp6       0      0 :::8080                 :::*                    LISTEN      16461/java       

3)测试
1.code目录下有index.html的情况,访问域名正常显示 index.html
2.把code目录改名,访问域名,返回的是tomcat下面配置的页面

#第一次测试
[18:50:07 root@web01 ~]#echo "test try_file" > /code/index.html
[18:51:15 root@web01 ~]#chown -R www.www /code
配置本地hosts文件,访问linux.try.com
10.0.0.7 linux.try.com===》正常显示index.html
#第二次测试
[18:53:53 root@web01 ~]#mv /code /codebak
配置本地hosts文件,访问linux.try.com
10.0.0.7 linux.try.com===》显示tomcat下面的配置文件ROOT/index.html


四、Nginx优雅显示错误页面

1.跳转到网络
[root@web01 conf.d]# vim linux.error.com.conf 
server {
    listen       80;
    server_name  linux.error.com;

    location / {
        root /code;
        index index.html;
        error_page 404 http://www.baidu.com;
    }
}
[20:19:31 root@web01 ~]#systemctl restart nginx
#配置hosts文件,访问linux.error.com
10.0.0.7 linux.error.com===》显示内容为/code下的index.html
#访问linux.error.com/1.php
因为/code下无此内容会报错404直接跳转百度页面

2.跳转到本地文件

[root@web01 conf.d]# vim linux.error.com.conf 
server {
    listen       80;
    server_name  linux.error.com;

    location / {
        root /code;
        index index.html;
        error_page 404 /1.jpg;
    }
}

3.访问php找不到文件时错误页面跳转

[root@web01 conf.d]# vim linux.error.com.conf 
server {
    listen       80;
    server_name  linux.error.com;
    root /code;
    index index.php;
    error_page 404 /404.jpg;

    location ~* \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
        if (!-e $request_filename) {
            rewrite (.*) http://linux.error.com/1.jpg;
        }
    }
}
[21:04:40 root@web01 code]#systemctl restart nginx
[21:04:51 root@web01 code]#rm -rf /code/1.jpg 
#配置本地hosts文件访问linux.error.com.1.php
10.0.0.7 linux.error.com===》页面显示404.jpg
只要访问以.php结尾的都交给location —— php处理,当文件不存在会去执行if判断。重新访问找1.jpg,如果1.jpg也没有的话去执行error_page报错404

Nginx优化

一、优化概述

1.需要了解

1、首先需要了解我们当前系统的结构和瓶颈,了解当前使用的是什么,运行的是什么业务,都有哪些服务,了解每个服务最大能支撑多少并发。比如nginx作为静态资源服务并发是多少,最高瓶颈在哪里,能支持多少qps(每秒查询率)的访问请求,那我们怎么得出这组系统结构瓶颈呢,比如top查看系统的CPU负载、内存使用率、总得运行进程等,也可以通过日志去分析请求的情况,当然也可以通过我们前面介绍到的stub_status模块查看当前的连接情况,也可以对线上的业务进行压力测试(低峰期),去了解当前这套系统能承担多少的请求和并发,以做好响应的评估。这个是我们做性能优化最先考虑的地方。

2、其次我们需要了解业务模式,虽然我们是做性能优化,但每一个性能的优化都是为业务所提供的服务的,我们需要了解每个业务接口的类型,比如:电商网站中的抢购模式,这种情况下,平时没什么流量,但到了抢购时间流量会突增。我们还需要了解系统层次化的结构,比如:我们使用nginx做的是代理、还是动静分离、还是后端直接服务用户,那么这个就需要我们对每一层做好相应的梳理。以便更好的服务业务。

3、最后我们需要考虑性能与安全,往往注重了性能,但是忽略了安全。往往过于注重安全,对性能又会产生影响。比如:我们在设计防火墙功能时,检测过于严密,这样就会给性能带来影响。那么如果对于性能完全追求,却不顾服务的安全,这个也会造成很大的隐患,所以需要评估好两者的关系,把握好两者的孰重孰轻。以及整体的相关性,权衡好对应的点。

1.首先需要了解我们当前系统的结构和瓶颈
2.其次我们需要了解业务模式
3.我们还需要了解系统层次化的结构
4.最后我们需要考虑性能与安全

2.从哪些方面入手

OSI七层模型:物理层,数据链路层,网络层,传输层,会话层,表示层,应用层

1.硬件:
1)nginx做负载均衡只需要cpu核心多一些
2)nginx做静态资源存储,磁盘大一些
3)nginx做动态资源代理,CPU
4)ES,redis服务内存需要大一些
2.网络层:
1)丢包、延迟
2)带宽
3.系统:
1)文件描述符
2)端口复用
4.应用:tcp长连接
5.服务:服务的针对性优化
3.影响性能的指标

  • 网络
  • 系统
  • 服务
  • 程序
  • 数据库

二、ab测试工具

1.安装ab测试工具
#查看命令所在的包
[root@web01 ~]# yum provides ab

#安装ab
[root@web01 ~]# yum install -y httpd-tools
2.工具使用
[root@web01 ~]# ab -n 200 -c 2 http://www.baidu.com/

-n	请求的次数
-c	请求的并发数
-k	开启长连接

Usage: ab [options] [http[s]://]hostname[:port]/path
3.配置nginx网站
[root@web01 conf.d]# vim linux.ab.com.conf 
server {
    listen 80;
    server_name linux.ab.com;
    root /code;

    location / {
        try_files $uri $uri/ @java;
    }

    location @java {
        proxy_pass http://172.16.1.7:8080;
    }
}

[root@web01 conf.d]# echo "test nginx web" > /code/index.html
4.配置hosts压测nginx访问静态资源
[root@web01 conf.d]# vim /etc/hosts
10.0.0.7 linux.ab.com

[root@web01 conf.d]# ab -n 10000 -c 200 http://linux.ab.com/
Server Software:        nginx/1.18.0
Server Hostname:        linux.ab.com
Server Port:            80

Document Path:          /
Document Length:        15 bytes

Concurrency Level:      200
Time taken for tests:   1.084 seconds
Complete requests:      10000
Failed requests:        0
Write errors:           0
Total transferred:      2590000 bytes
HTML transferred:       150000 bytes
Requests per second:    9226.11 [#/sec] (mean)
Time per request:       21.678 [ms] (mean)
Time per request:       0.108 [ms] (mean, across all concurrent requests)
Transfer rate:          2333.56 [Kbytes/sec] received
5.压测tomcat访问静态资源
[root@web01 conf.d]# mv /code/ /codebak

[root@web01 conf.d]# ab -n 10000 -c 200 http://linux.ab.com/
Server Software:        nginx/1.18.0
Server Hostname:        linux.ab.com
Server Port:            80

Document Path:          /
Document Length:        20 bytes

Concurrency Level:      200
Time taken for tests:   3.025 seconds
Complete requests:      10000
Failed requests:        0
Write errors:           0
Total transferred:      2720000 bytes
HTML transferred:       200000 bytes
Requests per second:    1739.70 [#/sec] (mean)
Time per request:       114.962 [ms] (mean)
Time per request:       0.575 [ms] (mean, across all concurrent requests)
Transfer rate:          462.11 [Kbytes/sec] received
6.压测httpd访问静态资源
[root@web01 conf.d]# yum install -y httpd
[root@web01 conf.d]# echo "test httpd" > /var/www/html/index.html
[root@web01 conf.d]# systemctl stop nginx
[root@web01 conf.d]# systemctl start httpd

[root@web01 conf.d]# ab -n 10000 -c 200 http://10.0.0.7/
Server Software:        Apache/2.4.6
Server Hostname:        10.0.0.7
Server Port:            80

Document Path:          /
Document Length:        11 bytes

Concurrency Level:      200
Time taken for tests:   2.888 seconds
Complete requests:      10000
Failed requests:        0
Write errors:           0
Total transferred:      2810000 bytes
HTML transferred:       110000 bytes
Requests per second:    3462.37 [#/sec] (mean)
Time per request:       57.764 [ms] (mean)
Time per request:       0.289 [ms] (mean, across all concurrent requests)
Transfer rate:          950.12 [Kbytes/sec] received
7.结论
nginx处理静态资源的速度比其他web服务要快
8.tomcat正经安装方式
#1.下载或上传tomcat包
[root@web01 ~]# mkdir /service
[root@web01 ~]# cd /service
[root@web01 service]# rz apache-tomcat-8.5.51.tar.gz

#2.解压代码包
[root@web01 service]# tar xf apache-tomcat-8.5.51.tar.gz

#3.配置java环境
1.上传并解压至指定文件夹
[root@web01 service]# tar xf jdk-8u40-linux-x64.gz -C /service/
[root@web01 service]# mv jdk1.8.0_40 java1.8

2.修改添加环境变量
[root@web01 service]# vim /etc/profile.d/java.sh
export JAVA_HOME=/service/java1.8
export JRE_HOME=/service/java1.8/jre
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib:$JRE_HOME/lib:$CLASSPATH
export PATH=$PATH:$JAVA_HOME/bin
[root@web01 service]# source /etc/profile

#4.配置tomcat页面
[root@web01 service]# echo "linux_tomcat" > apache-tomcat-8.5.51/webapps/ROOT/index.html

#5.启动tomcat,启动的时候最好看着日志
[04:29:08 root@lb01 service]#mv apache-tomcat-9.0.30 basic  #改名字
[04:29:22 root@lb01 service]#./basic/bin/startup.sh   #开启tomcat服务
Using CATALINA_BASE:   /service/basic
Using CATALINA_HOME:   /service/basic
Using CATALINA_TMPDIR: /service/basic/temp
Using JRE_HOME:        /service/java1.8/jre
Using CLASSPATH:       /service/basic/bin/bootstrap.jar:/service/basic/bin/tomcat-juli.jar
Tomcat started.
[04:29:40 root@lb01 service]#tail -f ./basic/logs/catalina.out  #查看tomcat日志

三、系统优化

1.查看文件句柄数
#1.查看文件句柄数设置
[root@lb01 ~]# ulimit -n
65535

#2.查看打开的文件句柄数
[root@lb01 ~]# lsof | wc -l
2945
[root@web01 ~]# lsof | wc -l
12490

#3.查看指定服务的打开文件句柄数
[root@web01 ~]# lsof -p 13592 | wc -l
2.设置文件句柄数

1)系统全局的设置

[root@web01 ~]# vim /etc/security/limits.conf
* - nofile 65535
* soft nofile 65535
* hard nofile 65535

*		#所有用户
-		#当超过设置的文件句柄数时,什么都不干
soft  	#当超过设置的文件句柄数时,仅提示
hard	    #当超过设置的文件句柄数时,直接限制

2)用户局部设置

[root@web01 ~]# vim /etc/security/limits.conf
root - nofile 65535
root soft nofile 65535
root hard nofile 65535

3)针对服务设置

[root@lb01 ~]# vim /etc/nginx/nginx.conf 
user  www;
worker_processes  1;
worker_rlimit_nofile 65535;

4)系统通用优化

[root@lb01 ~]# vim /etc/sysctl.conf
net.ipv4.tcp_fin_timeout = 2
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_keepalive_time = 600
net.ipv4.ip_local_port_range = 4000 65000
net.ipv4.tcp_max_syn_backlog = 16384
net.ipv4.tcp_max_tw_buckets = 36000
net.ipv4.route.gc_timeout = 100
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_synack_retries = 1
net.core.somaxconn = 16384
net.core.netdev_max_backlog = 16384
net.ipv4.tcp_max_orphans = 16384
net.ipv4.ip_forward = 1

四、代理服务优化

1.配置用户访问负载均衡的长连接
[root@lb01 ~]# vim /etc/nginx/nginx.conf
... ...
http {
	... ...
	keepalive_timeout  65;
	... ...
}
2.配置负载均衡代理到web的长连接
[root@lb01 ~]# vim /etc/nginx/conf.d/linux.keep.com.conf
upstream tomcat {
    server 172.16.1.7:8080;
    keepalive 8;				#配置开启长连接
}

server {
    listen 80;
    server_name linux.keep.com;

    location / {
        proxy_pass http://tomcat;
        proxy_http_version 1.1;			#代理带后端的http版本 1.0短链接 1.1长连接 2.0长连接  3.0长连接
        proxy_set_header Connection "";	 #清除请求头字段
        include proxy_params;
    }
}
3.配置web代理到php保持长连接
[root@web01 ~]# vim /etc/nginx/conf.d/linux.wp.com.conf 
upstream php_server {
    server 127.0.0.1:9000;
}

server {
    listen 80;
    server_name linux.wp.com;

    location / {
        root /code/wordpress;
        index index.php;
    }

    location ~* \.php$ {
        fastcgi_pass php_server;
        fastcgi_param SCRIPT_FILENAME /code/wordpress/$fastcgi_script_name;
        fastcgi_param HTTPS on;
        fastcgi_keep_conn on;
        include fastcgi_params;
    }
}
4.代理优化配置
[root@lb01 ~]# cat /etc/nginx/proxy_params 
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 60s;
proxy_read_timeout 60s;
proxy_send_timeout 60s;
proxy_buffering on;
proxy_buffer_size 8k;
proxy_buffers 8 8k;
proxy_next_upstream http_500 http_502 http_503 http_504;

五、静态资源优化

1.静态资源

在这里插入图片描述

2.静态资源缓存
#响应头部
cache-control: max-age=315360000
expires: Wed, 09 Nov 2050 02:40:58 GMT
last-modified: Sat, 18 Apr 2020 15:23:09 GMT
ETag: "5fd6c1d9-7cda"

#请求头部
If-None-Match: "5fd6c1d9-7cda"
If-Modified-Since: Sat, 18 Apr 2020 15:23:09 GMT

1)浏览器缓存原理

1.浏览器先去查看响应头部的 cache-control
2.如果没有超过缓存时间,直接返回浏览器缓存的内容
3.如果 cache-control 设置为 no-cache,浏览器会继续去读取 expires
4.如果没有到达 expires 设置的时间,那么浏览器会读取缓存
5.如果 cache-control 和 expires 都没有设置
6.浏览器会去查看服务器上面的 ETag
7.服务器上如果有 ETag,那么浏览器会拿着 If-None-Match 与其对比,如果值相同,那么走缓存
8.如果 ETag 与 If-None-Match 不同,浏览器会读取服务器上的 last-modified
9.服务器上如果有 last-modified,那么浏览器会拿着 If-Modified-Since 与其对比,如果值相同,那么走缓存
10.如果 last-modified 与 If-Modified-Since 不同,则不走缓存,需要重新到服务器上获取数据并返回

2)浏览器缓存参数含义
cache-control:缓存控制,记录的是文件的保留时长
expires:缓存到期时间
ETag:服务器上保留的文件的唯一识别符
If-None-Match:浏览器上上保留的文件的唯一识别符
last-modified:服务器上保留的文件的最后修改时间
If-Modified-Since:浏览器上保留的文件的最后修改时间

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值