Nginx 一款高并發,對系統配置要求不高,配置簡單就能實現負載均衡,反向代理等服務
設備: 阿里云服務器
系統: Centos 7
配置: 單核cpu 1G
安裝
sudo yum install nginx
默認文件
HTML路徑: /usr/share/nginx/html/
Nginx配置路徑: /etc/nginx/nginx.conf
啟動 重啟 關閉
nginx -c /etc/nginx/nginx.conf
nginx -s reload
nginx -s stop
Nginx配置文件 /etc/nginx/nginx.conf
access_log /var/log/nginx/access.log main;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
// http服務
http { //該配置只在http內起作用
server { //服務(可配置多個,如二級,三級域名不同跳轉邏輯)
listen 80; //監聽端口號
server_name _; //服務器域名
location / { //路徑匹配
root /usr/share/nginx/html/; //跳轉路徑
index index.html index.htm; //跳轉該路徑加載文件
}
}
}
Nginx log日志
access_log /var/log/nginx/access.log main;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
gzip壓縮(用戶瀏覽頁面更快)
gzip on; //開啟壓縮
gzip_min_length 1k; //壓縮最小長度為1k
目錄顯示(顯示文件夾)
location / {
autoindex on; //開啟顯示
autoindex_exact_size on; //文件大小
autoindex_localtime //顯示時間功能
}
負載均衡 反向代理
upstream a {
//weight權重 max_failes超時次數 failes_timeout超時30s
//非backup機器忙的時候,優先backup(用于特殊情況下)
server 192.168.1.10:80 weight=4 max_failes=2 failes_timeout=30s;
server 192.168.1.20:80 weight=4 max_failes=2 failes_timeout=30s;
server 192.168.1.30:80 backup;
}
upstream b {
server 192.168.1.10:80;
server 192.168.1.30:80 down; //down表示不參與負載均衡(關閉訪問)
}
server {
location / {
proxy_pass http://a;
proxy_set_header Host $host;
}
location /message/ {
proxy_pass http://b;
proxy_set_header X-Forwarded-For $remote_addr
}
}
location匹配
location = / {} // 僅僅匹配/
location / {} // 以/開頭
location ^~ /img/ {} // 以/img/開頭(不區分大小寫)
location ~* .(gif|jpg|jpeg)$ {} // 以.gif、.jpg、 .jpeg結尾