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到獨立的目錄,下圖是我解壓的目錄
2.配置tomcat,接下來,需要修改一下tomcat的默認ROOT目錄,使其指向nginx的目錄。
打開tomcat/conf/server.xml文件。找到<Host>標籤,在<Host>中加入以下內容。
<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是否配置正確了
3.
配置nginx,
打開nginx/conf/nginx.conf文件,這個文件是nginx服務器的核心配置文件。
裏面的內容替換成
#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結尾的,tomcat的網頁文件是以jsp結尾
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,內容如下
# 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的話,那麼就需要調整一下了。
打開瀏覽器,輸入localhost回車,如果我們看到了以下的內容,則表示配置成功了,如圖
------------------------------------------------------------
這是nginx默認的錯誤頁,如圖
-----------------------------------------------------------------
至此,tomcat和nginx已經整合成功了。