Nginx反向代理、負載均衡、動靜分離、緩存、壓縮、防盜鏈、跨域訪問
一、反向代理
1、在192.168.189.130機器啟動tomcat服務,http://192.168.189.130:8080/ 訪問服務正常
2、在192.168.189.131機器配置nginx
server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { proxy_pass http://192.168.189.130:8080;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; }
3、啟動nginx服務,http://192.168.189.131/ 能夠訪問,這就完成了一個最簡單的反向代理
二、負載均衡
1、負載均衡使用的是nginx的http upstream模塊,語法:server address;
2、負載均衡策略或算法:默認使用輪詢算法,可以使用ip_hash 或者 權重輪詢
3、為了配置清晰,在nginx配置文件中http模塊引入額外配置
http { include mime.types; include extra/*.conf; 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;
目錄:
/root/data/program/nginx/conf/extra
配置文件proxy_demo.conf
#負載均衡模塊
upstream tomcat {
#最大失敗2次,失敗後在60秒內不再轉發到該機器
server 192.168.189.130:8080 max_fails=2 fail_timeout=60s;
server 192.168.189.132:8080 max_fails=2 fail_timeout=60s;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://tomcat;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#性能優化相關
#例:130機器 error、超時、500、503的時候請求轉到132
proxy_next_upstream error timeout http_500 http_503;
#連接超時時間
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
}
}
4、分別在192.168.189.130,192.168.189.132機器上啟動tomcat,
然後在/root/data/program/apache-tomcat-8.5.34/webapps/ROOT 下index.jsp頁面添加一些標識區分,啟動tomcat服務
訪問http://192.168.189.131/,可以看到130、132兩個服務輪詢處理請求
三、動靜分離
1、132機器/root/data/program/apache-tomcat-8.5.34/webapps/ROOT目錄新建文件夾bak,把除index.jsp外的其他文件(靜態資源)移動到bak文件夾
此時訪問http://192.168.189.132:8080/發現首頁樣式沒了
2、配置nginx,使用正則表達式,當請求靜態資源時從static-resource獲取
#負載均衡模塊
upstream tomcat {
#最大失敗2次,失敗後在60秒內不再轉發到該機器
#server 192.168.189.130:8080 max_fails=2 fail_timeout=60s;
server 192.168.189.132:8080 max_fails=2 fail_timeout=60s;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://tomcat;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#性能優化相關
#例:130機器 error、超時、500、503的時候請求轉到132
proxy_next_upstream error timeout http_500 http_503;
#連接超時時間
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
}
location ~ .*\.(js|css|png|svg|ico|jpg)$ {
root static-resource;
}
3、在nginx根目錄下創建文件夾static-resource,把tomcat目錄下的靜態資源文件拷貝到此目錄(靜態資源文件,不包含jsp)
tomcat靜態資源文件地址:/root/data/program/apache-tomcat-8.5.34/webapps/ROOT
4、./nginx -s reload nginx重新加載,此時訪問http://192.168.189.131/可以看到當請求轉發到192.168.189.132:8080上時,首頁樣式能夠顯示
緩存
nginx可以通過expires設置緩存,比如我們可以針對圖片做緩存。
在location中設置expires
格式: expires 30s|m|h|d
#負載均衡模塊
upstream tomcat {
#最大失敗2次,失敗後在60秒內不再轉發到該機器
# server 192.168.189.130:8080 max_fails=2 fail_timeout=60s;
server 192.168.189.132:8080 max_fails=2 fail_timeout=60s;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://tomcat;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#性能優化相關
#例:130機器 error、超時、500、503的時候請求轉到132
proxy_next_upstream error timeout http_500 http_503;
#連接超時時間
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
}
location ~ .*\.(js|css|png|svg|ico|jpg)$ {
root static-resource;
expires 1d;
}
}
對比設置緩存前,瀏覽器請求靜態資源Response Headers
設置expires 1d;後瀏覽器多了Expires
壓縮
Nginx中提供了一種Gzip的壓縮優化手段,可以對後端的文件進行壓縮傳輸,壓縮以後的好處在於能夠降低文件的大小來提高傳輸效率
配置信息
Gzip on|off 是否開啟gzip壓縮
Gzip_buffers 4 16k #設置gzip申請內存的大小,作用是按指定大小的倍數申請內存空間。4 16k代表按照原始數據大小以16k為單位的4倍申請內存。
Gzip_comp_level[1-9] 壓縮級別, 級別越高,壓縮越小,但是會占用CPU資源
Gzip_disable #正則匹配UA 表示什麽樣的瀏覽器不進行gzip
Gzip_min_length #開始壓縮的最小長度(小於多少就不做壓縮),可以指定單位,比如 1k
Gzip_http_version 1.0|1.1 表示開始壓縮的http協議版本
Gzip_proxied (nginx 做前端代理時啟用該選項,表示無論後端服務器的headers頭返回什麽信息,都無條件啟用壓縮)
Gzip_type text/pliain,application/xml 對那些類型的文件做壓縮 (conf/mime.conf)
Gzip_vary on|off 是否傳輸gzip壓縮標識; 啟用應答頭"Vary: Accept-Encoding";給代理服務器用的,有的瀏覽器支持壓縮,有的不支持,所以避免浪費不支持的也壓縮,所以根據客戶端的HTTP頭來判斷,是否需要壓縮
user root;
#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;
include extra/*.conf;
default_type application/octet-stream;
#配置nginx壓縮
gzip on;
gzip_min_length 5k;
gzip_comp_level 3;
gzip_types application/javascript image/jpeg image/svg+xml;
gzip_buffers 4 32k;
gzip_vary on;
壓縮前:
壓縮後:
四、防盜鏈
1、設置靜態資源只能192.168.189.132訪問,其他ip訪問返回404
#負載均衡模塊
upstream tomcat {
#最大失敗2次,失敗後在60秒內不再轉發到該機器
# server 192.168.189.130:8080 max_fails=2 fail_timeout=60s;
server 192.168.189.132:8080 max_fails=2 fail_timeout=60s;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://tomcat;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#性能優化相關
#例:130機器 error、超時、500、503的時候請求轉到132
proxy_next_upstream error timeout http_500 http_503;
#連接超時時間
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
}
location ~ .*\.(js|css|png|svg|ico|jpg)$ {
#防盜鏈
#設置靜態資源只能192.168.189.132訪問,其他ip訪問返回404
valid_referers none blocked 192.168.189.132;
if ($invalid_referer) {
return 404;
}
root static-resource;
expires 1d;
}
}
“Referer”請求頭為指定值時,內嵌變量$invalid_referer被設置為空字符串,否則這個變量會被置成“1”。
查找匹配時不區分大小寫,其中none表示缺少referer請求頭、blocked表示請求頭存在,但是它的值被防火墻或者代理服務器刪除、server_names表示referer請求頭包含指定的虛擬主機名
五、跨域請求
環境說明:兩臺tomcat服務130,132;一臺nginx服務131,131nginx代理132tomcat服務,130tomcat服務器請求131上一個json
1、130tomcat index.jsp頁面請求json(註意添加jquery.js)
2、132服務添加demo.json
{
"hello":"world"
}
3、No ‘Access-Control-Allow-Origin‘ header is present on the requested resource.
4、nginx配置
upstream tomcat {
#最大失敗2次,失敗後在60秒內不再轉發到該機器
#server 192.168.189.130:8080 max_fails=2 fail_timeout=60s;
server 192.168.189.132:8080 max_fails=2 fail_timeout=60s;
}
server {
listen 80;
server_name localhost;
location / {
# 註釋最原始的代理,使用負載均衡
# proxy_pass http://192.168.189.132:8080;
proxy_pass http://tomcat;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#性能優化相關
#例:130機器 error、超時、500、503的時候請求轉到132
proxy_next_upstream error timeout http_500 http_503;
#連接超時時間
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
#跨域請求,註釋掉130,只代理132
add_header ‘Access-Control-Allow-Origin‘ ‘*‘; #允許來自所有的訪問地址
add_header ‘Access-Control-Allow-Methods‘ ‘GET,PUT,POST,DELETE,OPTIONS‘; #支持的請求方式
add_header ‘Access-Control-Allow-Header‘ ‘Content-Type,*‘; #支持的媒體類型
}
location ~ .*\.(js|css|png|svg|ico|jpg)$ {
#防盜鏈
#設置靜態資源只能192.168.189.132訪問,其他ip訪問返回404
valid_referers none blocked 192.168.189.132;
if ($invalid_referer) {
return 404;
}
#130、132兩臺機器ROOT下靜態資源改到bak下,創建static-resource存靜態資源
root static-resource;
#nginx緩存配置,Response Headers多了一個Expires
expires 1d;
}
}
5、效果
Nginx反向代理、負載均衡、動靜分離、緩存、壓縮、防盜鏈、跨域訪問