#這裏爲後端服務器wugk應用集羣配置,根據後端實際狀況修改便可,tdt_wugk爲負載均衡名稱,能夠任意指定(輪循的方式)php
#但必須跟vhosts.conf虛擬主機的pass段一致,不然不能轉發後端的請求。weight配置權重,在fail_timeout內檢查max_fails次數,失敗則剔除均衡。css
upstream tdt_wugk {html
server 127.0.0.1:8080 weight=1 max_fails=2 fail_timeout=30s;nginx
server 127.0.0.1:8081 weight=1 max_fails=2 fail_timeout=30s;web
}後端
#虛擬主機配置瀏覽器
server {緩存
#偵聽80端口服務器
listen 80;app
#定義使用www.wuguangke.cn訪問
server_name www.wuguangke.cn;
#設定本虛擬主機的訪問日誌
access_log logs/access.log main;
root /data/webapps/wugk; #定義服務器的默認網站根目錄位置
index index.php index.html index.htm; #定義首頁索引文件的名稱
#默認請求
location ~ /{
root /data/www/wugk; #定義服務器的默認網站根目錄位置
index index.php index.html index.htm; #定義首頁索引文件的名稱
#如下是一些反向代理的配置.
proxy_next_upstream http_502 http_504 error timeout invalid_header;
#若是後端的服務器返回502、504、執行超時等錯誤,自動將請求轉發到upstream負載均衡池中的另外一臺服務器,實現故障轉移。
proxy_redirect off;
#後端的Web服務器能夠經過X-Forwarded-For獲取用戶真實IP
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://tdt_wugk; #請求轉向後端定義的均衡模塊
}
# 定義錯誤提示頁面
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
#配置Nginx動靜分離,定義的靜態頁面直接從Nginx發佈目錄讀取。
location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$
{
root /data/www/wugk;
#expires定義用戶瀏覽器緩存的時間爲3天,若是靜態頁面不常更新,能夠設置更長,這樣能夠節省帶寬和緩解服務器的壓力。
expires 3d;
}
#PHP腳本請求所有轉發到 FastCGI處理. 使用FastCGI默認配置.
location ~ \.php$ {
root /root;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/www/wugk$fastcgi_script_name;
include fastcgi_params;
}
#設定查看Nginx狀態的地址
location /NginxStatus {
stub_status on;
}
}
}
1. grep空白行和註釋行
# cd /usr/local/nginx/
# grep -v "#" nginx.conf | grep -v "^$" >> nginx.conf.bak
# mv nginx.conf.bak nginx.conf
user nginx nginx;
worker_processes 1;
error_log logs/error.log info;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
server {
listen 80;
server_name www.doudou0826.com localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /404.html;
location = /50*.html {
root /data/www;
}
}
}
測試:
# /usr/local/nginx/sbin/nginx -t
# /usr/local/nginx/sbin/nginx -s reload
# curl 192.168.119.145
It's Works!
# tail -n 100 /usr/local/nginx/logs/access.log
進入到主頁目錄下,創建兩個目錄A、B。分別在A、B目錄下創建本身的主頁
# cd /usr/local/nginx/html/
# mkdir A B
# cp index.html a/
# cp index.html b/
# cat ./a/index.html
A site ! It's Works!
# cat ./b/index.html
B site ! It's Works!
修改配置文件爲:
user nginx nginx;
worker_processes 1;
error_log logs/error.log info;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
server {
listen 80;
server_name www.doudou0826a.com;
location / {
root html/a;
index index.html index.htm;
}
}
server {
listen 80;
server_name www.doudou0826b.com;
location / {
root html/b;
index index.html index.htm;
}
}
}
測試:
# /usr/local/nginx/sbin/nginx -t
# /usr/local/nginx/sbin/nginx -s reload
訪問以前須要修改本地主機的hosts文件,不然沒法用域名訪問
# curl www.doudou0826a.com
> A site ! It's Works!
# curl www.doudou0826b.com
B site ! It's Works!
在server裏面加入
location /NginxStatus {
stub_status on;
}
例如:
server {
listen 80;
server_name www.doudou0826b.com;
location / {
root html/b;
index index.html index.htm;
}
location /NginxStatus {
stub_status on;
}
}
測試:
# /usr/local/nginx/sbin/nginx -t
# /usr/local/nginx/sbin/nginx -s reload
# curl www.doudou0826b.com/NginxStatusActive connections: 2 server accepts handled requests 45 45 73 Reading: 0 Writing: 1 Waiting: 1