Nginx 是俄羅斯人編寫的十分輕量級的 HTTP 服務器,Nginx,它的發音爲「engine X」,是一個高 性能的 HTTP 和反向代理服務器,同時也是一個 IMAP/POP3/SMTP 代理服務器.Nginx 是由 俄羅斯人 Igor Sysoev 爲俄羅斯訪問量第二的 Rambler.ru 站點開發. Nginx 以事件驅動(epoll)的方式編寫,因此有很是好的性能,同時也是一個很是高效的反 向代理、負載平衡。可是 Nginx 並不支持 cgi 方式運行,緣由是能夠減小所以帶來的一些程 序上的漏洞。因此必須使用 FastCGI 方式來執行 PHP 程序。 因爲 Nginx 自己的一些優勢,輕量,開源,易用,愈來愈多的公司使用 nginx 做爲本身公司 的 web 應用服務器,本文詳細介紹 nginx 源碼安裝的同時並對 nginx 進行優化配置。javascript
1、Nginx 的優化php
一、編譯安裝前優化 編譯前的優化主要是用來修改程序名等等,目的更改源碼隱藏軟件名稱和版本號css
安裝 zlib-devel、pcre-devel 等依賴包html
[root@www ~]# yum -y install gcc gcc-c++ make libtool zlib zlib-devel pcre pcre-devel openssl openssl-develjava
下載 nginx 的源碼包:http://nginx.org/downloadnode
解壓源碼包:mysql
[root@www ~]# tar zxf nginx-1.10.2.tar.gz [root@www ~]# cd nginx-1.10.2/nginx
#####################c++
隱藏軟件名稱和版本號web
[root@www nginx-1.10.2]# vim src/core/nginx.h
//此行修改的是你想要的版本
#define NGINX_VERSION "1.10.2" //第 13 行
//此行修改的是你想修改的軟件名稱
#define NGINX_VER "nginx/" NGINX_VERSION //第 14 行
修改上面的信息,便可更改 nginx 顯示版本。例如:(curl –I 可看到,請求頭和響應頭顯示)
#define NGINX_VERSION "7.0"
#define NGINX_VER "ljl/" NGINX_VERSION
二、安裝 ngnix
[root@www ~]# groupadd www #添加 www 組
[root@www ~]# useradd -g www www -s /sbin/nologin #建立nginx運行帳戶www並加入到 www 組,不容許 www 用戶直接登陸系統
[root@www nginx-1.10.2]# ./configure --prefix=/usr/local/nginx(版本號可刪除) --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module --with-pcre --with-http_ssl_module --with-http_gzip_static_module --user=www --group=www
[root@www nginx-1.10.2]# make && make install
[root@www nginx-1.10.2]# ln -s /usr/local/nginx1.10/sbin/nginx /usr/local/sbin/
[root@www nginx-1.10.2]# nginx -t
啓動 nginx [root@www nginx-1.10.2]# nginx
[root@www nginx-1.10.2]# netstat -anpt | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 9834/nginx: master
三、nginx 配置項優化
[root@www ~]# ps -ef | grep nginx
root 9834 1 0 22:36 ? 00:00:00 nginx: master process nginx
www 9953 9834 0 22:43 ? 00:00:00 nginx: worker process
在這裏咱們還能夠看到在查看的時候,work 進程是 nginx 程序用戶,可是 master 進程仍是 root,其中,master 是監控進程,也叫主進程,work 是工做進程,部分還有 cache 相關進程, 關係如圖:
能夠直接理解爲 master 是管理員,work 進程纔是爲用戶提供服務的!
(1):Nginx 運行工做進程個數,通常咱們設置 CPU 的核心或者核心數 x2
若是不瞭解 cpu 的核數能夠查看cat/proc/cpuinfo 文 件#grep ^processor /proc/cpuinfo | wc -l
[root@www ~]# vi /usr/local/nginx1.10/conf/nginx.conf
worker_processes 4;
[root@www ~]# /usr/local/nginx1.10/sbin/nginx -s reload
[root@www ~]# ps -aux | grep nginx | grep -v grep
root 9834 0.0 0.0 47556 1948 ? Ss 22:36 0:00 nginx: master process nginx
www 10135 0.0 0.0 50088 2004 ? S 22:58 0:00 nginx: worker process
www 10136 0.0 0.0 50088 2004 ? S 22:58 0:00 nginx: worker process
www 10137 0.0 0.0 50088 2004 ? S 22:58 0:00 nginx: worker process
www 10138 0.0 0.0 50088 2004 ? S 22:58 0:00 nginx: worker process
Nginx 運行 CPU 親和力
好比 2 核配置 worker_processes 2;
worker_cpu_affinity 01 10;
worker_processes 最多開啓 8 個,8 個以上性能提高不會再提高了,並且穩定性變得更低, 因此 8 個進程夠用了。
Nginx 最多能夠打開文件數
這個指令是指當一個 nginx 進程打開的最多文件描述符數目,理論值應該是最多打開文件數 (ulimit -n)與 nginx 進程數相除,可是 nginx 分配請求並非那麼均勻,因此最好與 ulimit -n 的值保持一致。
注: 文件資源限制的配置能夠在/etc/security/limits.conf 設置,針對 root/user 等各個用戶或者* 表明全部用戶來設置。
* soft nofile 65535
* hard nofile 65535
用戶從新登陸生效(ulimit -n)
2、部署 LNMP (部署環境需求:nginx和php在一臺,再開啓一臺apache作防盜鏈)
一、安裝 php
(1)解決依賴關係 [root@www ~]# yum -y install libxml2-devel libcurl-devel openssl-devel bzip2-devel
安裝 libmcrypt
[root@www ~]# tar zxf libmcrypt-2.5.7.tar.gz
[root@wwwr ~]# cd libmcrypt-2.5.7/
[root@www libmcrypt-2.5.7]# ./configure --prefix=/usr/local/libmcrypt && make && make install
(2)編譯安裝 php
[root@www ~]# tar zxf php-5.6.27.tar.gz
[root@www ~]# cd php-5.6.27/
[root@www php-5.6.27]# ./configure --prefix=/usr/local/php5.6 --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-openssl --enable-fpm --enable-sockets --enable-sysvshm --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --with-mhash --with-mcrypt=/usr/local/libmcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --enable-maintainer-zts
[root@www php-5.6.27]# make && make install
(3)提供 php 配置文件
[root@www php-5.6.27]# cp php.ini-production /etc /php.ini
(4)爲 php-fpm 提供腳本
[root@www php-5.6.27]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@www php-5.6.27]# chmod +x /etc/init.d/php-fpm
[root@www php-5.6.27]# chkconfig --add php-fpm
[root@www php-5.6.27]# chkconfig php-fpm on
(5)提供 php-fpm 配置文件並編輯: # cp /usr/local/php5.6/etc/php-fpm.conf.default /usr/local/php5.6/etc/php-fpm.conf
[root@www ~]# vi /usr/local/php5.6/etc/php-fpm.conf
修改內容以下:
pid = run/php-fpm.pid
listen = 0.0.0.0:9000
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
啓動 php-fpm 服務:
[root@www ~]# service php-fpm start
Starting php-fpm done
[root@www ~]# netstat -anpt | grep php-fpm
tcp 0 0 0.0.0.0:9000 0.0.0.0:* LISTEN 25456/php-fpm: mast
[root@www ~]# firewall-cmd --permanent --add-port=9000/tcp
success
[root@www ~]# firewall-cmd --reload
Success
下面是nginx.conf的一個完整配置文件
user www www;
worker_processes 2;
worker_cpu_affinity 01 10;
error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
pid logs/nginx.pid;
events {
use epoll;
worker_connections 65535;
multi_accept on;
}
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 65;
tcp_nodelay on;
client_header_buffer_size 4k;
open_file_cache max=102400 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 1;
client_header_timeout 15;
client_body_timeout 15;
reset_timedout_connection on;
send_timeout 15;
server_tokens off;
client_max_body_size 10m;
fastcgi_connect_timeout 600;
fastcgi_send_timeout 600;
fastcgi_read_timeout 600;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
fastcgi_temp_path /usr/local/nginx/nginx_tmp;
fastcgi_intercept_errors on;
fastcgi_cache_path /usr/local/nginx/fastcgi_cache levels=1:2
keys_zone=cache_fastcgi:128m inactive=1d max_size=10g;
gzip on;
gzip_min_length 2k;
gzip_buffers 4 32k;
gzip_http_version 1.1;
gzip_comp_level 6;
gzip_types text/plain text/css text/javascript application/json application/javascript
application/x-javascript application/xml;
gzip_vary on;
gzip_proxied any;
server {
listen 80;
server_name www.benet.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location ~* ^.+\.(jpg|gif|png|swf|flv|wma|wmv|asf|mp3|mmf|zip|rar)$ {
valid_referers none blocked www.benet.com benet.com;
if ($invalid_referer) {
#return 302 http://www.benet.com/img/nolink.jpg;
return 404;
break;
}
access_log off;
}
location / {
root html;
index index.php index.html index.htm;
}
location ~* \.(ico|jpe?g|gif|png|bmp|swf|flv)$ {
expires 30d;
#log_not_found off;
access_log off;
}
location ~* \.(js|css)$ {
expires 7d;
log_not_found off;
access_log off;
}
location = /(favicon.ico|roboots.txt) {
access_log off;
log_not_found off;
}
location /status {
stub_status on;
}
location ~ .*\.(php|php5)?$ {
root html;
fastcgi_pass 192.168.254.129:9000; (phpip)
fastcgi_index index.php;
include fastcgi.conf;
fastcgi_cache cache_fastcgi;
fastcgi_cache_valid 200 302 1h;
fastcgi_cache_valid 301 1d;
fastcgi_cache_valid any 1m;
fastcgi_cache_min_uses 1;
fastcgi_cache_use_stale error timeout invalid_header http_500;
fastcgi_cache_key http://$host$request_uri;
}
#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;
}
}
}
圖片概述:
重載 nginx 服務
[root@www ~]# /usr/local/nginx1.10/sbin/nginx -s reload
3、驗證、壓力測試
(1)驗證防盜鏈
使用 apache 作爲一個測試站點,域名爲 www.test.com,在測試頁上作一個超連接,連接 nginx
站點的一張圖片 (nginx下放張照片)
[root@centos1 ~]# cat /var/www/html/index.html
<a href="http://www.benet.com/1.jpg">link</a>
從上圖能夠看到防盜鏈設置生效了
(2)驗證 gzip 功能
使用谷歌瀏覽器測試訪問,以下圖顯示結果:(提示:在訪問測試頁以前按 F12 鍵)
在php上作個測試頁面:
用戶訪問html.php文件,在上圖中content-encoding:gzip代表響應給用戶的數據是壓縮傳輸。
(3)壓力測試
安裝 httpd-tools 軟件包
[root@www ~]# yum -y install httpd-tools
[root@www ~]# ab -c 500 -n 50000 http://www.benet.com/index.html
(內容簡述)
第二次壓力測試,比較兩次的差別
[root@www ~]# ab -c 1000 -n 100000 http://www.benet.com/index.html
(內容簡述)
(5)xcache 加速 php
安裝 xcache
wget http://xcache.lighttpd.net/pub/Releases/3.2.0/xcache-3.2.0.tar.gz #下載
[root@www ~]# tar zxf xcache-3.2.0.tar.gz #解壓
[root@www ~]# cd xcache-3.2.0/ #進入安裝目錄
[root@www xcache-3.2.0]# /usr/local/php5.6/bin/phpize #用 phpize 生成 configure 配置文件
[root@www xcache-3.2.0]# ./configure --enable-xcache --enable-xcache-coverager --enable-xcache-optimizer --with-php-config=/usr/local/php5.6/bin/php-config #配置
[root@www xcache-3.2.0]# make && make install #編譯、安裝
2)建立 xcache 緩存文件
# touch /tmp/xcache
# chmod 777 /tmp/xcache
3)拷貝 xcache 後臺管理程序到網站根目錄
[root@www xcache-3.2.0]# cp -r htdocs/ /usr/local/nginx/html/xcache (版本號已刪除)
4)配置 php 支持 xcache
vi / etc/php.ini #編輯配置文件,在最後一行添加如下內容
[xcache-common]
extension = /usr/local/php5.6/lib/php/extensions/no-debug-zts-20131226/xcache.so
[xcache.admin]
xcache.admin.enable_auth = Off
[xcache]
xcache.shm_scheme ="mmap"
xcache.size=60M
xcache.count =1
xcache.slots =8K
xcache.ttl=0
xcache.gc_interval =0
xcache.var_size=64M
xcache.var_count =1
xcache.var_slots =8K
xcache.var_ttl=0
xcache.var_maxttl=0
xcache.var_gc_interval =300
xcache.test =Off
xcache.readonly_protection = Off
xcache.mmap_path ="/tmp/xcache"
xcache.coredump_directory =""
xcache.cacher =On
xcache.stat=On
xcache.optimizer =Off
[xcache.coverager]
xcache.coverager =On
xcache.coveragedump_directory =""
測試
service php-fpm restart #重啓 php-fpm
瀏覽器打開網站根目錄下面的 xcache
能夠看到此頁面:
進nginx主配置文件:(註釋掉)
測試對 php 動態頁面的壓力測試