這篇文章主要介紹“怎么使用nginx+tomcat實現(xiàn)靜態(tài)和動態(tài)頁面的分離”,在日常操作中,相信很多人在怎么使用nginx+tomcat實現(xiàn)靜態(tài)和動態(tài)頁面的分離問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”怎么使用nginx+tomcat實現(xiàn)靜態(tài)和動態(tài)頁面的分離”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
天鎮(zhèn)網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián),天鎮(zhèn)網(wǎng)站設(shè)計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為天鎮(zhèn)上千多家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站建設(shè)公司要多少錢,請找那個售后服務(wù)好的天鎮(zhèn)做網(wǎng)站的公司定做!
實驗環(huán)境:windows
實驗工具:nginx、tomcat
windows下安裝nginx非常簡單,去官網(wǎng)下載壓縮包解壓后并且雙擊解壓目錄下的nginx.exe程序即可。然后在瀏覽器輸入localhost可出現(xiàn)下圖,即表示nginx已經(jīng)在工作。
nginx的工作流程是:對外,nginx是一個服務(wù)器,所有的請求都先請求到nginx,然后再由nginx對內(nèi)網(wǎng)進(jìn)行請求的分發(fā)到tomcat,然后tomcat處理完請求后將數(shù)據(jù)發(fā)送給nginx,然后由nginx發(fā)送給用戶,整個過程對用戶的感覺就是nginx在處理用戶請求。既然這樣子,nginx肯定需要進(jìn)行配置,主要的配置文件是conf文件夾下的nginx.conf,因為我主要是進(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個用戶線程 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在請求完之后還會保留一段時間的連接,所以這里的timeout時長不能太大,也不能太小, #太小每次都要建立連接,太大會浪費系統(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; #} #對項目名進(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 /給注釋掉了,因為它會攔截所有的請求,無論是動態(tài)還是靜態(tài),還有一個就是對靜態(tài)文件的配置我配置成了javaweb的工作區(qū)間,接下來會說明為什么。
因為之前寫的項目一直以來都是使用jsp內(nèi)置對象來進(jìn)行目錄的文件訪問,但是使用了nginx一切都需要改變,當(dāng)我使用了nginx,并且項目沒有進(jìn)行路徑的修改的時候,總是無法加載靜態(tài)文件,查看日志發(fā)現(xiàn)這樣的錯誤: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(這是我的項目名)/lib/images包中查找靜態(tài)文件,而我又不想對項目文件做太大變化,其實還有一種方法是不使用jsp的內(nèi)置對象,直接使用http://localhost/username.png來代替內(nèi)置對象訪問靜態(tài)文件,但是這樣改要改很多的地方,所以我就直接將web-inf文件夾下的lib文件夾拷到上一個文件夾,也就是該文件夾和web-inf文件夾是兄弟文件夾的關(guān)系。
通過上述操作,就實現(xiàn)了動態(tài)與靜態(tài)的分離了,無圖無真相,下面展示效果圖。
上圖可以看到server是“apache-coyote/1.1”。tomcat的連接器就是這個。
而上面的server可以看到是nginx,說明對外而言接收請求的服務(wù)器是nginx。
到此,關(guān)于“怎么使用nginx+tomcat實現(xiàn)靜態(tài)和動態(tài)頁面的分離”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
新聞標(biāo)題:怎么使用nginx+tomcat實現(xiàn)靜態(tài)和動態(tài)頁面的分離
文章網(wǎng)址:http://aaarwkj.com/article44/jespee.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開發(fā)、云服務(wù)器、面包屑導(dǎo)航、App開發(fā)、網(wǎng)站維護(hù)、App設(shè)計
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)