麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

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

使用nginx+tomcat實(shí)現(xiàn)靜態(tài)和動(dòng)態(tài)頁面的分離

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

博主最近在優(yōu)化一個(gè)javaweb項(xiàng)目,該項(xiàng)目之前一直都是使用tomcat處理用戶請(qǐng)求的,無論靜態(tài)還是動(dòng)態(tài)的東西,一律交給tomcat處理。tomcat主要是負(fù)責(zé)處理servlet的,靜態(tài)的文件還是交給nginx處理,nginx對(duì)靜態(tài)文件的處理比tomcat不是只快了一點(diǎn),并且Nginx的使用對(duì)項(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,tomcat動(dòng)靜分離,nginx動(dòng)靜態(tài)分離,動(dòng)態(tài)分離 

nginx的工作流程是:對(duì)外,nginx是一個(gè)服務(wù)器,所有的請(qǐng)求都先請(qǐng)求到nginx,然后再由nginx對(duì)內(nèi)網(wǎng)進(jìn)行請(qǐng)求的分發(fā)到tomcat,然后tomcat處理完請(qǐng)求后將數(shù)據(jù)發(fā)送給nginx,然后由nginx發(fā)送給用戶,整個(gè)過程對(duì)用戶的感覺就是nginx在處理用戶請(qǐng)求。既然這樣子,nginx肯定需要進(jìn)行配置,主要的配置文件是conf文件夾下的nginx.conf,因?yàn)槲抑饕沁M(jìn)行了靜態(tài)與動(dòng)態(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在請(qǐng)求完之后還會(huì)保留一段時(shí)間的連接,所以這里的timeout時(shí)長不能太大,也不能太小,  #太小每次都要建立連接,太大會(huì)浪費(fèi)系統(tǒng)資源(用戶不再請(qǐ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;  #這里的/表示所有的請(qǐng)求    #location / {    #將80端口的所有請(qǐng)求都轉(zhuǎn)發(fā)到8080端口去處理,proxy_pass代表的是代理路徑   #  proxy_pass http://localhost:8080;     # root  html;      # index index.html index.htm;    #}  #對(duì)項(xiàng)目名進(jìn)行訪問就去訪問tomcat服務(wù)  location /Student_Vote {       proxy_pass http://localhost:8080;  }  #對(duì)jsp和do結(jié)尾的url也去訪問tomcat服務(wù)  location ~ /.(jsp|do)$ {       proxy_pass http://localhost:8080;  }    #對(duì)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)樗鼤?huì)攔截所有的請(qǐng)求,無論是動(dòng)態(tài)還是靜態(tài),還有一個(gè)就是對(duì)靜態(tài)文件的配置我配置成了javaweb的工作區(qū)間,接下來會(huì)說明為什么。

因?yàn)橹皩懙捻?xiàng)目一直以來都是使用jsp內(nèi)置對(duì)象來進(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將會(huì)從/Stdent_Vote(這是我的項(xiàng)目名)/lib/images包中查找靜態(tài)文件,而我又不想對(duì)項(xiàng)目文件做太大變化,其實(shí)還有一種方法是不使用jsp的內(nèi)置對(duì)象,直接使用http://localhost/username.png來代替內(nèi)置對(duì)象訪問靜態(tài)文件,但是這樣改要改很多的地方,所以我就直接將web-inf文件夾下的lib文件夾拷到上一個(gè)文件夾,也就是該文件夾和web-inf文件夾是兄弟文件夾的關(guān)系。

通過上述操作,就實(shí)現(xiàn)了動(dòng)態(tài)與靜態(tài)的分離了,無圖無真相,下面展示效果圖。

nginx,tomcat動(dòng)靜分離,nginx動(dòng)靜態(tài)分離,動(dòng)態(tài)分離

上圖可以看到server是“Apache-Coyote/1.1”。tomcat的連接器就是這個(gè)。

nginx,tomcat動(dòng)靜分離,nginx動(dòng)靜態(tài)分離,動(dòng)態(tài)分離

而上面的server可以看到是nginx,說明對(duì)外而言接收請(qǐng)求的服務(wù)器是nginx。

 以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 国产美女爽到喷白浆的 | 国产91丝袜在线熟 | 黄色免费小网站 | 一级裸体视频 | 成人综合一区二区 | 欧美一级小视频 | 亚洲成人入口 | 成人宗合网 | 精品一区二区三区毛片 | av在线播放免费 | 一级电影在线免费观看 | 欧美久久久久久久久 | 国产精品久久久久久久久久久久久久久久 | 久国产| 欧美三日本三级少妇三级99观看视频 | 久久久一区二区三区精品 | 日本黄色a视频 | 一级片免费在线 | 黄色片免费看网站 | 亚洲一区二区中文 | 九九热视频这里只有精品 | 精品亚洲va在线va天堂资源站 | 久久国产中文字幕 | 国产69精品久久久久久久久久 | a黄在线观看 | 国产jjizz一区二区三区视频 | 久久久久一本一区二区青青蜜月 | 欧美在线观看视频一区 | 日韩黄色精品视频 | 日本68xxxx | 狠狠久久伊人中文字幕 | 中文字幕一区在线观看视频 | 在线看一区二区三区 | 一级做a在线观看 | 欧美视频一区二区 | 久久精品中文字幕 | v天堂在线视频 | 久久久久免费精品国产小说色大师 | 青青国产在线视频 | 北原夏美av | 内地av在线 |