windows下整合tomcat和nginx
tomcat自帶的apache服務器對于并發(fā)請求的處理能力比較差,并且耗費資源很大,而nginx這方便卻很強悍,以下是在windows下整合tomcat和nginx的過程。
1.準備工作
??下載tomcat(http://tomcat.apache.org/download-70.cgi),下載nginx(http://nginx.org/en/download.html),我用的版本是tomcat 7.0,nginx 0.8.54,分別解壓tomcat,和nginx到獨立的目錄,下圖是我解壓的目錄
如果你的電腦上沒有jdk,那么還需要下載jdk(http://www.oracle.com/technetwork/java/javase/downloads/index.html),我用的jdk版本是1.6 ?。tomcat的運行需要JAVA_HOME環(huán)境變量中加上jdk的路徑。
如圖
我的電腦右鍵--》屬性
高級選項卡--->環(huán)境變量
系統(tǒng)變量中的 新建按鈕--->輸入變量名JAVA_HOME和變量值F:\JAVA\jdk1.6.0_02?( 這里是你自己的jdk路徑?),確定,確定。
2.配置tomcat,接下來,需要修改一下tomcat的默認ROOT目錄,使其指向nginx的目錄。
打開tomcat/conf/server.xml文件。找到<Host>標簽,在<Host>中加入以下內(nèi)容。
<Context path="" docBase="F:\service\nginx-0.8.54\html\www" reloadable="true"></Context>
記得保存,這句話的意思是,在這個主機中,打開的站點為 "F:\service\nginx-0.8.54\html\www" 下的目錄,如圖
--------------------------------------------------------------------
接下來,我們需要在這個目錄中("F:\service\nginx-0.8.54\html\www"?)中新建一個用于測試的index.jsp文件。如圖
index.jsp中可以簡單幾句話用于測試即可:
?
<!DOCTYPE html>
<html lang="en">
?? ?<head>
?? ?</head>
?? ?<body>
?? ? ? ?<h3>this is Tomcat 1+2 = <%=1+2%></h3>
?? ?</body>
</html>
然后我們就可以打開tomcat了(tomcat/bin下面的startup.bat雙擊就好)。默認端口是8080。如圖打開服務器后,我們就可以打開瀏覽器localhost:8080測試tomcat是否配置正確了。如圖 --------------------------------------------------------------- 如果出現(xiàn)了上面這個頁面,則tomcat配置成功,我們就可以放下tomcat了。
3.配置nginx,
打開nginx/conf/nginx.conf文件,這個文件是nginx服務器的核心配置文件。如圖
里面的內(nèi)容替換成
?
#user ?nobody;
worker_processes ?1;
#error_log ?logs/error.log;
#error_log ?logs/error.log ?notice;
#error_log ?logs/error.log ?info;
#pid ? ? ? ?logs/nginx.pid;
events {
?? ?worker_connections ?1024;
}
http {
?? ?include ? ? ? mime.types;
?? ?default_type ?application/octet-stream;
?? ?include ? ? proxy.conf; ?#這個文件是我們新建的,要導入
?? ?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;
?? ?keepalive_timeout ?65;
?? ?#gzip ?on;
?? ?server {
?? ? ? ?listen ? ? ? 80;
?? ? ? ?server_name ?localhost;
?? ? ? ?#charset koi8-r;
?? ? ? ?#access_log ?logs/host.access.log ?main;
?? ? ? ?location / {
?? ? ? ? ? ?root ? html\www;
?? ? ? ? ? ?index ?index.jsp index.html index.htm;
?? ? ? ?}
location ~ .*.jsp$ { ? ? #匹配以jsp結(jié)尾的,tomcat的網(wǎng)頁文件是以jsp結(jié)尾 ? ? ? ??
?? ? ? ? ? ? ? ?index ? index.jsp;
?? ? ? ? ? ? ? ?proxy_pass ? ? ?http://localhost:8080; #主要在這里,設置一個代理
?? ? ? ?}
?? ? ? ?location /nginxstatus {
?? ? ? ? ? ? ? ?stub_status on;
?? ? ? ? ? ? ? ?access_log on;
?? ? ? ? ? ? ? ?auth_basic "nginxstatus";
?? ? ? ? ? ? ? ?auth_basic_user_file htpasswd;
?? ? ? ?}
?? ? ? ?# redirect server error pages to the static page /50x.html
?? ? ? ?#
?? ? ? ?#error_page ? 500 502 503 504 ?/50x.html;
?? ? ? ?#error_page ?404 ?/404.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;
?? ?# ? ?server_name ?localhost;
?? ?# ? ?ssl ? ? ? ? ? ? ? ? ?on;
?? ?# ? ?ssl_certificate ? ? ?cert.pem;
?? ?# ? ?ssl_certificate_key ?cert.key;
?? ?# ? ?ssl_session_timeout ?5m;
?? ?# ? ?ssl_protocols ?SSLv2 SSLv3 TLSv1;
?? ?# ? ?ssl_ciphers ?ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
?? ?# ? ?ssl_prefer_server_ciphers ? on;
?? ?# ? ?location / {
?? ?# ? ? ? ?root ? html;
?? ?# ? ? ? ?index ?index.html index.htm;
?? ?# ? ?}
?? ?#}
}
--------------------------------------------------------在conf文件夾下再建一文件proxy.conf,內(nèi)容如下
# proxy.conf
proxy_redirect ? ? ? ? ?off;
proxy_set_header ? ? ? ?Host $host;
proxy_set_header ? ? ? ?X-Real-IP $remote_addr; #獲取真實IP
#proxy_set_header ? ? ? X-Forwarded-For ? $proxy_add_x_forwarded_for; #獲取代理者的真實ip
client_max_body_size ? ?10m;
client_body_buffer_size 128k;
proxy_connect_timeout ? 90;
proxy_send_timeout ? ? ?90;
proxy_read_timeout ? ? ?90;
proxy_buffer_size ? ? ? 4k;
proxy_buffers ? ? ? ? ? 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
-------------------------------------------------------------
雙擊nginx.exe,啟動服務器,nginx的默認端口是80,所以和tomcat不會沖突,如果tomcat也是80的話,那么就需要調(diào)整一下了。
打開瀏覽器,輸入localhost回車,如果我們看到了以下的內(nèi)容,則表示配置成功了,如圖
------------------------------------------------------------
這是nginx默認的錯誤頁,如圖
-----------------------------------------------------------------
至此,tomcat和nginx已經(jīng)整合成功了。
轉(zhuǎn)載于:https://blog.51cto.com/lya041/691008
總結(jié)
以上是生活随笔為你收集整理的windows下整合tomcat和nginx的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ubuntu11.10 64bits机器
- 下一篇: Windows Phone 知识锦(12