我們的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,還是可以顯著提升響應速度的,當然這隻是我們自己環境下測試的情況,僅作參考。
评论