总所周知,网络上咱们购买的服务器的性能各不相同,若是采用 Nginx 的默认配置的话,没法将服务器的所有性能优点发挥出来,咱们应该选择适合本身需求的配置。html
当咱们默认安装后 Nginx 后,咱们便获得 Nginx 的默认配置 nginx.conf ,这个文件默认放在 /usr/local/nginx/conf 中(若经过 apt-get install nginx 安装,则在 /etc/nginx/ 目录下),只需修改目录下的 nginx.conf 文件。nginx
以个人云主机为例(低流量配置:2核 CPU,2GB RAM,请求 ~1s),没办法,做为学生党,我穷(/_\)。ubuntu
实例文件以下:浏览器
user ubuntu ubuntu; # 建立用户和用户组 worker_processes 2; # 开启2个工做进程(2 核 CPU,至少应该为 CPU 的每个核分配一个工做进程) worker_priority 0; # worker 进程会在一个适当的优先级启动。值的范围为 -20(优先级最高)~19(优先权最低),设置的值不该该低于-5,由于它是内核进程的默认优先级 error_log logs/error.log error; log_not_found off; #该指令指定 Nginx 是否记录404错误。推荐设置为 off,然而不要在 server级别将 log_not_found off 的值设置为 off。 events { multi_accept on; worker_connections 128; } http { include mime.types; default_type application/octet_stream; sendfile on; server { listen 80; server_name localhost; location / { root html; index index.html index.html } error_page 500 502 503 504 /50x.html; location = /50x.html{ root html; } } }
Nginx 的默认安装包中,有一个简单的测试页,在 html 目录中(/usr/local/nginx/html/index.html),咱们能够经过访问该网页来测试咱们的配置是否生效。服务器
改完 nginx.conf 文件后,咱们直接浏览器中访问服务器器 IP 地址,若是浏览器能正确输出下列界面,即证实咱们的配置文件是有效的,不然从新检查配置文件,修改配置,直到能正确访问。网络
接下来咱们能够来进行服务器性能测试,来评估服务器性能。app
这里我使用的是 httperf 模块,Ubuntu 自带,没有的话经过下列指令安装。dom
sudo apt-get install httperf
这里我以重复下载我服务器中的 index.html 为例,每秒300次,总共请求30000次。性能
httperf --server your domain --port 80 --uri /index.html --rate 300 --num-conn 30000 --num-call 1 --timeout 5
输出以下:测试
咱们能够在其中看到响应时间和成功请求的次数。固然咱们是但愿成功率为100%或响应时间为0ms 的,咱们能够不断测试,增长请求,适当调整配置,使成功率接近 100% ,这时候咱们的Nginx 配置就是比较完美的了。