IO複用
http://nginx.org/
Http服務器
反向代理 reverse proxy
http
C10k
單進程;阻塞
多進程;每個進程響0應一個請求
進程量大,進程切換次數過多
每個進程的地址空間是獨立,很多空間是重複的數據,所以內存使用效率較低
線程:thread,
Light Weight Process, LWP
每個線程響應一個請求
線程仍然切換:屬於輕量級
同一個 進程的線程可以共享進程的諸多資源,比如打開的文件
對內存的需求較之進程略有下降
快速切換時候會帶來線程抖動
多進程多線程
多線程:N個請求
一個線程響應多個請求
多路IO,IO複用;
blocking I/O 阻塞I/O
nonblocking I/o 非阻塞I/O
I/O multiplexing I/O複用
signal driven I/O 信號驅動的I/O
asynchronous I/O 異步I/O
nginx優勢:
支持A I/O
支持內存映射 mmap
支持事件驅動event-driven
httpd:
MPM
prefork:一個進程響應一個請求,最多併發1024個
worker:一個線程顯影一個請求,多進程,一個進程生成多個線程
event:基於事件驅動
keepalived+ningx:實現高可用
corosync+ningx
nginx:
web服務器
反向代理
web
Tengine
varnish,squid
nginx:cache(disk)
httpd:cache(disk,memory)
memcached
ningx熱部署:平滑升級
主進程主要完成一下工作
1.讀取並驗證配置信息
2.創建,綁定及關閉套接字
3.啓動,終止及維護worker進程的個數
4.無需終止服務而重新配置工作特性
5.控制非中斷式程序升級,啓用新的二進制程序並在需要時回滾至老版本;
6.重新打開日誌文件,實現日誌滾動
7.編譯嵌入式perl腳本
worker進程主要完成任務包括
1.接收,傳入並處理來自客戶端的連接
2.提供反向代理及過濾功能
3.nginx任何能完成的其他任務
cache loader進程主要完成任務包括
1.檢查緩存存儲中的緩存對象
2.使用緩存元數據建立內存數據庫
cache manager進程的主要任務
1.緩存的失效及過期檢查
Nginx的配置有着幾個不同的上下文:main, http, server, upstream和location(還有實現郵件服務反響代理的mail)。
配置語法的格式和定義方式遵循所謂的c風格,因此支持嵌套,還有着邏輯清晰並易於創建,閱讀和維護等優勢
支持事件驅動的I/O框架:kqueue epoll /dev/poll
支持sendfile儘可能避免數據拷貝操作(避免數據在用戶空間和內存空間來回拷貝)
[root@localhost nginx-1.10.3]# yum info openssl-devel查看openssl-屬於哪個組
[root@localhost nginx-1.10.3]# rpm -qi openssl-devel
# yum groupinfo "Compatibility libraries" 查看某個組的關聯包
yum -y groupinstall 'Development tools'
yum -y groupinstall 'Server Platform Development'
yum -y groupinstall 'Compatibility libraries' 提供兼容庫
yum -y groupinstall 'Desktop Platform Development'
1.確認系統時間比軟件包時間靠後。
[root@localhost ~]# date -s 20170213
Mon Feb 13 00:00:00 EST 2017
[root@localhost ~]# date -s 20:45:50
[root@localhost ~]# hwclock --set --date="02/13/17 20:48:00"
[root@localhost ~]# tar -xf nginx-1.10.3.tar.gz
[root@localhost ~]# du -sh nginx-1.10.3
6.2Mnginx-1.10.3
[root@localhost nginx-1.10.3]# groupadd -r -g 108 nginx
[root@localhost nginx-1.10.3]# useradd -r -g 108 -u 108 nginx
[root@localhost nginx-1.10.3]# yum -y install gd.i686
[root@localhost nginx-1.10.3]# yum -y install 'pcre-devel'
[root@localhost nginx-1.10.3]# ./configure --help |less
./configure \
--prefix=/usr \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/tmp/nginx/client/ \
--http-proxy-temp-path=/var/tmp/nginx/proxy/ \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
--http-scgi-temp-path=/var/tmp/nginx/scgi \
--with-pcre \
--with-file-aio \
--with-http_p_w_picpath_filter_module
make && make install
#########################################################
啓動腳本:
[root@localhost nginx-1.10.3]# vim /etc/rc.d/init.d/nginx
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
}
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
sleep 1
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
############################################################
添加執行權限和自啓動
[root@localhost nginx-1.10.3]# chkconfig --add nginx
[root@localhost nginx-1.10.3]# chmod +x /etc/rc.d/init.d/nginx
[root@localhost nginx-1.10.3]# chkconfig --list nginx
nginx 0:off1:off2:off3:off4:off5:off6:off
[root@localhost nginx-1.10.3]# chkconfig nginx on
[root@localhost nginx-1.10.3]# service nginx start
Starting nginx: [ OK ]
[root@localhost nginx-1.10.3]# ls /var/tmp/nginx/
client fcgi proxy scgi uwsgi
在/usr/html/下存放了測試頁,如果不存在可以cp 編譯目錄下的html的文件直接到/usr/html
###############################################################################
配置文件:
[root@localhost nginx-1.10.3]# cd /etc/nginx/
[root@localhost nginx]# ls
fastcgi.conf koi-utf nginx.conf uwsgi_params
fastcgi.conf.default koi-win nginx.conf.default uwsgi_params.default
fastcgi_params mime.types scgi_params win-utf
fastcgi_params.default mime.types.default scgi_params.default
#.default結尾的都是nginx中默認提供的配置文件
mime 多用途互聯網郵件擴展,主要靠這個識別非文本文檔
fastcgi.conf和fastcgi_params 是用於實現fastcgi功能的,兩個文件通常只用一個
[root@localhost nginx]# cp nginx.conf nginx.conf.back
[root@localhost nginx]# vim nginx.conf
worker_processes 1; 啓動的worker的線程數,通常跟cpu個數相關,
如果是cpu密集型應用爲主,如SSL或壓縮應用,則worker數應與CPU數相同;如果負載以IO密集型爲主,如響應大量內容給客戶,則
worker數應該爲CPU個數的1.5或2倍
events {
worker_connections 1024;
}定義事件驅動中每個worker最大的連接數
http段:
include mime.types; 指定所包含的文件
default_type application/octet-stream; 指定默認支持的類型
sendfile on; 定義是否開機sendfile(開啓以後會盡量避免數據在用戶空間和內核空間來回複製)
#tcp_nopush on; 是否推送
Nagle算法:如果發送端發送多次少量字符的數據包,則第一次發送出去以後 剩下的 先緩存下來而不立即發送
,直到接收端發送對前一個數據報文的ACK確認,或者當前字符數據緊急數據,或者積攢到了一定量的數據後再向外發送
TCP中Nagle算法默認是啓用的,但它並不是適合任何場景,對telnet,rlogin這樣的遠程登錄比較合適,但是在某些應用場景下又需要關
閉它,因爲會導致客戶端運行很慢,降低了實時響應速度
keepalive_timeout 65; 使用長連接,並指定超時時間
#gzip on; 對響應給用戶的數據是否先壓縮
server段:
每一個server{}定義一個虛擬主機
server {
listen 80; 監聽端口
server_name localhost; 基於名稱的虛擬主機,IP不同就是基於IP的虛擬主機
location / { 基於URI路勁來定義訪問路徑
root html; 相對路徑定義root在/usr/html下
index index.html index.htm; 定義index在/usr/html/index.html 或者/usr/html/index.htm
deny 192.168.1.1; 拒絕某個地址訪問
}
error_page 500 502 503 504 /50x.html; 定義如果返回的錯誤代碼是404則返回的主頁是usr/html/50x.html;
#####################################################################
location URI {} 表示花括號中所定義的屬性對這個URI所有文件都生效,優先級最低
location = URI {}精確匹配只對當前路徑生效 ,優先級最高
~ #表示此處URI可以使用正則表達式,區分大小寫, 優先級第三
~* #表示此處URI可以使用正則表達式,不區分大小寫,優先級第三
^~ #表示不使用正則表達式,優先級第二
##############################
定義訪問控制,把規則小的放在最前面,默認是允許訪問的
location /{
deny 192.168.1.1; 拒絕某個地址訪問
allow 192.168.1.0/24; 允許某個網段
allow 10.1.1.0/16;
allow 2620:100:e000::8001;
deny all; 拒絕所有
}
##########################################################################
利用auth_basic實現http認證
location /{
auth_basic "Restricted"; 指定認證信息
auth_basic_user_file htpasswd; 指定認證文件路徑和文件名
}
[root@localhost ~]# yum -y install 安裝httpd爲了使用htpasswd工具
[root@localhost ~]# chkconfig --list httpd 確認是關閉的
[root@localhost ~]# htpasswd -c -m /etc/nginx/.users tom 第一次使用需要-c創建文件,-m指定md5加密方式
New password:
Re-type new password:
Adding password for user tom
[root@localhost ~]#
location / {
root html;
index index.html index.htm;
auth_basic "Restricted Area...";
auth_basic_user_file /etc/nginx/.users;
}
service nginx restart
###########################################################################
autoindex on:在沒有主頁的情況下把所有文件都列出來
autoindex_exac_size on|off 顯示每一個文件大小的精確值
autoindex_localtime 顯示當前操作系統的本地時間
location / {
root html;
index index.html index.htm;
auth_basic "Restricted Area...";
auth_basic_user_file /etc/nginx/.users;
autoindex on;
}
####################################################################
stub_status on; 定義狀態頁面
location /status {
stub_status on;
access_log off;
allow ...
deny all;
}
192.168.1.10/status
Active connections: 3 當前的活動連接數
server accepts handled requests
3 3 3 已經接受的連接數,已經處理過的連接數,已經處理過的請求數
Reading: 0 Writing: 1 Waiting: 2
Reading: 正在讀取其首部的請求的個數
Writing: 正在讀取其主體的請求個數,正在處理着其請求內容的個數或者正在向客戶端發送響應的個數;
Waiting: 長連接模式的保持的連接個數
###################################################################
SSL: :.,$ s/^\([[:space:]]*\)#/\1/g
server {
listen 443 ssl;
server_name localhost;
ssl_certificate /etc/nginx/ssl/nginx.crt; 證書
ssl_certificate_key /etc/nginx/ssl/nginx.key; 私鑰
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m; 會話超時時間
ssl_ciphers HIGH:!aNULL:!MD5; 加密算法
ssl_prefer_server_ciphers on; 是否允許服務端選擇傾向的加密算法
location / { 定義對應的網頁文件在什麼路徑下
root /web/ssl;
index index.html index.htm;
}
}
搭建CA:
[root@localhost nginx]# vim /etc/pki/tls/openssl.cnf
dir = /etc/pki/CA
[root@localhost nginx]# cd /etc/pki/CA/
[root@localhost CA]# ls
certs crl newcerts private
[root@localhost CA]# ls private/
[root@localhost CA]# (umask 077;openssl genrsa 2048 >private/cakey.pem)
[root@localhost CA]# openssl req -new -x509 -key private/cakey.pem -out cacert.pem
[root@localhost CA]# touch serial
[root@localhost CA]# echo 01 >serial
[root@localhost CA]# touch index.txt
[root@localhost CA]# cd /etc/nginx/
[root@localhost nginx]# mkdir ssl && cd ssl
[root@localhost ssl]# (uamsk 077;openssl genrsa 1024 >nginx.key)
[root@localhost ssl]# openssl req -new -key nginx.key -out nginx.csr
[root@localhost ssl]# openssl ca -in nginx.csr -out nginx.crt -days 3650
[root@localhost ssl]# service nginx restart
https://192.168.1.10/
###################################################################
:set nohlsearch
定義基於主機名的虛擬主機:
server {
listen 80;
server_name www.mylinux.com;
--------------------------------------------
server {
listen 80;
server_name www.mylinux2.com;
location / {
root /web/www;
index test test.htm;
}
}
# another virtual host using mix of IP-, name-, and port-based configuration
---------------------------------------------------------
nginx -t測試配置文件語法
#########################################
實現lnmp(lemp)
MySQL+PHP
fastCGI:
php-fpm:
127.0.0.1:9000
編譯:php
mysql:
[root@localhost ~]# useradd -r mysql
[root@localhost ~]# mkdir /mydata/data -pv
mkdir: created directory `/mydata'
mkdir: created directory `/mydata/data'
[root@localhost ~]# chown -R mysql.mysql /mydata/data
[root@localhost local]# tar -xf mysql-5.5.28-linux2.6-x86_64.tar.gz
[root@localhost local]# ln -sv mysql-5.5.28-linux2.6-x86_64 mysql
[root@localhost local]# chown -R mysql.mysql mysql/*
[root@localhost local]# chmod -R 750 mysql/*
[root@localhost local]# fdisk /dev/sdb
8e
[root@localhost ~]# partprobe /dev/sdb
[root@localhost ~]# pvcreate /dev/sdb1
[root@localhost ~]# vgcreate myvg /dev/sdb1
[root@localhost ~]# lvcreate -n mydata -L 10G myvg
[root@localhost ~]# vim /etc/fstab
/dev/myvg/mydata /mydata ext3 defaults 0 0
[root@localhost ~]# chmod o-rx /mydata/data
[root@localhost mysql]# scripts/mysql_install_db --user=mysql --datadir=/mydata/data
[root@localhost mysql]# chown -R root /usr/local/mysql/*
[root@localhost mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[root@localhost mysql]# chkconfig --add mysqld
[root@localhost support-files]# cp my-large.cnf /etc/my.cnf
[root@localhost support-files]#vim /etc/my.cnf
datadir = /mydata/data
innodb_file_per_table = ON
log-bin = master-bin
[root@localhost ~]# service mysqld start
[root@mylinux support-files]# vim /etc/profile.d/mysql.sh
export PATH=$PATH:/usr/local/mysql/bin
[root@localhost ~]# vim /etc/man.config
MANPATH /usr/man
MANPATH /usr/share/man
MANPATH /usr/local/man
MANPATH /usr/local/share/man
MANPATH /usr/X11R6/man
MANPATH /usr/local/mysql/man 新增一條
[root@localhost ~]# vim /etc/ld.so.conf.d/mysql.conf
/usr/local/mysql/lib
[root@localhost ~]# ldconfig -v 讓系統重新讀取
[root@localhost ~]# ls -l /etc/ld.so.cache 緩存到這個文件
-rw-r--r--. 1 root root 41662 Jul 27 17:15 /etc/ld.so.cache
[root@localhost mysql]# ln -sv /usr/local/mysql/include /usr/include/mysql
php:
[root@localhost php-5.4.13]# yum -y install bzip2-devel
[root@localhost php-5.4.13]# yum -y install libcurl-devel
[root@localhost ~]# tar -xf php-5.4.13.tar.gz
[root@localhost php-5.4.13]# ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --enable-fpm --enable-sockets --enable-sysvshm --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib-dir --with-libxml-dir=/usr --enable-xml --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --with-curl
[root@localhost php-5.4.13]# make && make install
爲php提供配置文件:
[root@localhost php-5.4.13]# cp php.ini-production /etc/php.ini
爲php-fpm提供Sysv init腳本,並將其添加至服務列表:
[root@localhost php-5.4.13]# cd /usr/local/php/etc/
[root@localhost etc]# ls
pear.conf php-fpm.conf.default
[root@localhost etc]# cp php-fpm.conf.default php-fpm.conf
[root@localhost etc]# vim php-fpm.conf
pm.max_children = 150 最多多少個
pm.start_servers = 8 啓動的時候啓動幾個
pm.min_spare_servers = 5 最少空閒爲幾個
pm.max_spare_servers = 10 最多空閒爲幾個
[root@localhost php-5.4.13]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@localhost php-5.4.13]# chmod +x /etc/init.d/php-fpm
[root@localhost php-5.4.13]# chkconfig --add php
[root@localhost php-5.4.13]# chkconfig --add php-fpm
[root@localhost php-5.4.13]# chkconfig php-fpm on
[root@localhost php-5.4.13]# service php-fpm start
Starting php-fpm done
[root@localhost php-5.4.13]# netstat -tnlp
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 113123/php-fpm
[root@localhost php-5.4.13]# ps aux |grep php
php啓動了8個子進程
整合nginx和php5
[root@localhost php-5.4.13]# vim /etc/nginx/nginx.conf
啓用:
location ~ \.php$ {
root /web/www;
fastcgi_pass 127.0.0.1:9000; #所有對.php頁面的訪問都以fastcgi的方式代理給127.0.0.1:9000這個主機處理
fastcgi_index index.php; #fastcgi的主頁爲index.php
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
}
[root@localhost php-5.4.13]# vim /etc/nginx/fastcgi_params
fastcgi_param GATEWAY_INTERFACE CGI/1.1; #CGI接口
fastcgi_param SERVER_SOFTWARE nginx; #服務器端軟件程序
fastcgi_param QUERY_STRING $query_string; #查詢方式
fastcgi_param REQUEST_METHOD $request_method; #請求方法
fastcgi_param CONTENT_TYPE $content_type; #內容類型
fastcgi_param CONTENT_LENGTH $content_length; #內容長度
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; #用戶請求的網頁頁面文件
fastcgi_param SCRIPT_NAME $fastcgi_script_name; #腳本名稱
fastcgi_param REQUEST_URI $request_uri; #啓動的是哪個URI
fastcgi_param DOCUMENT_URI $document_uri; #網頁路勁URI
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol; #協議版本
fastcgi_param REMOTE_ADDR $remote_addr; #客戶端地址
fastcgi_param REMOTE_PORT $remote_port; #客戶端端口
fastcgi_param SERVER_ADDR $server_addr; #服務器IP
fastcgi_param SERVER_PORT $server_port; #服務器端口
fastcgi_param SERVER_NAME $server_name; #服務器名字
[root@localhost php-5.4.13]# nginx -t
[root@localhost php-5.4.13]# service nginx reload
測試是否已經支持php
[root@localhost www]# vim index.php
<?php
phpinfo();
?>
~
並在所支持的主頁面格式中添加php格式的主頁,類似如下:
location / {
root html;
index index.php index.html index.htm;
}
而後重新載入nginx的配置文件:
# service nginx reload
、安裝xcache,爲php加速:
1、安裝
# tar xf xcache-2.0.0.tar.gz
# cd xcache-2.0.0
# /usr/local/php/bin/phpize
# ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config
# make && make install
安裝結束時,會出現類似如下行:
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-zts-20100525/
2、編輯php.ini,整合php和xcache:
首先將xcache提供的樣例配置導入php.ini
# mkdir /etc/php.d
# cp xcache.ini /etc/php.d
說明:xcache.ini文件在xcache的源碼目錄中。
接下來編輯/etc/php.d/xcache.ini,找到zend_extension開頭的行,修改爲如下行:
zend_extension = /usr/local/php/lib/php/extensions/no-debug-zts-20100525/xcache.so
注意:如果php.ini文件中有多條zend_extension指令行,要確保此新增的行排在第一位。
3、重新啓動php-fpm
# service php-fpm restart
六、補充說明
如果要在SSL中使用php,需要在php的location中添加此選項:
fastcgi_param HTTPS on;