內容摘要:
nginx 特點
nginx應用場合
nginx安裝
nginx主配置文件
nginx基於域名的虛擬主機
nginx基於域名的端口主機
nginx基於域名的IP主機
nginx配置負載均衡
nginx加入service添加啟動
nginx 特點
1、配置簡單,靈活,輕量。
2、高並發(靜態小文件),靜態幾萬的並發。
3、占用資源少。2W並發 開10個線程服務,內存消耗幾百M。
4、功能種類比較多(web,cache,proxy),每一個功能都不是特別強。
5、支持epoll模型。使得NGINX可以支持高並發!apache select 模型。
6、利用nginx 可以對IP限速,可以限制連接數。
7、nginx可以配合動態PHP(FASTCGI接口)
它所具備的其他WWW服務特性如下:
支持基於名字、端口以及IP的多虛擬主機站點:
支持rewrite模塊,支持URI重寫及正則表達式匹配:
支持http響應速率限制:
支持同一IP地址的並發連接或請求數限制
nginx應用場合
1、提供靜態服務(圖片,視頻服務),另一個lighttpd。並發:幾萬並發
html,js,css,.flv,jpg,gif等,類似lighttpd。
2、提供動態服務,nginx+fastcgi的方式運行php,jsp。
動態並發:500-1500
3、提供反向代理(proxy)服務,或者稱為負載均衡。日PV2000W 以下,並發1萬以下,都可以直接用NGINX 做代理。
4、緩存服務,類似SQUID,VARNISH。
nginx 虛擬主機
一個server標簽就是一個虛擬主機
1、基於域名的虛擬主機。通過域名來區分虛擬主機 ==》應用:外部網站
2、基於端口的虛擬主機。通過端口來區分虛擬主機 ==》應用:公司內部網站,網站的后台
3、基於IP的虛擬主機。幾乎不用。不支持ifconfig別名,配置文件可以。
nginx安裝
nginx網址 nginx.org
a、安裝PCRE(Perl Compatible Regular Expressions),peri兼容正則表達式,官方站點為www.pcre.org
先查看服務器的版本
cat /etc/redhat-release
uname -m
查看是否有pcre
rpm -qa pcre pcre-devel
安裝pcre
yum install pcre pcre-devel -y
b、SSL modules require the OpenSSL library
安裝openssl
yum install openssl openssl-devel -y
c、安裝nginx
創建虛擬用戶
useradd nginx -s /sbin/nologin -M
解壓文件
tar zxcf nginx-1.6.3.tar.gz
cd nginx-1.6.3
./configure --help 列出所有的可選項
./configure --prefix=/mnt/nginx-1.6.3 --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module
echo $? 判斷是否成功
0
然后 make 編譯安裝
make
make install
啟動nginx
/mnt/nginx-1.6.3/sbin/nginx
檢查是否啟動
ps -ef |grep nginx| grep -v grep 或者 ss -lntup | grep nginx
/mnt/nginx/sbin/nginx -V 來查看這個是如何編譯安裝的
部署一個簡單的web站點
cd /mnt/nginx-1.6.3
ls -l | grep -v temp
drwxr-xr-x. 2 root root 4096 12月 9 10:19 conf 配置
drwxr-xr-x. 2 root root 4096 12月 9 10:19 html 默認網站
drwxr-xr-x. 2 root root 4096 12月 9 10:29 logs 錯誤,訪問日志,PID
drwxr-xr-x. 2 root root 4096 12月 9 10:19 sbin 啟動命令
cd html
vim index.html 隨意修改后,保存
可以發現這個時候,nginx 默認首頁已經變化了
nginx主配置文件
#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;
------------------------------------------ Main區,Nginx核心功能模塊
events {
worker_connections 1024;
}
------------------------------------------ events區,Nginx核心功能模塊
http {
include mime.types;
default_type application/octet-stream;
sendfile on; #高效文件傳輸
keepalive_timeout 65; #超時時間
#一個server標簽就是一個虛擬主機
server {
listen 80;#監聽的端口
server_name localhost;#域名
#所有的請求,執行的參數 location區塊
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
#如果是下面的情況,優先執行下面
location = /50x.html {
root html;
}
}
}
nginx基於域名的虛擬主機
最小化配置文件,去掉#和空格
egrep -v "#|^$" nginx.conf.default >nginx.conf
vim nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.etiantian.org;
location / {
root html/www; #目錄為www
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name bbs.etiantian.org;
location / {
root html/bbs; #目錄為bbs
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
創建www和bbs的目錄,分別設置站點
mkdir mnt/nginx/html/{www,bbs} -p
echo "www" > www/index.html
echo "bbs" > bbs/index.html
#檢查語法是否OK
/mnt/nginx/sbin/nginx -t
hosts綁定本機IP后,測試成功
[root@localhost html]# curl www.etiantian.org
www
[root@localhost html]# curl bbs.etiantian.org
bbs
nginx基於端口的虛擬主機
修改server中的端口即可
server {
listen 80;
server_name www.etiantian.org;
location / {
root html/www; #目錄為www
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 8001;
server_name www.etiantian.org;
location / {
root html/www; #目錄為www
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
nginx基於IP的虛擬主機
用的少,這邊不描述
nginx配置負載均衡
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; upstream test.miaohr.com { server 118.31.239.163:80; server 118.31.239.163:8080; } server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; proxy_pass http://test.miaohr.com ; #proxy_set_header X-Real-IP $remote_addr; client_max_body_size 100m; }
這邊配置負載均衡池
upstream test.miaohr.com { server 118.31.239.163:80; server 118.31.239.163:8080; }
后面寫好地址即可
proxy_pass http://test.miaohr.com ;
nginx加入service添加啟動

1 #!/bin/bash 2 3 # nginx Startup script for the Nginx HTTP Server 4 5 # it is v.0.0.2 version. 6 7 # chkconfig: - 85 15 8 9 # description: Nginx is a high-performance web and proxy server. 10 11 # It has a lot of features, but it's not for everyone. 12 13 # processname: nginx 14 15 # pidfile: /var/run/nginx.pid 16 17 # config: /usr/local/nginx/conf/nginx.conf 18 19 nginxd=/usr/local/nginx/sbin/nginx 20 21 nginx_config=/usr/local/nginx/conf/nginx.conf 22 23 nginx_pid=/var/run/nginx.pid 24 25 RETVAL=0 26 27 prog="nginx" 28 29 # Source function library. 30 31 . /etc/rc.d/init.d/functions 32 33 # Source networking configuration. 34 35 . /etc/sysconfig/network 36 37 # Check that networking is up. 38 39 [ ${NETWORKING} = "no" ] && exit 0 40 41 [ -x $nginxd ] || exit 0 42 43 # Start nginx daemons functions. 44 45 start() { 46 47 if [ -e $nginx_pid ];then 48 49 echo "nginx already running...." 50 51 exit 1 52 53 fi 54 55 echo -n $"Starting $prog: " 56 57 daemon $nginxd -c ${nginx_config} 58 59 RETVAL=$? 60 61 echo 62 63 [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx 64 65 return $RETVAL 66 67 } 68 69 # Stop nginx daemons functions. 70 71 stop() { 72 73 echo -n $"Stopping $prog: " 74 75 killproc $nginxd 76 77 RETVAL=$? 78 79 echo 80 81 [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid 82 83 } 84 85 # reload nginx service functions. 86 87 reload() { 88 89 echo -n $"Reloading $prog: " 90 91 #kill -HUP `cat ${nginx_pid}` 92 93 killproc $nginxd -HUP 94 95 RETVAL=$? 96 97 echo 98 99 } 100 101 # See how we were called. 102 103 case "$1" in 104 105 start) 106 107 start 108 109 ;; 110 111 stop) 112 113 stop 114 115 ;; 116 117 reload) 118 119 reload 120 121 ;; 122 123 restart) 124 125 stop 126 127 start 128 129 ;; 130 131 status) 132 133 status $prog 134 135 RETVAL=$? 136 137 ;; 138 139 *) 140 141 echo $"Usage: $prog {start|stop|restart|reload|status|help}" 142 143 exit 1 144 145 esac 146 147 exit $RETVAL