Nginx簡介:Nginx是一款輕量級的Web 服務器/反向代理服務器及電子郵件(IMAP/POP3)代理服務器,並在一個BSD-like 協議下發行。其特點是占有內存少,並發能力強,事實上nginx的並發能力確實在同類型的網頁服務器中表現較好,中國大陸使用nginx網站用戶有:百度、京東、新浪、網易、騰訊、淘寶等。
Nginx的安裝:
1.依賴安裝
# yum -y install zlib zlib-devel openssl openssl--devel pcre pcre-devel
2.下載源碼包
www.nginx.org #官網地址,自己去找包
3.源碼安裝
./configure --user=www \ #worker進程運行用戶 --group=www \ #worker進程運行的組 --prefix=/usr/ \ #Nginx安裝的根路徑,所有其他的路徑都要依賴於改選項 --conf-path=/etc/nginx/nginx.conf \ #如果在命令行沒有指定配置文件,那么將會通過這里指定的路徑,Nginx將會去那里查找它的配置文件 --sbin-path=/usr/sbin/nginx \ #指定Nginx二進制文件的路徑。如果沒有指定,那么這個路徑會依賴於--prefix選項 --error-log-path=/var/log/nginx/nginx_error.log \ #指定錯誤文件的路徑,Nginx將會往其中寫入錯誤日志文件,除非有其他配置 --http-log-path=/var/log/nginx/nginx_access.log \ #http訪問日志的默認路徑 --pid-path=/usr/local/nginx/run/nginx.pid \ #指定的文件將會寫入Nginx master進程的pid,通常在/var/run下 --lock-path=/usr/local/nginx/lock/nginx \ #共享存儲器互斥鎖文件的路徑 --with-http_ssl_module \ #啟用 ngx_http_ssl_module 支持(使支持 https 請求,需已安裝openssl) --with-http_realip_module \ #啟用 ngx_http_realip_module 支持(這個模塊允許從請求標頭更改客戶端的 IP 地址值,默認為關) --with-http_addition_module \ #啟用 ngx_http_addition_module 支持(作為一個輸出過濾器,支持不完全緩沖,分部分響應請求) --with-http_sub_module \ #啟用 ngx_http_sub_module 支持(允許用一些其他文本替換nginx 響應中的一些文本) --with-http_dav_module \ #啟用ngx_http_dav_module支持(增加PUT,DELETE,MKCOL:創建集合,COPY 和 MOVE 方法)默認情況下為關閉,需編譯開啟 --with-http_flv_module \ #啟用 ngx_http_flv_module 支持(提供尋求內存使用基於時間的偏移量文件) --with-http_gzip_static_module \ #啟用 ngx_http_gzip_static_module 支持(在線實時壓縮輸出數據流) --with-http_stub_status_module \ #啟用ngx_http_stub_status_module支持(獲取nginx自上次啟動以來的工作狀態) --with-http_perl_module \ #Nginx配置能夠擴展使用Perl代碼。這個選項啟用這個模塊(然而使用這個模塊會降低性能) --with-mail \ #該選項用於啟用mail模塊,該模塊默認沒有被激活 --with-mail_ssl_module \ #為了代理任何一種類型的使用SSL/TLS的mail,激活該模塊 --with-pcre \ --http-client-body-temp-path=/var/tmp/nginx/client/ \ #從客戶端收到請求后,該選項設置的目錄用於作為請求體零食存放的目錄,如果WebDAV模塊啟用,那么推薦設置該路徑為同一文件系統上的目錄作為最終的目的地 --http-proxy-temp-path=/var/tmp/nginx/proxy \ #在使用代理后,通過該選項設置存放臨時文件路徑 --http-fastcgi-temp-path=/var/tmp/nginx/fcgi \ #設置FastCGI臨時文件的目錄 --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \ #設置uWSGI臨時文件的目錄 --http-scgi-temp-path=/var/tmp/nginx/scgi #設置SCGI臨時文件的目錄 #以下是完整的復制 ./configure --user=www --group=www --prefix=/usr/ --conf-path=/etc/nginx/nginx.conf --sbin-path=/usr/sbin/nginx --error-log-path=/var/log/nginx/nginx_error.log --http-log-path=/var/log/nginx/nginx_access.log --pid-path=/usr/local/nginx/run/nginx.pid --lock-path=/usr/local/nginx/lock/nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_gzip_static_module --with-http_stub_status_module --with-http_perl_module --with-mail --with-mail_ssl_module --with-pcre --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
4.安裝后配置
1.創建用戶
useradd -r www -s /bin/false -M
2創建目錄
mkdir -p /var/tmp/nginx/client/
3.修改nginx.conf
vi /etc/nginx/nginx.conf
#user nobody >> user www
3.創建啟動腳本腳本
vi /etc/rc.d/init.d/nginx
#!/bin/bash
#
# nginx - this script starts and stops the nginx daemin
#
# 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
# pidfile: /usr/local/nginx/logs/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"
lockfile=/var/lock/subsys/nginx
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
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
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
chmod +x /etc/rc.d/init.d/nginx
把nginx加入chkconfig,並設置開機啟動
chkconfig --add nginx
chkconfig nginx on
啟動、停止、重新加載的命令如下
service nginx start
service nginx stop
service nginx reload
Ngin -V 查看有關參數
腳本獲得方法:
#啟動腳本 wget http://www.centos.bz/wp-content/uploads/2011/07/nginx 改里面的相應文件位置即可
5.安裝常見問題
1.源碼編譯可能會出現
./configure: error: perl module ExtUtils::Embed is required
解決方法:
yum -y install perl-ExtUtils-Embed
2.使用nginx啟動腳本可能出現nginx: [emerg] getpwnam(“www”) failed
解決方法:
1)
nginx.conf中去掉user nobody注釋即可
2)
錯誤原因是沒有www這個用戶,加上就好了
useradd www
3.-bash: /etc/init.d/nginx: /bin/sh^M: bad interpreter: 沒有那個文件或目錄
解決方法:
查看腳本格式 :set ff
會顯示 fileformat=doc
改一下 :set ff=unix
保存執行
6.配置文件解讀
user www; #運行用戶
worker_processes 1; #啟動進程,通常設置成和cpu的數量相等
error_log /var/log/nginx/error.log; #全局錯誤日志
pid /usr/local/nginx/run/nginx.pid; #pid文件
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
#日志格式,以下具體介紹變量
$remote_addr 與$http_x_forwarded_for #用以記錄客戶端的 ip 地址;
$remote_user #用來記錄客戶端用戶名稱;
$time_local #用來記錄訪問時間與時區;
$request #用來記錄請求的 url 與 http 協議;
$status #用來記錄請求狀態;成功是 200;
$body_bytes_s ent #記錄發送給客戶端文件主體內容大小;
$http_referer #用來記錄從那個頁面鏈接訪問過來的;
#工作模式及連接數上限
events {
use epoll; #epoll 是多路復用 IO(I/O Multiplexing)中的一種方式,但是僅用於 linux2.6以上內核,可以大大提高 nginx 的性能
worker_connections 1024; #單個后台 worker process 進程的最大並發鏈接數
# multi_accept on;
}
#設定 http 服務器,利用它的反向代理功能提供負載均衡支持
http {
#設定 mime 類型,類型由 mime.type 文件定義
include /etc/nginx/mime.types;
default_type application/octet-stream;
#設定日志格式
access_log /usr/local/nginx/logs/access.log;
#sendfile 指令指定 nginx 是否調用 sendfile 函數(zero copy 方式)來輸出文件,對於普通應用,
#必須設為 on,如果用來進行下載等應用磁盤 IO 重負載應用,可設置為 off,以平衡磁盤與網絡 I/O 處理速度,降低系統的 uptime.
sendfile on;
#tcp_nopush on;
#連接超時時間
#keepalive_timeout 0;
keepalive_timeout 65;
tcp_nodelay on;
#開啟 gzip 壓縮
gzip on;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
#設定請求緩沖
client_header_buffer_size 1k;
large_client_header_buffers 4 4k;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
#設定負載均衡的服務器列表
upstream mysvr {
#weigth 參數表示權值,權值越高被分配到的幾率越大
server 192.168.8.1:3128 weight=5;
server 192.168.8.2:80 weight=1;
server 192.168.8.3:80 weight=6;
}
server {
#偵聽 80 端口
listen 80;
#定義使用 www.xx.com 訪問
server_name www.xxx.com;
#設定本虛擬主機的訪問日志
access_log /var/log/nginx/www.xxx.com_access.log access;
#默認請求
location / {
root /var/www/html/; #定義服務器的默認網站根目錄位置
index index.php index.html index.htm; #定義首頁索引文件的名稱
}
# 定義錯誤提示頁面
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /root;
}
#靜態文件,nginx 自己處理
location ~ ^/(images|javascript|js|css|flash|media|static)/ {
root /var/www/virtual/htdocs;
#過期 30 天,靜態文件不怎么更新,過期可以設大一點,如果頻繁更新,則可以設置得小一點。
expires 30d;
}
#PHP 腳本請求全部轉發到 FastCGI 處理. 使用 FastCGI 默認配置.
location ~ \.php$ {
root /root;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME/home/www/www$fastcgi_script_name;
include fastcgi_params;
}
#設定查看 Nginx 狀態的地址
location /NginxStatus {
stub_status on;
access_log on;
auth_basic "NginxStatus";
auth_basic_user_file conf/htpasswd;
}
#禁止訪問 .htxxx 文件
location ~ /\.ht {
allow host;
deny all;
}
}
}
Nginx簡單實戰
#基於域名的虛擬主機
server {
listen 80;
server_name www.Daniel.org Daniel.org;
access_log /var/log/nginx/Daniel/Daniel_access.log main;
error_log /var/log/nginx/Daniel/Daniel_error.log crit;
location / {
root html/www;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
#多域名就多個server段
#基於端口的只需要將listen修改端口即可,server_name也要修改成IP
#別名:
#上面例子中,server_name中有兩個域名,這個就是別名
#nginx狀態信息配置
server{
listen 80;
server_name status.Daniel.org;
location / {
stub_status on;
access_log off;
}
}
#瀏覽器訪問status.Daniel.org:
Active connections: 3 #正在處理的活動連接數
server accepts handled requests #server表示nginx啟動到現在共處理了幾個連接,accepts表示nginx啟動到現在共成功創建了幾次連接,請求丟失數據=(握手數-連接數),可以看出,本次狀態顯示沒有丟失請求,handled requests表示總共處理了幾次請求
9 9 192
Reading: 0 Writing: 1 Waiting: 2
#Reading:nginx讀取客戶端的Header信息數
#Writing:nginx返回給客戶端的Header信息數
#Waiting:nginx已經處理完正在等候下一次請求指令的駐留連接,開啟keep-alive的情況下,這個值等於active-(reading + writing)
#訪問控制權限實戰
server {
listen 80;
server_name www.etiantian.org etiantian.org;
access_log /var/log/nginx/etiantian/etiantian_access.log main;
error_log /var/log/nginx/etiantian/etiantian_error.log crit;
location / {
root html/www;
index index.html index.htm;
deny 172.16.50.173; #拒絕172.16.50.173
allow all; #允許全部,意思就是允許除了172.16.50.173的其他全部IP訪問
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
server{
listen 80;
server_name status.Daniel.org;
location / {
stub_status on;
access_log off;
allow 172.16.50.173; #只允許172.16.50.173,其他全部拒絕
deny all;
}
}
#也可以用網段表示:172.16.50.0/24
