博主最近在優(yōu)化一個javaweb項目,該項目之前一直都是使用tomcat處理用戶請求的,無論靜態(tài)還是動態(tài)的東西,一律交給tomcat處理。tomcat主要是負(fù)責(zé)處理servlet的,靜態(tài)的文件還是交給nginx處理,nginx對靜態(tài)文件的處理比tomcat不是只快了一點,并且Nginx的使用對項目并發(fā)能力有很大的提升。下面主要記錄下主要的配置過程:
實驗環(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ù)載均衡的配置。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
#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。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://www.cnblogs.com/liucaihao/p/5513709.html