一、修改配置文件
- nginx的配置文件分爲三部分,我們一般只需要關注http配置的部分即可
- 基本配置
- events配置
- http配置
- 配置文件詳解
# =======基本配置=======================================
user nobody; # 配置work進程運行用戶,即進程的名稱,windows下可以註釋掉
worker_processes 1; #配置工作進程數目,根據硬件調整,通常等於cpu數量或者2倍cpu數量
error_log logs/error.log; #配置全局錯誤日誌及類型,[debug | info | notice | warn | eror | crit ] 默認是error
#pid logs/nginx.pid; # 配置進程pid文件
# =======events 配置=======================================
events {
worker_connections 1024; #配置每個work進程連接數上限,nginx支持的總連接數等於worker_processes * worker_connections
}
# =======http 配置=======================================
http {
include mime.types; #配置ngix支持哪些多媒體類型,可以在conf/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日誌及存放路徑,並使用上面定義的main日誌格式
#access_log logs/access.log main;
sendfile on; #開啓高效文件傳輸模式
#tcp_nopush on; #防止網絡阻塞
#keepalive_timeout 0;
keepalive_timeout 65; #長連接超時時間,單位是秒
#gzip on; # 開啓gzip壓縮輸出
# 配置虛擬主機,可以多個
server {
listen 80; #監聽端口
server_name localhost; #服務器主機
#charset koi8-r; #字符集(默認的是俄羅斯字符集)
#access_log logs/host.access.log main; #日誌路徑,如果配置了就優先用這個,否則用公共的
location / { #模糊匹配請求url裏面帶有斜槓的
root html; #配置服務器的默認網站根目錄位置,默認爲nginx安裝主目錄下的html目錄
index index.html index.htm; # 配置文件首頁名稱,可以配置多個,如果第一個找不到會自動往後找
}
#error_page 404 /404.html; #404頁面
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html; #其他報錯頁面配置
location = /50x.html { # 精確匹配
root html;
}
#php請求轉發
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
#php請求轉發
# 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;
#}
}
}
二、nginx的使用場景
nginx一般有兩種常用場景,即作爲靜態資源網站和負載均衡
靜態網站
nginx是一個HTTP的web服務器,可以將服務器上的靜態文件(如html、圖片等)通過http協議返回給瀏覽器客戶端
sever {
listen 80 #端口號
location / {
root /opt/www/; #靜態文件路徑
}
}
負載均衡
- 修改配置文件http部分
upstream www.baiducom {
server 127.0.0.1:9100 weight=1; //weight 是權重
server 127.0.0.1:9100 weight=3;
}
location / {
proxy_pass http://www.baiducom; // 這兒的網址要和upstream後面的字符串一樣
}
注意:如果訪問不了有可能是端口導致的,修改掉默認的80端口再試一下
三、常用的負載均衡策略
1、輪詢(默認)
- 每個請求輪流分配到不同的後端服務器,如果後端服務器down掉,將自動剔除
upstream tomcatserver {
server localhost:9090;
server localhost:8080;
}
2、權重
- 每個請求按一定比例分發到不同的後端服務器,weight值越大訪問比例越大,用於後端服務器性能不均的情況
upstream tomcatserver {
server localhost:9090 weight=3;
server localhost:8080 weight=1;
}
3、ip_hash
- ip_hash也叫ip綁定,每個請求按方位ip的hash值分配,這樣每個訪問客戶端會固定訪問一個後端服務器,可以解決會話session丟失的問題
upstream tomcatserver {
ip_hash;
server localhost:9090;
server localhost:8080;
}
4、最少連接策略
- web請求會被轉發到連接數最少的服務器上
upstream tomcatserver {
least_conn;
server localhost:9090;
server localhost:8080;
}