博主最近在優化一個javaweb項目,該項目之前一直都是使用tomcat處理用戶請求的,無論靜態還是動態的東西,一律交給tomcat處理。tomcat主要是負責處理servlet的,靜態的文件還是交給nginx處理,nginx對靜態文件的處理比tomcat不是只快了一點,並且Nginx的使用對項目並發能力有很大的提升。下面主要記錄下主要的配置過程:
實驗環境:windows
實驗工具:Nginx、tomcat
windows下安裝Nginx非常簡單,去官網下載壓縮包解壓后並且雙擊解壓目錄下的nginx.exe程序即可。然后在瀏覽器輸入localhost可出現下圖,即表示nginx已經在工作。
nginx的工作流程是:對外,nginx是一個服務器,所有的請求都先請求到nginx,然后再由nginx對內網進行請求的分發到tomcat,然后tomcat處理完請求后將數據發送給nginx,然后由nginx發送給用戶,整個過程對用戶的感覺就是nginx在處理用戶請求。既然這樣子,nginx肯定需要進行配置,主要的配置文件是conf文件夾下的nginx.conf,因為我主要是進行了靜態與動態分離,所以沒有進行靜態文件緩存,也沒有進行負載均衡的配置。
1 #user nobody; 2 worker_processes 2; 3 4 #error_log logs/error.log; 5 #error_log logs/error.log notice; 6 #error_log logs/error.log info; 7 8 #pid logs/nginx.pid; 9 10 11 events { 12 #nginx默認最大並發數是1024個用戶線程 13 worker_connections 1024; 14 } 15 16 17 http { 18 include mime.types; 19 default_type application/octet-stream; 20 21 #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 22 # '$status $body_bytes_sent "$http_referer" ' 23 # '"$http_user_agent" "$http_x_forwarded_for"'; 24 25 #access_log logs/access.log main; 26 27 sendfile on; 28 #tcp_nopush on; 29 30 #keepalive_timeout 0; 31 #http1.1在請求完之后還會保留一段時間的連接,所以這里的timeout時長不能太大,也不能太小, 32 #太小每次都要建立連接,太大會浪費系統資源(用戶不再請求服務器) 33 keepalive_timeout 65; 34 35 #gzip on; 36 37 server { 38 #nginx監聽80端口 39 listen 80; 40 server_name localhost; 41 42 #charset koi8-r; 43 44 #access_log logs/host.access.log main; 45 #這里的/表示所有的請求 46 #location / { 47 #將80端口的所有請求都轉發到8080端口去處理,proxy_pass代表的是代理路徑 48 # proxy_pass http://localhost:8080; 49 # root html; 50 # index index.html index.htm; 51 #} 52 53 #對項目名進行訪問就去訪問tomcat服務 54 location /Student_Vote { 55 proxy_pass http://localhost:8080; 56 } 57 58 #對jsp和do結尾的url也去訪問tomcat服務 59 location ~ \.(jsp|do)$ { 60 proxy_pass http://localhost:8080; 61 } 62 63 #對js、css、png、gif結尾的都去訪問根目錄下查找 64 location ~ \.(js|css|png|gif)$ { 65 root F:/javaweb; 66 } 67 68 69 #error_page 404 /404.html; 70 71 # redirect server error pages to the static page /50x.html 72 # 73 error_page 500 502 503 504 /50x.html; 74 location = /50x.html { 75 root html; 76 } 77 78 # proxy the PHP scripts to Apache listening on 127.0.0.1:80 79 # 80 #location ~ \.php$ { 81 # proxy_pass http://127.0.0.1; 82 #} 83 84 # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 85 # 86 #location ~ \.php$ { 87 # root html; 88 # fastcgi_pass 127.0.0.1:9000; 89 # fastcgi_index index.php; 90 # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; 91 # include fastcgi_params; 92 #} 93 94 # deny access to .htaccess files, if Apache's document root 95 # concurs with nginx's one 96 # 97 #location ~ /\.ht { 98 # deny all; 99 #} 100 } 101 102 103 # another virtual host using mix of IP-, name-, and port-based configuration 104 # 105 #server { 106 # listen 8000; 107 # listen somename:8080; 108 # server_name somename alias another.alias; 109 110 # location / { 111 # root html; 112 # index index.html index.htm; 113 # } 114 #} 115 116 117 # HTTPS server 118 # 119 #server { 120 # listen 443 ssl; 121 # server_name localhost; 122 123 # ssl_certificate cert.pem; 124 # ssl_certificate_key cert.key; 125 126 # ssl_session_cache shared:SSL:1m; 127 # ssl_session_timeout 5m; 128 129 # ssl_ciphers HIGH:!aNULL:!MD5; 130 # ssl_prefer_server_ciphers on; 131 132 # location / { 133 # root html; 134 # index index.html index.htm; 135 # } 136 #} 137 138 }
上面的配置中我把默認的location /給注釋掉了,因為它會攔截所有的請求,無論是動態還是靜態,還有一個就是對靜態文件的配置我配置成了javaweb的工作區間,接下來會說明為什么。
因為之前寫的項目一直以來都是使用jsp內置對象來進行目錄的文件訪問,但是使用了nginx一切都需要改變,當我使用了nginx,並且項目沒有進行路徑的修改的時候,總是無法加載靜態文件,查看日志發現這樣的錯誤: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",大致信息是根據jsp中文件的配置,nginx將會從/Stdent_Vote(這是我的項目名)/lib/images包中查找靜態文件,而我又不想對項目文件做太大變化,其實還有一種方法是不使用jsp的內置對象,直接使用http://localhost/username.png來代替內置對象訪問靜態文件,但是這樣改要改很多的地方,所以我就直接將web-inf文件夾下的lib文件夾拷到上一個文件夾,也就是該文件夾和web-inf文件夾是兄弟文件夾的關系。
通過上述操作,就實現了動態與靜態的分離了,無圖無真相,下面展示效果圖。
上圖可以看到server是“Apache-Coyote/1.1”。tomcat的連接器就是這個。
而上面的server可以看到是nginx,說明對外而言接收請求的服務器是nginx。