動靜分離
(1)純粹把靜態文件獨立成單獨的域名,放在獨立的服務器上,也是目前主流推崇的方案;(2)動態跟靜態文件混合在一起發佈,通過 nginx 來分開。
動靜分離原理圖:
在配置文件中:1、通過 location 指定不同的後綴名實現不同的請求轉發。2、可通過 expires 參數設置,可以使瀏覽器緩存過期時間,減少與服務器之前的請求和流量(非必要)。////////具體 Expires 定義:是給一個資源 設定一個過期時間,也就是說無需去服務端驗證,直接通過瀏覽器自身確認是否過期即可,所以不會產生額外的流量。此種方法非常適合不經常變動的資源。(如果經常更新的文件,不建議使用 Expires 來緩存),我這裏設置 3d,表示在這 3 天之內訪問這個 URL,發送一個請求,比對服務器該文件最後更新時間沒有變化,則不會從服務器抓取,返回狀態碼 304,如果有修改,則直接從服務器重新下載,返回狀態碼 200。
準備工作
準備靜態資源:創建nginx_test_data文件夾,其下兩個文件夾,分別放一個頁面和圖片
準備動態資源:在tomcat的webapps目錄下隨便建一個目錄(如nginx),其下隨便放一個jsp文件(如index.jsp)
靜態配置
在nginx.conf的server塊配置:
location /www/{
root I:/nginx-1.17.10/nginx_test_data;
}location /image/{
root I:/nginx-1.17.10/nginx_test_data;
autoindex on;
}
動態配置
配置一個代理:
#配置代理
upstream mynginx{
server 127.0.0.1:8080;
}
在server下配置:
# 所有動態請求都轉發給tomcat處理
location ~ \.(jsp|do)$ {
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_pass http://mynginx;
}
測試
瀏覽器輸入http://127.0.0.1/image/chutian.jpg就能訪問到圖片資源
瀏覽器輸入http://127.0.0.1/www/test.html就能訪問到靜態頁面
瀏覽器輸入http://127.0.0.1/nginx/index01.jsp,訪問結果如下:
完整配置文件
#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;
#配置服務器列表
upstream myserver{
server 127.0.0.1:8080 weight=1;
server 127.0.0.1:8081 weight=1;
}
#配置代理
upstream mynginx{
server 127.0.0.1:8080;
}
server {
listen 80;
server_name www.xb.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
proxy_pass http://myserver;
index index.html index.htm;
}
# 所有靜態請求都轉發給nginx處理
location /www/{
root I:/nginx-1.17.10/nginx_test_data;
}
location /image/{
root I:/nginx-1.17.10/nginx_test_data;
autoindex on;
}
# 所有動態請求都轉發給tomcat處理
location ~ \.(jsp|do)$ {
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_pass http://mynginx;
}
#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;
#}
# 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 9001;
server_name localhost;
location ~ /edu {
proxy_pass http://127.0.0.1:8080;
}
location ~ /vod {
proxy_pass http://127.0.0.1:8081;
}
}
# 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;
# }
#}
}