我们的Web服务器使用Apache有十年了,一直使用LAMP架构来做网站。很早也知道Nginx,据说性能上有明显优势,但怕麻烦就一直没有尝试。
最近有个网站在与某互联网大厂做合作对接,对方要求我们的API接口速度上达到99分位在300ms以内、可靠性上达到99.9%以上,我们初期测试是很难达到的,后来商议了各种措施来优化和保障,其中就说到使用一个单独的Nginx为对方提供API接口。
于是4月初就在服务器上安装了一套Nginx,测试速度还是有明显改善的,而且与其它网站使用的Apache独立开来,在稳定性上也更有保证,下面来记录一下安装和使用的一些要点。
1、下载:
去Nginx官方网站下载最新的稳定版本,当时是1.19.9,可以进入下载页面查看和下载。
我是直接在/root目录下用 wget https://nginx.org/download/nginx-1.19.9.tar.gz 下载,然后 tar -xzf nginx-1.19.9.tar.gz 解压到 /root/nginx-1.19.9/
4096 Apr 5 22:35 auto
310598 Mar 30 22:47 CHANGES
474012 Mar 30 22:47 CHANGES.ru
4096 Apr 5 22:35 conf
2590 Mar 30 22:47 configure
4096 Apr 5 22:35 contrib
4096 Apr 5 22:35 html
1397 Mar 30 22:47 LICENSE
438 Apr 6 11:12 Makefile
4096 Apr 5 22:35 man
4096 Apr 6 11:13 objs
49 Mar 30 22:47 README
4096 Apr 5 22:35 src
2、安装:
cd nginx-1.19.9 ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.35 make make install
可能需要在此前安装一些其它软件,例如:
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
以及PCRE(作用是让 Nginx 支持 Rewrite 功能)。
安装好了以后,可以运行
/usr/local/nginx/sbin/nginx -v 来查看正在运行的Nginx的版本号
3、配置:
修改/usr/local/nginx/conf/nginx.conf
我们使用的例子:
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
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;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
gzip on;
server {
listen 8080;# http的默认80端口被apache占用了,这里使用8080
server_name test.example.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#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;
#}
# 设置PHP支持: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 $document_root$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 8443 ssl;# https的默认443端口被apache占用了,这里使用8443
server_name test.example.com;
#ssl_certificate cert.pem;
#ssl_certificate_key cert.key;
#ssl_certificate /etc/letsencrypt/live/example.com/cert.pem;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;# 注意这里需要使用fullchain.pem而不是上面的cert.pem
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
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;
}
# 设置PHP支持: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 $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
}
检查配置文件nginx.conf的正确性命令:/usr/local/nginx/sbin/nginx -t
4、启动 Nginx:
Nginx 启动命令如下:/usr/local/nginx/sbin/nginx
然后用浏览器访问https://test.example.com:8443 就可以看到/usr/local/nginx/html/index.html中的欢迎页面:

其它几个命令:
/usr/local/webserver/nginx/sbin/nginx -s reload # 重新载入配置文件 /usr/local/webserver/nginx/sbin/nginx -s reopen # 重启 Nginx /usr/local/webserver/nginx/sbin/nginx -s stop # 停止 Nginx
5、其它:
Nginx上运行PHP是需要把PHP作为一个单独的服务,安装php-fpm模块,这是暂时不详细记录。
为了SSL证书的获取、更新,还是需要在Apache中设置对应域名的80、443端口http和https站点,以便Lets Encrypt的certbot运行验证。
6、测试速度:
我们自己测试apache的一次访问返回时间大约是50ms,而nginx的一次返回时间大约是10ms,还是可以显著提升响应速度的,当然这只是我们自己环境下测试的情况,仅作参考。
评论