使用nginx+tomcat實(shí)現(xiàn)靜態(tài)和動態(tài)頁面的分離
博主最近在優(yōu)化一個(gè)javaweb項(xiàng)目,該項(xiàng)目之前一直都是使用tomcat處理用戶請求的,無論靜態(tài)還是動態(tài)的東西,一律交給tomcat處理。tomcat主要是負(fù)責(zé)處理servlet的,靜態(tài)的文件還是交給nginx處理,nginx對靜態(tài)文件的處理比tomcat不是只快了一點(diǎn),并且Nginx的使用對項(xiàng)目并發(fā)能力有很大的提升。下面主要記錄下主要的配置過程:
實(shí)驗(yàn)環(huán)境:windows
實(shí)驗(yàn)工具:Nginx、tomcat
windows下安裝Nginx非常簡單,去官網(wǎng)下載壓縮包解壓后并且雙擊解壓目錄下的nginx.exe程序即可。然后在瀏覽器輸入localhost可出現(xiàn)下圖,即表示nginx已經(jīng)在工作。
nginx的工作流程是:對外,nginx是一個(gè)服務(wù)器,所有的請求都先請求到nginx,然后再由nginx對內(nèi)網(wǎng)進(jìn)行請求的分發(fā)到tomcat,然后tomcat處理完請求后將數(shù)據(jù)發(fā)送給nginx,然后由nginx發(fā)送給用戶,整個(gè)過程對用戶的感覺就是nginx在處理用戶請求。既然這樣子,nginx肯定需要進(jìn)行配置,主要的配置文件是conf文件夾下的nginx.conf,因?yàn)槲抑饕沁M(jìn)行了靜態(tài)與動態(tài)分離,所以沒有進(jìn)行靜態(tài)文件緩存,也沒有進(jìn)行負(fù)載均衡的配置。
#user nobody; worker_processes 2; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { #nginx默認(rèn)最大并發(fā)數(shù)是1024個(gè)用戶線程 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; #http1.1在請求完之后還會保留一段時(shí)間的連接,所以這里的timeout時(shí)長不能太大,也不能太小, #太小每次都要建立連接,太大會浪費(fèi)系統(tǒng)資源(用戶不再請求服務(wù)器) keepalive_timeout 65; #gzip on; server { #nginx監(jiān)聽80端口 listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; #這里的/表示所有的請求 #location / { #將80端口的所有請求都轉(zhuǎn)發(fā)到8080端口去處理,proxy_pass代表的是代理路徑 # proxy_pass http://localhost:8080; # root html; # index index.html index.htm; #} #對項(xiàng)目名進(jìn)行訪問就去訪問tomcat服務(wù) location /Student_Vote { proxy_pass http://localhost:8080; } #對jsp和do結(jié)尾的url也去訪問tomcat服務(wù) location ~ \.(jsp|do)$ { proxy_pass http://localhost:8080; } #對js、css、png、gif結(jié)尾的都去訪問根目錄下查找 location ~ \.(js|css|png|gif)$ { root F:/javaweb; } #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 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; # } #} }
上面的配置中我把默認(rèn)的location /給注釋掉了,因?yàn)樗鼤r截所有的請求,無論是動態(tài)還是靜態(tài),還有一個(gè)就是對靜態(tài)文件的配置我配置成了javaweb的工作區(qū)間,接下來會說明為什么。
因?yàn)橹皩懙捻?xiàng)目一直以來都是使用jsp內(nèi)置對象來進(jìn)行目錄的文件訪問,但是使用了nginx一切都需要改變,當(dāng)我使用了nginx,并且項(xiàng)目沒有進(jìn)行路徑的修改的時(shí)候,總是無法加載靜態(tài)文件,查看日志發(fā)現(xiàn)這樣的錯(cuò)誤:2016/05/20 18:27:30 [error] 6748#6936: *225 CreateFile() "F:/javaweb/Student_Vote/lib/images/username.png" failed (3: The system cannot find the path specified), client: 127.0.0.1, server: localhost, request: "GET /Student_Vote/lib/images/username.png HTTP/1.1", host: "localhost", referrer: "http://localhost/Student_Vote/index.jsp",大致信息是根據(jù)jsp中文件的配置,nginx將會從/Stdent_Vote(這是我的項(xiàng)目名)/lib/images包中查找靜態(tài)文件,而我又不想對項(xiàng)目文件做太大變化,其實(shí)還有一種方法是不使用jsp的內(nèi)置對象,直接使用http://localhost/username.png來代替內(nèi)置對象訪問靜態(tài)文件,但是這樣改要改很多的地方,所以我就直接將web-inf文件夾下的lib文件夾拷到上一個(gè)文件夾,也就是該文件夾和web-inf文件夾是兄弟文件夾的關(guān)系。
通過上述操作,就實(shí)現(xiàn)了動態(tài)與靜態(tài)的分離了,無圖無真相,下面展示效果圖。
上圖可以看到server是“Apache-Coyote/1.1”。tomcat的連接器就是這個(gè)。
而上面的server可以看到是nginx,說明對外而言接收請求的服務(wù)器是nginx。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Nginx設(shè)置目錄的訪問權(quán)限實(shí)現(xiàn)訪問靜態(tài)資源
遇到Permission denied錯(cuò)誤,通常是Nginx用戶權(quán)限不足,本文就來介紹一下Nginx設(shè)置目錄的訪問權(quán)限實(shí)現(xiàn)訪問靜態(tài)資源2024-10-10Nginx 流量控制/限流的具體實(shí)現(xiàn)示例
限流是一種流量控制手段,用于限制單位時(shí)間內(nèi)可以通過系統(tǒng)的請求數(shù)或連接數(shù),本文主要介紹了Nginx流量控制/限流的具體實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下2024-07-07Nginx配置PATHINFO隱藏thinkphp index.php
這篇文章主要介紹了Nginx配置PATHINFO隱藏thinkphp index.php,本文直接給出配置示例,需要的朋友可以參考下2015-07-07解決Nginx 配置 proxy_pass 后 返回404問題
這篇文章主要介紹了Nginx 配置 proxy_pass 后 返回404問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01利用nginx實(shí)現(xiàn)動靜分離的負(fù)載均衡集群實(shí)戰(zhàn)教程
這篇文章介紹了利用nginx實(shí)現(xiàn)動靜分離的負(fù)載均衡集群實(shí)戰(zhàn),本次用到的操作系統(tǒng)及服務(wù),本次實(shí)驗(yàn)一共需要3臺服務(wù)器,一臺nginx做為負(fù)載均衡分發(fā)器和動靜分離的分發(fā)器,兩臺apache做為后端服務(wù)器,使用nginx實(shí)現(xiàn)兩臺apache服務(wù)器的負(fù)載均衡和動靜分離,需要的朋友可以參考下2023-03-03Centos7安裝、卸載nginx及配置,配置成系統(tǒng)服務(wù)方式(一步到位)
這篇文章主要介紹了Centos7安裝、卸載nginx及配置,配置成系統(tǒng)服務(wù)方式(一步到位),具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12Keepalived實(shí)現(xiàn)Nginx負(fù)載均衡高可用的示例代碼
這篇文章主要介紹了Keepalived實(shí)現(xiàn)Nginx負(fù)載均衡高可用的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04Nginx+Windows搭建域名訪問環(huán)境的操作方法
這篇文章主要介紹了Nginx搭建域名訪問環(huán)境,包括nginx配置文件的相關(guān)介紹及對nginx配置文件的分析,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03