總覽nginx配置文件內(nèi)容
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;
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;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#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;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass [http://127.0.0.1;](http://127.0.0.1;)
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
include host/*.host;
}
一闰靴、NGINX配置文件組成
(1)nginx配置文件有三部分組成
第一部分:全局塊
從配置文件開始到events塊之間的內(nèi)容侄旬,主要設(shè)置一些影響nginx服務(wù)器整體運(yùn)行的配置指令
比如:worker_processes 1; worker_processes的值越大,可以支持的并發(fā)處理量也越多冬耿,一般與cpu一致
第二部分:events塊
events塊涉及的指令主要影響nginx服務(wù)器與用戶的網(wǎng)絡(luò)連接
比如 worker_connections 1024;
第三部分:http塊
Nginx服務(wù)器配置中最頻繁的部分坤塞,http塊包括http全局塊和server塊
Http全局塊:
http全局塊配置的指令主要包括文件引入冯勉、MIME-TYPE定義、日志定義摹芙、連接超時時間灼狰、單連接請求數(shù)上限等。
Server塊:
Server塊和虛擬主機(jī)有密切關(guān)系浮禾,每個http塊可包括多個server塊交胚,而每個server塊相當(dāng)于一個虛擬主機(jī)份汗。分為全局server塊和多個location塊。
全局server塊:
最常見的配置就是本虛擬主機(jī)的監(jiān)聽端口和本虛擬主機(jī)的名稱或IP配置蝴簇。
location塊:
反向代理杯活、負(fù)載均衡、動靜分離等熬词。
location \[ = | ~ | ~\* | ^~\] url{
}
二旁钧、NGINX配置反向代理
實(shí)現(xiàn)效果:
打開瀏覽器,在瀏覽器地址欄輸入www.aaa.com互拾,跳轉(zhuǎn)到Linux系統(tǒng)指定的頁面phpinfo的信息
實(shí)現(xiàn)配置:
location / {
root html;
proxy_pass http://127.0.0.1:10000;
index index.html index.htm;
}
三歪今、NGINX配置負(fù)載均衡
在http塊中配置 upstream塊
upstream myserver {
server 192.168.33.101:8080;
server 192.168.33.101:8081;
}
修改server塊的內(nèi)容
location / {
root html;
proxy_pass http://myserver;
index index.html index.htm;
}
nginx提供了負(fù)載均衡的分配方式:
方式一:輪詢(默認(rèn))
每個請求按事假順序逐一分配到不同的后端服務(wù)器,如果后端服務(wù)器down掉摩幔,能自動剔除
方式二:weight-權(quán)重
weight代表權(quán)重彤委,權(quán)重默認(rèn)為1,權(quán)重越高被分配的客戶端越多
配置方式:
upstream server_pool {
server 192.168.33.101 weight=10
server 192.168.33.103 weight=10
}
方式三:ip-hash
每個請求按訪問ip的hash結(jié)果分配或衡,這樣每個訪客固定訪問一個后端服務(wù)器焦影,可以解決session的問題。
配置方式:
upstream server_pool {
ip_hash;
server 192.168.33.101;
server 192.168.33.103;
}
四封断、NGINX配置動靜分離
動靜分離從目前實(shí)現(xiàn)角度來講大致分為兩種:
一種是純粹把靜態(tài)文件獨(dú)立成單獨(dú)的域名斯辰,放在獨(dú)立的服務(wù)器上,目前主流的方案坡疼。
另外一種是就是動態(tài)跟靜態(tài)文件混合在一起發(fā)布彬呻,通過nginx來分開。
通過location指定不同的后綴名實(shí)現(xiàn)不同的請求轉(zhuǎn)發(fā)柄瑰。
通過expires參數(shù)設(shè)置闸氮,可以使瀏覽器緩存過期時間,減少與服務(wù)器直接的請求和流量教沾。設(shè)置3d蒲跨,表示在這3天內(nèi)訪問這個url,發(fā)送一個請求授翻,比對服務(wù)器該文件最會更新時間沒有變化或悲,則不會從服務(wù)器抓取,返回304堪唐;如果有修改巡语,則直接從服務(wù)器重新下載,返回狀態(tài)碼200.
配置方式:
location /images/ {
root /data/web/;
}
location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*.(js|css)?$
{
expires 1h;
}
五淮菠、NGINX配置高可用
需要兩臺nginx服務(wù)器:192.168.33.101 和 192.168.33.103
在兩臺服務(wù)器中安裝nginx男公,安裝keepalived
安裝keepalived:
yum install keepalived -y
rpm -q -a keepalived
修改配置文件:
cd /etc/keepalived
vi keepalived.conf
分別將如下配置文件復(fù)制粘貼,覆蓋掉 keepalived.conf 虛擬 ip 為 192.168.25.50
對應(yīng)主機(jī) ip 需要修改的是 smtp_server 192.168.25.147(主)smtp_server 192.168.25.147(備) state MASTER(主) state BACKUP(備)
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.25.147
smtp_connect_timeout 30
router_id LVS_DEVEL # 訪問的主機(jī)地址
}
vrrp_script chk_nginx {
script "/usr/local/src/nginx_check.sh" # 檢測文件的地址
interval 2 # 檢測腳本執(zhí)行的間隔
weight 2 # 權(quán)重
}
vrrp_instance VI_1 {
state BACKUP # 主機(jī)MASTER兜材、備機(jī)BACKUP
interface ens33 # 網(wǎng)卡
virtual_router_id 51 # 同一組需一致
priority 90 # 訪問優(yōu)先級理澎,主機(jī)值較大逞力,備機(jī)較小
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.25.50 # 虛擬ip
}
}
啟動:
systemctl start keepalived.service
訪問虛擬 ip 192.168.25.50 成功
關(guān)閉主機(jī) 147 的 nginx 和 keepalived曙寡,發(fā)現(xiàn)仍然可以訪問
nginx_check.sh檢測腳本內(nèi)容:
!/bin/bash
A=ps -C nginx --no-header | wc -l
if [ $A -eq 0 ];then
/user/local/nginx/sbin/nginx
sleep
if [ `ps -C nginx --no-header | wc -l` -eq 0];then
killall keepalived
fi
fi
NGINX配置動靜分離圖
NGINX配置高可用圖