Nginx
1.nginx簡介:
Nginx (engine x) 是一個高性能的HTTP和反向代理服務器,也是一個IMAP/POP3/SMTP服務器。Nginx是由伊戈爾·賽索耶夫爲俄羅斯訪問量第二的Rambler.ru站點(俄文:Рамблер)開發的,第一個公開版本0.1.0發佈於2004年10月4日。
Nginx是一款輕量級的Web服務器/反向代理服務器及電子郵件(IMAP/POP3)代理服務器,並在一個BSD-like協議下發行。其特點是佔有內存少,併發能力強,事實上nginx的併發能力確實在同類型的網頁服務器中表現較好,中國大陸使用nginx網站用戶有:百度、京東、新浪、網易、騰訊、淘寶等。
2.Nginx國內版本
Tengine是由淘寶網發起的Web服務器項目。它在Nginx的基礎上,針對大訪問量網站的需求,添加了很多高級功能和特性。Tengine的性能和穩定性已經在大型的網站如淘寶網 ,天貓商城 等得到了很好的檢驗。它的最終目標是打造一個高效、穩定、安全、易用的Web平臺。
從2011年12月開始,Tengine成爲一個開源項目,Tengine團隊在積極地開發和維護着它。Tengine團隊的核心成員來自於淘寶 、搜狗 等互聯網企業。
3.傳統All IN ONE架構:
4.反向代理:
反向代理: :互聯網中的機器訪問局域網中代理服務器 代理服務器通過各種規則找到對應後臺服務器 獲取數據響應 反向代理中 訪問機器明確訪問的
5.Tegine安裝
$ ./configure :檢查
$ make :編譯
$ sudo make install :安裝
搜索庫:yum search gcc
安裝:yum install gcc
缺少pcre和pcre-devel:yum -y install pcre pcre-devel
缺少openssl和openssl-devel 安裝 :yum -y install openssl openssl-devel
6.nginx命令參數:
nginx -m 顯示所有加載的模塊
nginx -l 顯示所有可以使用的指令
nginx -t 檢查nginx的配置文件是否正確
nginx -s 啓動nginx
nginx -s reload 重啓nginx
nginx -s stop 停止nginx
7.配置nginx
nginx負責負載均衡轉發
配置負載均衡使用nginx的代理模塊 具體參考 tengine官網配置(http://tengine.taobao.org/document_cn/http_upstream_check_cn.html)
找到usr/local/nginx/conf/nginx.conf 配置
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 backend {
server 192.168.182.128:8080;
server 192.168.182.1: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://backend;//配置反向代理
}
內核模塊
#cup個數:
worker_processes 1;
支持的最大數:
events {
worker_connections 1024;
}
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 backend {
server 192.168.182.128:8080;
server 192.168.182.1:8080;
#session_sticky模塊功能中
session_sticky; #粘性session
#健康檢查(首頁/文檔/健康檢查模塊
#interval:間隔時間
#rise=2表示連續兩次連接後端服務器表示存活
#fall=5表示連續5次連接後端服務器失敗表示後端tomcat掛了
)
check interval=3000 rise=2 fall=5 timeout=1000 type=http;
#發送一個HEAD請求
check_http_send "HEAD / HTTP/1.0\r\n\r\n";
#返回 2和3開頭響應碼錶示成功
check_http_expect_alive http_2xx http_3xx;
}
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://backend;
#當用戶訪問 /admin/路徑 阻止訪問(nginx文檔/文檔/ngx_htt_rewrite_module)
if ($uri ~* ^/admin/.*$ ){
return 505;
}
}
location /status {
check_status;
#添加用戶名密碼框
auth_basic "closed site";#標題
auth_basic_user_file /usr/local/nginx /conf/htpasswd;#絕對路徑
access_log off;
allow 192.168.182.1;
deny all;
}
#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;
}
location = /my505.html {
root html;
}
添加用戶名密碼:
Nginx/文檔/ngx_http_auth=basic_module
指定保存用戶名和密碼的文件,格式如下:
# comment
name1:password1
name2:password2:comment
name3:password3
密碼應該使用crypt()函數加密。 可以用Apache發行包中的htpasswd命令來創建此類文件。