nginx簡介
1.nginx可以作爲一個HTTP服務器進行網站的發佈處理
2.nginx可以作爲反向代理進行負載均衡的實現
#什麼是反向代理
反向代理(Reverse Proxy)方式是指以代理服務器來接受internet上的連接請求,然後將請求轉發給內部網絡上的服務器,並將從服務器上得到的結果返回給internet上請求連接的客戶端,此時代理服務器對外就表現爲一個反向代理服務器。
其實這個機制很簡單:
當一臺服務器處理不過來高併發的請求時,把壓力分發給其他多臺服務器 從而達到負載均衡的作用
nginx安裝
linux:
yum install nginx 或apt-get install nginx (yum和apt-get都是軟件獲取管理工具 爽歪歪)
nginx的啓動
[root@VM_158_86_centos /]# nginx
[root@VM_158_86_centos /]# ps -ef|grep nginx
root 14361 1 0 16:42 ? 00:00:00 nginx: master process nginx
nginx 14362 14361 0 16:42 ? 00:00:00 nginx: worker process
root 14365 13653 0 16:42 pts/0 00:00:00 grep --color=auto nginx
**nginx會自動根據當前主機的CPU的內核數目創建對應的進程數量 當前服務器是單核單線程的玩具服務器 所以只有一個worker process業務進程 **
master process 爲守護進程 守護master process,防止master process被異常終止
##配置nginx.conf文件
main //全局配置
events { //nginx 工作模式配置
worker_connections 1024;
}
http { //http配置
server {//服務端配置
location / {//路由配置
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
upstream name { //負載均衡配置
....
}
}
#端口映射域名
server {
listen 80;
server_name www.xxx.com;
location / {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
負載均衡
upstream www.xxx.com
{
//server weight表示權重 值越高被分配的機率就越大
server 10.139.158.85:9527 weight=4 max_fails=2 fail_timeout=30s;
server 10.139.158.86:9527 weight=4 max_fails=2 fail_timeout=30s;
}
server
{
listen 80; #監聽端口
server_name localhost;
#默認請求設置
location / {
proxy_pass http://www.xxx.com;
}
}