nginx功能:
- 支持修改nginx配置,可平滑重啓,不中斷業務訪問
- 可自定義訪問日誌格式,臨時緩衝寫日誌操作,快速日誌輪詢,及通過rsyslog處理日誌
- 可利用信號控制nginx進程
- 支持rewrite,支持uri重寫和正則表達式匹配
- 支持基於可獨斷ip地址和http基本認證的訪問控制
- 支持put,delete,mkcol,copy,move等http請求
- 支持flv和MP4流技術產品應用
- 支持http相應速率限制
- 支持同一ip地址的併發連接或請求數限制
優點:
- 支持高併發,能支持幾萬併發
- 資源消耗少,3萬併發連接下,10個線程消耗不到200m
- 可以做http反向代理和加速緩存,即負載均衡,內置對rs節點服務器健康檢查功能,相當於專業的haproxy軟件或lvs功能
- 具備squid等專業緩存軟件等的緩存功能
- 支持異步網絡io事件模型epoll
安裝nginx
1.上傳
可以使用lrzsz 上傳壓縮包
yum install lrzsz
2.解壓
3.安裝nginx所需的pcre庫(實現僞靜態功能)
yum install pcre pcre-devel -y
yum install opssl openssl-devel -y
#編譯
useradd www -s /sbin/nologin -M(不創建家目錄)
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module(查看狀態) --with-http_ssl_module(支持https)
#創建軟連接,忽略版本號方便升級
ln -s /dir/nginx-version /dir/nginx
#檢查語法
/usr/local/nginx -t
安裝常見錯誤
- ssl modules require the Openssl
解決:安裝openssl - c編譯器沒安裝
- 【emerg】getpwnam(“nginx”) failed
解決:沒有對應的用戶,創建用戶
最簡配置
worker_processes 1;
worker_processes 1;
#events模塊
events {
worker_connections 1024;
}
#http模塊
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
#一個server代表一個網站
server {
listen 80;
server_name localhost;#域名
location / {
root html; #根目錄
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
#完事後
nginx -t
nginx -s reload
#檢查(待完善)
常用模塊
虛擬主機
基於域名的虛擬主機
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
#一個server代表一個網站
server {
listen 80;
server_name www.baidu.com;#域名
location / {
root html/www; #根目錄
index index.html index.htm;
}
}
#一個server代表一個網站
server {
listen 80;
server_name yunpan.baidu.com;#域名
location / {
root html/yunpan; #根目錄
index index.html index.htm;
}
}
}基於端口的虛擬主機
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
#一個server代表一個網站
server {
listen 80;#端口不一樣
server_name www.baidu.com;#域名
location / {
root html/www; #根目錄
index index.html index.htm;
}
}
#一個server代表一個網站
server {
listen 90;#端口不一樣
server_name yunpan.baidu.com;#域名
location / {
root html/yunpan; #根目錄
index index.html index.htm;
}
}
}基於ip的虛擬主機
配置網卡
ifconfig eth0:10.0.0.101/24 up
ip addr add 10.0.0.102/24 dev eth0 label eth0:1http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
#一個server代表一個網站
server {
listen 10.0.0.8:80;#域名不一樣
server_name www.baidu.com;#域名
location / {
root html/www; #根目錄
index index.html index.htm;
}
}
#一個server代表一個網站
server {
listen 10.0.0.10:90;#域名不一樣
server_name yunpan.baidu.com | yunpan2.baidu.com;#域名,配上了別名
location / {
root html/yunpan; #根目錄
index index.html index.htm;
}
}
}
配置完善
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
include ./www.conf; #通過外部引用
include ./yunpan.conf;#通過外部引用
}
啓動
./nginx
平滑重啓
./nginx -s reload
查看編譯參數
./nginx -V