国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網(wǎng)站 > Nginx > 正文

Nginx + Tomcat實(shí)現(xiàn)請求動態(tài)數(shù)據(jù)和請求靜態(tài)資源的分離詳解

2024-08-30 12:24:51
字體:
供稿:網(wǎng)友

前言

上篇博客說明了Nginx在應(yīng)用架構(gòu)中的作用,以及負(fù)載均衡的思路。這篇實(shí)踐一下其中的訪問靜態(tài)資源與訪問動態(tài)資源的操作。

一、認(rèn)識訪問靜態(tài)資源與訪問動態(tài)資源的區(qū)別

靜態(tài)資源:指存儲在硬盤內(nèi)的數(shù)據(jù),固定的數(shù)據(jù),不需要計算的數(shù)據(jù)。

如:圖片、字體、js文件、css文件等等。在用戶訪問靜態(tài)資源時,服務(wù)器會直接將這些資源返回到用戶的計算機(jī)內(nèi)。

動態(tài)資源:指需要服務(wù)器根據(jù)用戶的操作所返回的數(shù)據(jù),以及存儲在數(shù)據(jù)庫的數(shù)據(jù),經(jīng)過一系列邏輯計算后返回的數(shù)據(jù)。

如:請求明天的天氣信息數(shù)據(jù)、請求查看賬戶余額。

二、請求動態(tài)數(shù)據(jù)與請求靜態(tài)資源的分離的必要性

Tomcat應(yīng)用服務(wù)器是用來處理Servlet容器和JSP的,雖然它也可以處理HTML等等一系列靜態(tài)資源,但是效率不如Nginx;而且對Servlet容器和JSP的運(yùn)算已經(jīng)有很大壓力了,如果不分離會導(dǎo)致大量的性能浪費(fèi)。說到底,在應(yīng)用服務(wù)方面,要遵循一條原則——一個服務(wù)只做一件事。要做動態(tài)請求就專做動態(tài)請求,要做靜態(tài)請求就專做靜態(tài)請求,這樣才能提高性能。

我們要做的,就是當(dāng)用戶訪問靜態(tài)資源時,讓Nginx將靜態(tài)資源返回給用戶;當(dāng)用戶訪問動態(tài)資源時,將訪問轉(zhuǎn)到Tomcat應(yīng)用服務(wù)器上,Tomcat將數(shù)據(jù)返回給Nginx,Nginx再返回給用戶。

三、Nginx配置方法

在這里,對于Nginx的配置文件內(nèi)的各項(xiàng)參數(shù)說明不多講解,如需了解Nginx配置文件移步這里。

不知道配置文件位置的,一條指令:

sudo find / -name nginx.conf

要善于利用Linux指令,這樣就會無法自拔的愛上Linux;

先來一個全部配置:

# user www www;user root root;worker_processes 2; #設(shè)置值和CPU核心數(shù)一致error_log /home/zuoyu/ServerComputer/nginx/logs/nginx_error.log crit; #日志位置和日志級別pid /home/zuoyu/ServerComputer/nginx/nginx.pid;worker_rlimit_nofile 65535;events { #使用epoll模型提高性能 use epoll; #單個進(jìn)程最大連接數(shù) worker_connections 65535;}http { #擴(kuò)展名與文件類型映射表 include mime.types; #默認(rèn)類型 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"'; client_header_buffer_size 32k; large_client_header_buffers 4 32k; client_max_body_size 8m; types_hash_max_size 2048; types_hash_bucket_size 128;  sendfile on; tcp_nopush on; keepalive_timeout 60; tcp_nodelay on; fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; fastcgi_buffer_size 64k; fastcgi_buffers 4 64k; fastcgi_busy_buffers_size 128k; fastcgi_temp_file_write_size 128k; # 解壓縮傳輸 gzip on;  gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.0; gzip_comp_level 2; gzip_types text/plain application/x-javascript text/css application/xml; gzip_vary on; #負(fù)載均衡組 #靜態(tài)服務(wù)器組 upstream static.zuoyu.com { server localhost:81; } #動態(tài)服務(wù)器組 upstream dynamic.zuoyu.com { server localhost:8080; # server localhost:8081; # server localhost:8082; # server localhost:8083; } #配置代理參數(shù) proxy_redirect off; proxy_set_header HOST $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # client_max_body_size 10m; client_body_buffer_size 128k; proxy_connect_timeout 90; proxy_send_timeout 90; proxy_read_timeout 90; proxy_buffer_size 16k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k;  #緩存配置 proxy_cache_key '$host:$server_port$request_uri'; # proxy_temp_file_write_size 64k; proxy_temp_path /home/zuoyu/ServerComputer/nginx/proxy_temp_path; proxy_cache_path /home/zuoyu/ServerComputer/nginx/proxy_cache_path levels=1:2 keys_zone=cache_one:200m inactive=5d max_size=1g; proxy_ignore_headers X-Accel-Expires Expires Cache-Control Set-Cookie; #靜態(tài)資源主機(jī) server { listen 81; server_name localhost_0; charset utf8; location / {  root /home/zuoyu/Public/NginxStaticSource/static; } } # 下面是server虛擬主機(jī)的配置 server { listen 80;#監(jiān)聽端口 server_name localhost_1;#域名 charset utf8; location / {  # root /usr/share/nginx/html;  proxy_pass http://dynamic.zuoyu.com;  index index.html index.jsp; } location ~ .*/.(jsp|do|action)$ {  index index.jsp;  proxy_pass http://dynamic.zuoyu.com;   }  location ~ .*/.(gif|jpg|jpeg|png|bmp|swf|ico|svg)$ {  #緩存30天  expires 30d;  proxy_pass http://static.zuoyu.com;  proxy_cache cache_one;  proxy_cache_valid 200 304 302 5d;  proxy_cache_valid any 5d;  proxy_cache_key '$host:$server_port$request_uri';  add_header X-Cache '$upstream_cache_status from $host'; } location ~ .*/.(ttf|woff|woff2)$ {  #緩存30天  expires 30d;  proxy_pass http://static.zuoyu.com;  proxy_cache cache_one;  proxy_cache_valid 200 304 302 5d;  proxy_cache_valid any 5d;  proxy_cache_key '$host:$server_port$request_uri';  add_header X-Cache '$upstream_cache_status from $host'; } location ~ .*/.(js|css)$ {  #緩存7天  expires 7d;  proxy_pass http://static.zuoyu.com;  proxy_cache cache_one;  proxy_cache_valid 200 304 302 5d;  proxy_cache_valid any 5d;  proxy_cache_key '$host:$server_port$request_uri';  add_header X-Cache '$upstream_cache_status from $host'; } #其他頁面反向代理到tomcat容器 location ~ .*$ {  index index.jsp index.html;  proxy_pass http://dynamic.zuoyu.com; } access_log off;  error_page 500 502 503 504 /50x.html; location = /50x.html {  root /usr/share/nginx/html; } } }            
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 松潘县| 安阳市| 景泰县| 广水市| 浦城县| 乐至县| 连南| 新平| 壤塘县| 宣化县| 临沧市| 天门市| 筠连县| 庆阳市| 清原| 清河县| 鄄城县| 临邑县| 尼玛县| 龙南县| 新野县| 马边| 汨罗市| 惠水县| 景东| 桐梓县| 陕西省| 和硕县| 鄂州市| 阿拉尔市| 尼玛县| 奉新县| 肃北| 滨州市| 安远县| 荆州市| 无极县| 湟中县| 息烽县| 黎城县| 民勤县|