由于公司的服務(wù)器是windows環(huán)境,所以學(xué)習(xí)nginx的過程也是在windows中進(jìn)行。
1.下載安裝Nginx
安裝成功后有歡迎頁

2.Nginx的配置文件
nginx.conf文件的全部內(nèi)容:
#user nobody;
#開啟進(jìn)程數(shù) <=CPU數(shù)
worker_processes 1;
#錯誤日志保存位置
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#進(jìn)程號保存文件
#pid logs/nginx.pid;
#每個進(jìn)程最大連接數(shù)(最大連接=連接數(shù)x進(jìn)程數(shù))每個worker允許同時產(chǎn)生多少個鏈接,默認(rèn)1024
events {
worker_connections 1024;
}
http {
#文件擴(kuò)展名與文件類型映射表
include mime.types;
#默認(rèn)文件類型
default_type application/octet-stream;
#日志文件輸出格式 這個位置相于全局設(shè)置
#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;
#打開發(fā)送文件
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
#連接超時時間
keepalive_timeout 65;
#打開gzip壓縮
#gzip on;
server {
#監(jiān)聽端口,默認(rèn)是80端口
listen 80;
#監(jiān)聽域名
server_name localhost;
#charset koi8-r;
#nginx訪問日志放在logs/host.access.log下,并且使用main格式(還可以自定義格式)
#access_log logs/host.access.log main;
#如果沒有l(wèi)ocation更明確的匹配訪問路徑的話,訪問請求都會被該location處理。
location / {
#root指定nginx的根目錄為/usr/local/nginx/html
root html;
#默認(rèn)訪問文件,歡迎頁先去html目錄下找index.html,如果找不到再去找index.htm
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
#錯誤頁面及其返回地址,錯誤碼為500、502、503、504都會返回50.html錯誤頁面。
error_page 500 502 503 504 /50x.html;
#location后面是"="的話,說明是精確匹配
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;
#}
# 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;
# }
#}
}
3.Nginx的主要應(yīng)用
a.靜態(tài)網(wǎng)站
b.負(fù)載均衡
c.靜態(tài)代理
d.動靜分離
e.虛擬主機(jī)
a.靜態(tài)網(wǎng)站
1.準(zhǔn)備一個html

2.配置location

這里匹配的是/,在url輸入127.0.0.1:8001就會訪問到index.html

若寫成test,即匹配roor/test,即c:/nginxTest/test
b.負(fù)載均衡
舉例:
1.準(zhǔn)備兩個后臺,我這邊為9005和9006

2.在nginx配置

3.測試頁面


其他方式:
1、輪詢(默認(rèn))
每個請求按時間順序逐一分配到不同的后端服務(wù)器,如果后端服務(wù)器down掉,能自動剔除。
upstream backserver {
server 192.168.0.14;
server 192.168.0.15;
}
2、指定權(quán)重
指定輪詢幾率,weight和訪問比率成正比,用于后端服務(wù)器性能不均的情況。
upstream backserver {
server 192.168.0.14 weight=8;
server 192.168.0.15 weight=10;
}
3、IP綁定 ip_hash
每個請求按訪問ip的hash結(jié)果分配,這樣每個訪客固定訪問一個后端服務(wù)器,可以解決session的問題。
upstream backserver {
ip_hash;
server 192.168.0.14:88;
server 192.168.0.15:80;
}
4、fair(第三方)
按后端服務(wù)器的響應(yīng)時間來分配請求,響應(yīng)時間短的優(yōu)先分配。
upstream backserver {
server server1;
server server2;
fair;
}
5、url_hash(第三方)
按訪問url的hash結(jié)果來分配請求,使每個url定向到同一個后端服務(wù)器,后端服務(wù)器為緩存時比較有效。
upstream backserver {
server squid1:3128;
server squid2:3128;
hash $request_uri;
hash_method crc32;
}
c.靜態(tài)代理
使用nginx來訪問靜態(tài)資源,可以加快資源加載速度和效率。
root的處理結(jié)果是:root路徑+location路徑
alias的處理結(jié)果是:使用alias路徑替換location路徑
1.準(zhǔn)備一張圖片

2.在nginx配置
server {
listen 8005;
server_name test1;
location / {
root E:/nginxTest;
index index.html;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_redirect off;
}
location /img/ {
alias E:/nginxTest/img/;
expires 30d;
}
}
3.測試頁面

d.動靜分離
1.準(zhǔn)備前端頁面,且寫好請求的ajax
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<script src="js/jquery-3.0.0.js"></script>
<script>
$(function () {
$("#getInfo").on("click",function () {
$.ajax({
url: "/main/getHello",
type: "get",
contentType: "application/json",
dataType: "json",
success: function (res) {
console.log("???")
$("#test").html(res)
}
})
})
})
</script>
<body>
<button id="getInfo">從后臺獲取</button>
<p id="test">test</p>
</body>
</html>

2.寫好后臺,我后臺端口為9005
@Controller
@RequestMapping("/main")
public class MainController {
@RequestMapping(value="/getHello",method=RequestMethod.GET)
@ResponseBody
public String getHello() {
System.out.println("getHello");
return "this is from 9005";
}
3.在nginx配置文件中,配置前端頁面位置并將ajax的請求轉(zhuǎn)發(fā)到9005

4.頁面測試

e.虛擬主機(jī)

基于不同端口的演示:
1.nginx的配置

2.測試頁

